Skip to content

Commit 193649c

Browse files
Merge pull request #6 from react-declarative/apwrite-1-5
Apwrite 1 5
2 parents 5b9767d + 1c19f79 commit 193649c

File tree

3 files changed

+61
-115
lines changed

3 files changed

+61
-115
lines changed

package-lock.json

Lines changed: 24 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"dependencies": {
2424
"dotenv": "16.3.1",
2525
"glob": "10.3.4",
26-
"node-appwrite": "10.0.0"
26+
"node-appwrite": "12.0.1"
2727
},
2828
"devDependencies": {
2929
"@types/glob": "8.1.0"

scripts/create-collection.mjs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const GET_COLLECTION = (id) => {
2424
};
2525

2626
const DATABASE_ID = '64c4de8e7b30179809ef';
27-
const COLLECTION_ID = '64d7502a5ce9f881b680';
2827

2928
const client = new sdk.Client();
3029
const databases = new sdk.Databases(client);
@@ -37,37 +36,56 @@ if (process.env.APPWRITE_SELF_SIGNED) {
3736
client.setSelfSigned();
3837
}
3938

40-
const hasCollection = async () => {
41-
try {
42-
return await databases.getCollection(DATABASE_ID, COLLECTION_ID)
43-
} catch {
44-
return null
45-
}
46-
}
47-
4839
const sleep = (timeout = 1_000) => new Promise((res) => {
4940
setTimeout(() => {
5041
res();
5142
}, timeout);
5243
});
5344

54-
const { attributes, name } = GET_COLLECTION(COLLECTION_ID);
5545

56-
{
57-
if (!(await hasCollection())) {
58-
console.log(`Creating collection id=${COLLECTION_ID} name=${name}`);
59-
await databases.createCollection(DATABASE_ID, COLLECTION_ID, name);
46+
for (const collectionId of schema.collections.map(({ $id }) => $id)) {
47+
48+
const { attributes, name } = GET_COLLECTION(collectionId);
49+
50+
const hasCollection = async () => {
51+
try {
52+
return await databases.getCollection(DATABASE_ID, collectionId)
53+
} catch {
54+
return null
55+
}
56+
};
57+
58+
const hasAttribute = async (key) => {
59+
try {
60+
return await databases.getAttribute(DATABASE_ID, collectionId, key)
61+
} catch {
62+
return null
63+
}
64+
};
65+
66+
if (await hasCollection()) {
67+
console.log(`Found collection id=${collectionId} name=${name}`);
68+
} else {
69+
console.log(`Creating collection id=${collectionId} name=${name}`);
70+
await databases.createCollection(DATABASE_ID, collectionId, name);
6071
}
72+
6173
for (const { key, type, required, array, size } of attributes) {
62-
console.log(`creating ${key}: type=${type} array=${array} size=${size}`)
74+
if (await hasAttribute(key)) {
75+
console.log(`skip ${key}: type=${type} array=${array} size=${size}`)
76+
await sleep(1_500);
77+
continue;
78+
} else {
79+
console.log(`creating ${key}: type=${type} array=${array} size=${size}`)
80+
}
6381
if (type === "string") {
64-
await databases.createStringAttribute(DATABASE_ID, COLLECTION_ID, key, size, required, undefined, array);
82+
await databases.createStringAttribute(DATABASE_ID, collectionId, key, size, required, undefined, array);
6583
}
6684
if (type === "boolean") {
67-
await databases.createBooleanAttribute(DATABASE_ID, COLLECTION_ID, key, required, undefined, array);
85+
await databases.createBooleanAttribute(DATABASE_ID, collectionId, key, required, undefined, array);
6886
}
6987
if (type === "integer") {
70-
await databases.createIntegerAttribute(DATABASE_ID, COLLECTION_ID, key, required, undefined, undefined, undefined, array);
88+
await databases.createIntegerAttribute(DATABASE_ID, collectionId, key, required, undefined, undefined, undefined, array);
7189
}
7290
await sleep(1_500);
7391
}

0 commit comments

Comments
 (0)