mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { LiveProvider } from "@refinedev/core";
|
|
import { Client as Appwrite } from "appwrite";
|
|
import { getRefineEvent } from "./utils";
|
|
|
|
export const liveProvider = (
|
|
appwriteClient: Appwrite,
|
|
options: { databaseId: string } = { databaseId: "default" },
|
|
): LiveProvider => {
|
|
const { databaseId } = options;
|
|
return {
|
|
subscribe: ({ channel, types, params, callback }): any => {
|
|
const resource = channel.replace("resources/", "");
|
|
|
|
let appwriteChannel;
|
|
|
|
if (params?.ids) {
|
|
appwriteChannel = params.ids.map(
|
|
(id) =>
|
|
`databases.${databaseId}.collections.${resource}.documents.${id}`,
|
|
);
|
|
} else {
|
|
appwriteChannel = `databases.${databaseId}.collections.${resource}.documents`;
|
|
}
|
|
|
|
return appwriteClient.subscribe(appwriteChannel, (event) => {
|
|
const refineEvent = getRefineEvent(event.events[0]);
|
|
if (
|
|
types.includes("*") ||
|
|
(refineEvent && types.includes(refineEvent))
|
|
) {
|
|
callback({
|
|
channel,
|
|
type:
|
|
getRefineEvent(event.events[0]) ?? event.events[0],
|
|
payload: event.payload as any,
|
|
date: new Date(event.timestamp * 1000),
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
unsubscribe: async (unsubscribe: () => void) => {
|
|
unsubscribe();
|
|
},
|
|
};
|
|
};
|