Skip to content

Commit

Permalink
feat(appwrite): add swizzle to appwrite (refinedev#4269)
Browse files Browse the repository at this point in the history
* feat(appwrite): add swizzle to appwrite

* chore(changeset): major to minor

Co-authored-by: Salih Özdemir <[email protected]>

---------

Co-authored-by: Salih Özdemir <[email protected]>
  • Loading branch information
alicanerdurmaz and salihozdemir authored May 5, 2023
1 parent ee8b71e commit 396bf06
Show file tree
Hide file tree
Showing 17 changed files with 655 additions and 349 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-shoes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@refinedev/appwrite": minor
---

feat: added swizzle to appwrite
1 change: 1 addition & 0 deletions packages/appwrite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"module": "dist/esm/index.js",
"devDependencies": {
"@refinedev/core": "^4.1.0",
"@refinedev/cli": "^2.5.3",
"@esbuild-plugins/node-resolve": "^0.1.4",
"@types/jest": "^29.2.4",
"jest": "^29.3.1",
Expand Down
73 changes: 73 additions & 0 deletions packages/appwrite/refine.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/** @type {import('@refinedev/cli').RefineConfig} */
module.exports = {
group: "Data Provider",
swizzle: {
items: [
{
label: "Data Provider",
requiredPackages: ["[email protected]"],
files: [
{
src: "./src/index.ts",
dest: "./providers/appwrite/index.ts",
},
{
src: "./src/dataProvider.ts",
dest: "./providers/appwrite/dataProvider.ts",
},
{
src: "./src/liveProvider.ts",
dest: "./providers/appwrite/liveProvider.ts",
},
{
src: "./src/utils/index.ts",
dest: "./providers/appwrite/utils/index.ts",
},
{
src: "./src/utils/generateFilter.ts",
dest: "./providers/appwrite/utils/generateFilter.ts",
},
{
src: "./src/utils/getAppwriteFilters.ts",
dest: "./providers/appwrite/utils/getAppwriteFilters.ts",
},
{
src: "./src/utils/getAppwritePagination.ts",
dest: "./providers/appwrite/utils/getAppwritePagination.ts",
},
{
src: "./src/utils/getAppwriteSorting.ts",
dest: "./providers/appwrite/utils/getAppwriteSorting.ts",
},
{
src: "./src/utils/getRefineEvent.ts",
dest: "./providers/appwrite/utils/getRefineEvent.ts",
},
],
message: `
**\`Usage\`**
\`\`\`
// title: App.tsx
import { dataProvider, liveProvider } from "./providers/appwrite";
import { appwriteClient } from "utility";
const App = () => {
return (
<Refine
dataProvider={dataProvider(appwriteClient, {
databaseId: "default",
})}
liveProvider={liveProvider(appwriteClient, {
databaseId: "default",
})}
/* ... */
/>
);
}
\`\`\`
`,
},
],
},
};
213 changes: 213 additions & 0 deletions packages/appwrite/src/dataProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import { DataProvider } from "@refinedev/core";
import { Client as Appwrite, Databases, Permission, Role, ID } from "appwrite";
import {
getAppwriteFilters,
getAppwritePagination,
getAppwriteSorting,
} from "./utils";

export const dataProvider = (
appwriteClient: Appwrite,
options: { databaseId: string } = { databaseId: "default" },
): Required<DataProvider> => {
const { databaseId } = options;

const database = new Databases(appwriteClient);

return {
getList: async ({ resource, pagination, filters, sorters }) => {
const {
current = 1,
pageSize = 10,
mode = "server",
} = pagination ?? {};

const appwriteFilters = getAppwriteFilters(filters);

const appwritePagination =
mode === "server"
? getAppwritePagination(current, pageSize)
: [];

const appwriteSorts = getAppwriteSorting(sorters);

const { total: total, documents: data } =
await database.listDocuments<any>(databaseId, resource, [
...appwriteFilters,
...appwritePagination,
...appwriteSorts,
]);

return {
data: data.map(({ $id, ...restData }: { $id: string }) => ({
id: $id,
...restData,
})) as any,
total,
};
},
getOne: async ({ resource, id }) => {
const { $id, ...restData } = await database.getDocument(
databaseId,
resource,
id.toString(),
);

return {
data: {
id: $id,
...restData,
},
} as any;
},
update: async ({ resource, id, variables, meta }) => {
const permissions = [
Permission.read(Role.any()),
Permission.write(Role.any()),
...(meta?.readPermissions ?? ""),
...(meta?.writePermissions ?? ""),
];
const { $id, ...restData } = await database.updateDocument(
databaseId,
resource,
id.toString(),
variables as any,
permissions,
);

return {
data: {
id: $id,
...restData,
},
} as any;
},
create: async ({ resource, variables, meta }) => {
const permissions = [
Permission.read(Role.any()),
Permission.write(Role.any()),
...(meta?.readPermissions ?? ""),
...(meta?.writePermissions ?? ""),
];

const { $id, ...restData } = await database.createDocument(
databaseId,
resource,
meta?.documentId ?? ID.unique(),
variables as unknown as object,
permissions,
);

return {
data: {
id: $id,
...restData,
},
} as any;
},
createMany: async ({ resource, variables, meta }) => {
const permissions = [
Permission.read(Role.any()),
Permission.write(Role.any()),
...(meta?.readPermissions ?? ""),
...(meta?.writePermissions ?? ""),
];
const data = await Promise.all(
variables.map((document) =>
database.createDocument<any>(
databaseId,
resource,
meta?.documentId ?? ID.unique(),
document as unknown as any,
permissions,
),
),
);

return {
data: data.map(({ $id, ...restData }) => ({
id: $id,
...restData,
})),
} as any;
},
deleteOne: async ({ resource, id }) => {
await database.deleteDocument(databaseId, resource, id.toString());

return {
data: { id },
} as any;
},
deleteMany: async ({ resource, ids }) => {
await Promise.all(
ids.map((id) =>
database.deleteDocument(
databaseId,
resource,
id.toString(),
),
),
);

return {
data: ids.map((id) => ({
id,
})),
} as any;
},
getMany: async ({ resource, ids }) => {
const data = await Promise.all(
ids.map((id) =>
database.getDocument<any>(
databaseId,
resource,
id.toString(),
),
),
);

return {
data: data.map(({ $id, ...restData }) => ({
id: $id,
...restData,
})),
} as any;
},
updateMany: async ({ resource, ids, variables, meta }) => {
const permissions = [
Permission.read(Role.any()),
Permission.write(Role.any()),
...(meta?.readPermissions ?? ""),
...(meta?.writePermissions ?? ""),
];
const data = await Promise.all(
ids.map((id) =>
database.updateDocument<any>(
databaseId,
resource,
id.toString(),
variables as unknown as object,
permissions,
),
),
);

return {
data: data.map(({ $id, ...restData }) => ({
id: $id,
...restData,
})),
} as any;
},
getApiUrl: () => {
throw Error(
"'getApiUrl' method is not implemented on refine-appwrite data provider.",
);
},
custom: () => {
throw Error(
"'custom' method is not implemented on refine-appwrite data provider.",
);
},
};
};
Loading

0 comments on commit 396bf06

Please sign in to comment.