appwrite-utils-cli 0.0.267 → 0.0.269

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -112,12 +112,11 @@ export const generateSchemas = async (config, appwriteFolderPath) => {
112
112
  };
113
113
  export const createOrUpdateCollections = async (database, databaseId, config, deletedCollections) => {
114
114
  const configCollections = config.collections;
115
+ const usedIds = new Set(); // To track IDs used in this operation
115
116
  for (const { attributes, indexes, ...collection } of configCollections) {
116
- let collectionsFound = await database.listCollections(databaseId, [
117
- Query.equal("name", collection.name),
118
- ]);
117
+ // Prepare permissions for the collection
119
118
  const permissions = [];
120
- if (collection.$permissions.length > 0) {
119
+ if (collection.$permissions && collection.$permissions.length > 0) {
121
120
  for (const permission of collection.$permissions) {
122
121
  switch (permission.permission) {
123
122
  case "read":
@@ -141,36 +140,48 @@ export const createOrUpdateCollections = async (database, databaseId, config, de
141
140
  }
142
141
  }
143
142
  }
143
+ // Check if the collection already exists by name
144
+ let collectionsFound = await database.listCollections(databaseId, [
145
+ Query.equal("name", collection.name),
146
+ ]);
144
147
  let collectionToUse = collectionsFound.total > 0 ? collectionsFound.collections[0] : null;
148
+ // Determine the correct ID for the collection
149
+ let collectionId;
145
150
  if (!collectionToUse) {
146
151
  console.log(`Creating collection: ${collection.name}`);
147
- if (deletedCollections && deletedCollections.length > 0) {
148
- const foundColl = deletedCollections.find((coll) => coll.collectionName.toLowerCase().trim().replace(" ", "") ===
149
- collection.name.toLowerCase().trim().replace(" ", ""));
150
- if (foundColl) {
151
- const collectionId = collection.$id || foundColl.collectionId || ID.unique();
152
- console.log(`Processing collection: ${collection.name} with ID: ${collectionId}`);
153
- collectionToUse = await database.createCollection(databaseId, collectionId, collection.name, permissions, collection.documentSecurity, collection.enabled);
154
- nameToIdMapping.set(collection.name, collectionToUse.$id);
155
- }
156
- else {
157
- collectionToUse = await database.createCollection(databaseId, ID.unique(), collection.name, permissions, collection.documentSecurity, collection.enabled);
158
- nameToIdMapping.set(collection.name, collectionToUse.$id);
159
- }
152
+ const foundColl = deletedCollections?.find((coll) => coll.collectionName.toLowerCase().trim().replace(" ", "") ===
153
+ collection.name.toLowerCase().trim().replace(" ", ""));
154
+ if (foundColl && !usedIds.has(foundColl.collectionId)) {
155
+ collectionId = foundColl.collectionId; // Use ID from deleted collection if not already used
156
+ }
157
+ else if (collection.$id && !usedIds.has(collection.$id)) {
158
+ collectionId = collection.$id; // Use the provided $id if not already used
160
159
  }
161
160
  else {
162
- collectionToUse = await database.createCollection(databaseId, ID.unique(), collection.name, permissions, collection.documentSecurity, collection.enabled);
161
+ collectionId = ID.unique(); // Generate a new unique ID
162
+ }
163
+ usedIds.add(collectionId); // Mark this ID as used
164
+ // Create the collection with the determined ID
165
+ try {
166
+ collectionToUse = await database.createCollection(databaseId, collectionId, collection.name, permissions, collection.documentSecurity, collection.enabled);
167
+ collection.$id = collectionToUse.$id;
163
168
  nameToIdMapping.set(collection.name, collectionToUse.$id);
164
169
  }
170
+ catch (error) {
171
+ console.error(`Failed to create collection ${collection.name} with ID ${collectionId}: ${error}`);
172
+ continue; // Skip to the next collection on failure
173
+ }
165
174
  }
166
175
  else {
167
176
  console.log(`Collection ${collection.name} already exists.`);
168
177
  }
178
+ // Update attributes and indexes for the collection
169
179
  console.log("Creating Attributes");
170
180
  await createUpdateCollectionAttributes(database, databaseId, collectionToUse, attributes);
171
181
  console.log("Creating Indexes");
172
182
  await createOrUpdateIndexes(databaseId, database, collectionToUse.$id, indexes);
173
183
  }
184
+ // Process any remaining tasks in the queue
174
185
  await processQueue(database, databaseId);
175
186
  };
176
187
  export const generateMockData = async (database, databaseId, configCollections) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "appwrite-utils-cli",
3
3
  "description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
4
- "version": "0.0.267",
4
+ "version": "0.0.269",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -157,13 +157,12 @@ export const createOrUpdateCollections = async (
157
157
  deletedCollections?: { collectionId: string; collectionName: string }[]
158
158
  ): Promise<void> => {
159
159
  const configCollections = config.collections;
160
- for (const { attributes, indexes, ...collection } of configCollections) {
161
- let collectionsFound = await database.listCollections(databaseId, [
162
- Query.equal("name", collection.name),
163
- ]);
160
+ const usedIds = new Set(); // To track IDs used in this operation
164
161
 
162
+ for (const { attributes, indexes, ...collection } of configCollections) {
163
+ // Prepare permissions for the collection
165
164
  const permissions = [];
166
- if (collection.$permissions.length > 0) {
165
+ if (collection.$permissions && collection.$permissions.length > 0) {
167
166
  for (const permission of collection.$permissions) {
168
167
  switch (permission.permission) {
169
168
  case "read":
@@ -187,56 +186,58 @@ export const createOrUpdateCollections = async (
187
186
  }
188
187
  }
189
188
  }
189
+
190
+ // Check if the collection already exists by name
191
+ let collectionsFound = await database.listCollections(databaseId, [
192
+ Query.equal("name", collection.name),
193
+ ]);
194
+
190
195
  let collectionToUse =
191
196
  collectionsFound.total > 0 ? collectionsFound.collections[0] : null;
197
+
198
+ // Determine the correct ID for the collection
199
+ let collectionId;
192
200
  if (!collectionToUse) {
193
201
  console.log(`Creating collection: ${collection.name}`);
194
- if (deletedCollections && deletedCollections.length > 0) {
195
- const foundColl = deletedCollections.find(
196
- (coll) =>
197
- coll.collectionName.toLowerCase().trim().replace(" ", "") ===
198
- collection.name.toLowerCase().trim().replace(" ", "")
199
- );
200
- if (foundColl) {
201
- const collectionId =
202
- collection.$id || foundColl.collectionId || ID.unique();
203
- console.log(
204
- `Processing collection: ${collection.name} with ID: ${collectionId}`
205
- );
206
- collectionToUse = await database.createCollection(
207
- databaseId,
208
- collectionId,
209
- collection.name,
210
- permissions,
211
- collection.documentSecurity,
212
- collection.enabled
213
- );
214
- nameToIdMapping.set(collection.name, collectionToUse.$id);
215
- } else {
216
- collectionToUse = await database.createCollection(
217
- databaseId,
218
- ID.unique(),
219
- collection.name,
220
- permissions,
221
- collection.documentSecurity,
222
- collection.enabled
223
- );
224
- nameToIdMapping.set(collection.name, collectionToUse.$id);
225
- }
202
+ const foundColl = deletedCollections?.find(
203
+ (coll) =>
204
+ coll.collectionName.toLowerCase().trim().replace(" ", "") ===
205
+ collection.name.toLowerCase().trim().replace(" ", "")
206
+ );
207
+
208
+ if (foundColl && !usedIds.has(foundColl.collectionId)) {
209
+ collectionId = foundColl.collectionId; // Use ID from deleted collection if not already used
210
+ } else if (collection.$id && !usedIds.has(collection.$id)) {
211
+ collectionId = collection.$id; // Use the provided $id if not already used
226
212
  } else {
213
+ collectionId = ID.unique(); // Generate a new unique ID
214
+ }
215
+
216
+ usedIds.add(collectionId); // Mark this ID as used
217
+
218
+ // Create the collection with the determined ID
219
+ try {
227
220
  collectionToUse = await database.createCollection(
228
221
  databaseId,
229
- ID.unique(),
222
+ collectionId,
230
223
  collection.name,
231
224
  permissions,
232
225
  collection.documentSecurity,
233
226
  collection.enabled
234
227
  );
228
+ collection.$id = collectionToUse.$id;
235
229
  nameToIdMapping.set(collection.name, collectionToUse.$id);
230
+ } catch (error) {
231
+ console.error(
232
+ `Failed to create collection ${collection.name} with ID ${collectionId}: ${error}`
233
+ );
234
+ continue; // Skip to the next collection on failure
236
235
  }
237
236
  } else {
238
237
  console.log(`Collection ${collection.name} already exists.`);
239
238
  }
239
+
240
+ // Update attributes and indexes for the collection
240
241
  console.log("Creating Attributes");
241
242
  await createUpdateCollectionAttributes(
242
243
  database,
@@ -252,6 +253,7 @@ export const createOrUpdateCollections = async (
252
253
  indexes
253
254
  );
254
255
  }
256
+ // Process any remaining tasks in the queue
255
257
  await processQueue(database, databaseId);
256
258
  };
257
259