forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(appwrite): add swizzle to appwrite (refinedev#4269)
* 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
1 parent
ee8b71e
commit 396bf06
Showing
17 changed files
with
655 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@refinedev/appwrite": minor | ||
--- | ||
|
||
feat: added swizzle to appwrite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
})} | ||
/* ... */ | ||
/> | ||
); | ||
} | ||
\`\`\` | ||
`, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
); | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.