appwrite-utils-cli 0.0.12 → 0.0.13

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.
@@ -119,8 +119,8 @@ export class ImportController {
119
119
  continue;
120
120
  console.log(`Processing update definitions for collection ID: ${collection.$id}`);
121
121
  await this.processBatch(db, collection, importDef, dataToImport);
122
- await setAllPendingAfterImportActionsToReady(this.database, db.$id, collection.$id);
123
122
  }
123
+ await setAllPendingAfterImportActionsToReady(this.database, db.$id, collection.$id);
124
124
  }
125
125
  async loadData(importDef) {
126
126
  const filePath = path.resolve(this.appwriteFolderPath, importDef.filePath);
@@ -158,7 +158,6 @@ export class ImportController {
158
158
  let associatedDoc;
159
159
  if (isMembersCollection &&
160
160
  (finalItem.hasOwnProperty("email") || item.hasOwnProperty("phone"))) {
161
- console.log("Found members collection, creating user...");
162
161
  const usersController = new UsersController(this.config, this.database);
163
162
  const userToCreate = AuthUserCreateSchema.safeParse({
164
163
  ...finalItem,
@@ -172,7 +171,6 @@ export class ImportController {
172
171
  createIdToUse = user.$id;
173
172
  context.docId = createIdToUse;
174
173
  context = { ...context, ...user };
175
- console.log("Created user, deleting keys in finalItem that exist in user...");
176
174
  const associatedDocFound = await this.database.listDocuments(db.$id, context.collId, [Query.equal("$id", createIdToUse)]);
177
175
  if (associatedDocFound.documents.length > 0) {
178
176
  associatedDoc = associatedDocFound.documents[0];
@@ -185,7 +183,6 @@ export class ImportController {
185
183
  deletedKeys.push(key);
186
184
  }
187
185
  });
188
- console.log(`Set createIdToUse to ${createIdToUse}. Deleted keys: ${deletedKeys.join(", ")}.`);
189
186
  }
190
187
  else if (isMembersCollection) {
191
188
  logger.error(`Skipping user & contact creation for ${item} due to lack of email...`);
@@ -201,15 +198,13 @@ export class ImportController {
201
198
  !associatedDoc) {
202
199
  const createdContext = await this.handleCreate(context, finalItem, updateDefs, createIdToUse);
203
200
  context = { ...context, ...createdContext };
204
- logger.info(`Handled create for ${context.docId}}`);
205
201
  }
206
202
  else {
207
203
  const updatedContext = await this.handleUpdate(context, finalItem, importDef);
208
204
  context = { ...context, ...updatedContext };
209
- logger.info(`Handled update for ${context.docId}`);
210
205
  }
211
206
  const afterImportActionContext = structuredClone(context);
212
- const attributeMappingsWithActions = this.getAttributeMappingsWithActions(importDef.attributeMappings, context, finalItem);
207
+ const attributeMappingsWithActions = this.getAttributeMappingsWithActions(importDef.attributeMappings, afterImportActionContext, finalItem);
213
208
  if (attributeMappingsWithActions.some((m) => m.postImportActions)) {
214
209
  logger.info(`Pushing to post-import actions queue for ${context.docId}`);
215
210
  const afterImportOperationContext = ContextObject.parse({
@@ -238,9 +233,6 @@ export class ImportController {
238
233
  async handleCreate(context, finalItem, updateDefs, id) {
239
234
  const existing = await documentExists(this.database, context.dbId, context.collId, finalItem);
240
235
  if (!existing) {
241
- if (id) {
242
- console.log(`Creating document with provided ID (member): ${id}`);
243
- }
244
236
  const createdDoc = await this.database.createDocument(context.dbId, context.collId, id || ID.unique(), finalItem);
245
237
  context.docId = createdDoc.$id;
246
238
  context.createdDoc = createdDoc;
@@ -253,7 +245,6 @@ export class ImportController {
253
245
  }
254
246
  });
255
247
  }
256
- console.log(`Created document ID: ${createdDoc.$id}`);
257
248
  return context;
258
249
  }
259
250
  else {
@@ -354,13 +345,18 @@ export class ImportController {
354
345
  for (const batch of resultsData) {
355
346
  const actionOperation = ContextObject.parse(JSON.parse(batch.data));
356
347
  const { context, finalItem, attributeMappings } = actionOperation;
348
+ if (finalItem.$id && !context.docId) {
349
+ context.docId = finalItem.$id;
350
+ logger.info(`Setting docId to ${finalItem.$id} because docId not found in context, batch ${batch.$id}, context is ${JSON.stringify(context)}`);
351
+ }
357
352
  try {
358
353
  await this.importDataActions.executeAfterImportActions(finalItem, attributeMappings, context);
359
354
  // Mark batch as processed
360
355
  await this.database.deleteDocument("migrations", "batches", batch.$id);
361
356
  }
362
357
  catch (error) {
363
- logger.error(`Failed to execute batch ${batch.$id}:`, error);
358
+ logger.error(`Failed to execute batch ${batch.$id}:`, error, "Context is :", context);
359
+ await this.database.deleteDocument("migrations", "batches", batch.$id);
364
360
  }
365
361
  }
366
362
  // After processing all batches, update the operation status
@@ -1,7 +1,7 @@
1
1
  import winston from "winston";
2
2
  export const logger = winston.createLogger({
3
3
  level: "info",
4
- format: winston.format.prettyPrint(),
4
+ format: winston.format.json({ space: 2 }),
5
5
  defaultMeta: { service: "appwrite-utils-cli" },
6
6
  transports: [
7
7
  //
@@ -9,6 +9,7 @@ export const logger = winston.createLogger({
9
9
  // - Write all logs with importance level of `info` or less to `combined.log`
10
10
  //
11
11
  new winston.transports.File({ filename: "error.log", level: "error" }),
12
+ new winston.transports.File({ filename: "warn.log", level: "warn" }),
12
13
  new winston.transports.File({ filename: "combined.log" }),
13
14
  ],
14
15
  });
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.12",
4
+ "version": "0.0.13",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -199,12 +199,12 @@ export class ImportController {
199
199
  `Processing update definitions for collection ID: ${collection.$id}`
200
200
  );
201
201
  await this.processBatch(db, collection, importDef, dataToImport);
202
- await setAllPendingAfterImportActionsToReady(
203
- this.database,
204
- db.$id,
205
- collection.$id
206
- );
207
202
  }
203
+ await setAllPendingAfterImportActionsToReady(
204
+ this.database,
205
+ db.$id,
206
+ collection.$id
207
+ );
208
208
  }
209
209
 
210
210
  async loadData(importDef: ImportDef): Promise<any[]> {
@@ -269,7 +269,6 @@ export class ImportController {
269
269
  isMembersCollection &&
270
270
  (finalItem.hasOwnProperty("email") || item.hasOwnProperty("phone"))
271
271
  ) {
272
- console.log("Found members collection, creating user...");
273
272
  const usersController = new UsersController(
274
273
  this.config,
275
274
  this.database
@@ -288,9 +287,6 @@ export class ImportController {
288
287
  createIdToUse = user.$id;
289
288
  context.docId = createIdToUse;
290
289
  context = { ...context, ...user };
291
- console.log(
292
- "Created user, deleting keys in finalItem that exist in user..."
293
- );
294
290
  const associatedDocFound = await this.database.listDocuments(
295
291
  db.$id,
296
292
  context.collId,
@@ -307,11 +303,6 @@ export class ImportController {
307
303
  deletedKeys.push(key);
308
304
  }
309
305
  });
310
- console.log(
311
- `Set createIdToUse to ${createIdToUse}. Deleted keys: ${deletedKeys.join(
312
- ", "
313
- )}.`
314
- );
315
306
  } else if (isMembersCollection) {
316
307
  logger.error(
317
308
  `Skipping user & contact creation for ${item} due to lack of email...`
@@ -341,7 +332,6 @@ export class ImportController {
341
332
  createIdToUse
342
333
  );
343
334
  context = { ...context, ...createdContext };
344
- logger.info(`Handled create for ${context.docId}}`);
345
335
  } else {
346
336
  const updatedContext = await this.handleUpdate(
347
337
  context,
@@ -349,13 +339,12 @@ export class ImportController {
349
339
  importDef
350
340
  );
351
341
  context = { ...context, ...updatedContext };
352
- logger.info(`Handled update for ${context.docId}`);
353
342
  }
354
343
  const afterImportActionContext = structuredClone(context);
355
344
  const attributeMappingsWithActions =
356
345
  this.getAttributeMappingsWithActions(
357
346
  importDef.attributeMappings,
358
- context,
347
+ afterImportActionContext,
359
348
  finalItem
360
349
  );
361
350
  if (attributeMappingsWithActions.some((m) => m.postImportActions)) {
@@ -404,9 +393,6 @@ export class ImportController {
404
393
  finalItem
405
394
  );
406
395
  if (!existing) {
407
- if (id) {
408
- console.log(`Creating document with provided ID (member): ${id}`);
409
- }
410
396
  const createdDoc = await this.database.createDocument(
411
397
  context.dbId,
412
398
  context.collId,
@@ -428,8 +414,6 @@ export class ImportController {
428
414
  }
429
415
  });
430
416
  }
431
-
432
- console.log(`Created document ID: ${createdDoc.$id}`);
433
417
  return context;
434
418
  } else {
435
419
  console.log("Document already exists, skipping creation.");
@@ -563,6 +547,16 @@ export class ImportController {
563
547
  for (const batch of resultsData) {
564
548
  const actionOperation = ContextObject.parse(JSON.parse(batch.data));
565
549
  const { context, finalItem, attributeMappings } = actionOperation;
550
+ if (finalItem.$id && !context.docId) {
551
+ context.docId = finalItem.$id;
552
+ logger.info(
553
+ `Setting docId to ${
554
+ finalItem.$id
555
+ } because docId not found in context, batch ${
556
+ batch.$id
557
+ }, context is ${JSON.stringify(context)}`
558
+ );
559
+ }
566
560
  try {
567
561
  await this.importDataActions.executeAfterImportActions(
568
562
  finalItem,
@@ -576,7 +570,17 @@ export class ImportController {
576
570
  batch.$id
577
571
  );
578
572
  } catch (error) {
579
- logger.error(`Failed to execute batch ${batch.$id}:`, error);
573
+ logger.error(
574
+ `Failed to execute batch ${batch.$id}:`,
575
+ error,
576
+ "Context is :",
577
+ context
578
+ );
579
+ await this.database.deleteDocument(
580
+ "migrations",
581
+ "batches",
582
+ batch.$id
583
+ );
580
584
  }
581
585
  }
582
586
 
@@ -2,7 +2,7 @@ import winston from "winston";
2
2
 
3
3
  export const logger = winston.createLogger({
4
4
  level: "info",
5
- format: winston.format.prettyPrint(),
5
+ format: winston.format.json({ space: 2 }),
6
6
  defaultMeta: { service: "appwrite-utils-cli" },
7
7
  transports: [
8
8
  //
@@ -10,6 +10,7 @@ export const logger = winston.createLogger({
10
10
  // - Write all logs with importance level of `info` or less to `combined.log`
11
11
  //
12
12
  new winston.transports.File({ filename: "error.log", level: "error" }),
13
+ new winston.transports.File({ filename: "warn.log", level: "warn" }),
13
14
  new winston.transports.File({ filename: "combined.log" }),
14
15
  ],
15
16
  });