appwrite-utils-cli 0.9.983 → 0.9.984

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.
Files changed (42) hide show
  1. package/dist/main.js +56 -42
  2. package/dist/migrations/dataLoader.js +44 -8
  3. package/dist/migrations/importController.js +16 -7
  4. package/dist/migrations/transfer.d.ts +2 -2
  5. package/dist/utilsController.js +39 -26
  6. package/package.json +1 -1
  7. package/src/appwrite.zip +0 -0
  8. package/src/main.ts +77 -52
  9. package/src/migrations/dataLoader.ts +65 -25
  10. package/src/migrations/importController.ts +30 -19
  11. package/src/migrations/transfer.ts +2 -2
  12. package/src/utilsController.ts +62 -53
  13. package/zlogs/album.json +0 -4
  14. package/zlogs/announcements.json +0 -397
  15. package/zlogs/announcementscomments.json +0 -36
  16. package/zlogs/articles.json +0 -138
  17. package/zlogs/articlescomments.json +0 -4
  18. package/zlogs/artist.json +0 -4
  19. package/zlogs/businesscategories.json +0 -7097
  20. package/zlogs/contacts.json +0 -517063
  21. package/zlogs/contactscouncils.json +0 -61905
  22. package/zlogs/contactssociallinks.json +0 -13776
  23. package/zlogs/councils.json +0 -5076
  24. package/zlogs/documents.json +0 -917
  25. package/zlogs/emails.json +0 -4
  26. package/zlogs/events.json +0 -132625
  27. package/zlogs/genre.json +0 -4
  28. package/zlogs/knowledgebase.json +0 -333
  29. package/zlogs/knowledgebasecomments.json +0 -4
  30. package/zlogs/linkcategories.json +0 -180
  31. package/zlogs/links.json +0 -4364
  32. package/zlogs/memberrequests.json +0 -83
  33. package/zlogs/memberrequestscomments.json +0 -65
  34. package/zlogs/mergedUserMap.json +0 -1
  35. package/zlogs/oldIdToNewIdPerCollectionMap.json +0 -1
  36. package/zlogs/playlist.json +0 -4
  37. package/zlogs/regions.json +0 -145
  38. package/zlogs/song.json +0 -4
  39. package/zlogs/testimonials.json +0 -335
  40. package/zlogs/useractivity.json +0 -4
  41. package/zlogs/userdata.json +0 -4
  42. package/zlogs/users.json +0 -4
package/dist/main.js CHANGED
@@ -217,52 +217,66 @@ async function main() {
217
217
  await controller.importData(options);
218
218
  }
219
219
  if (parsedArgv.transfer) {
220
- const isRemote = !!parsedArgv.remoteEndpoint;
221
- const fromDb = await controller.getDatabasesByIds([parsedArgv.fromDbId]);
222
- let toDb;
223
- let targetDatabases;
224
- let targetStorage;
225
- if (isRemote) {
226
- if (!parsedArgv.remoteEndpoint ||
227
- !parsedArgv.remoteProjectId ||
228
- !parsedArgv.remoteApiKey) {
229
- throw new Error("Remote transfer details are missing");
220
+ if (parsedArgv.transfer) {
221
+ const isRemote = !!parsedArgv.remoteEndpoint;
222
+ let fromDb, toDb;
223
+ let targetDatabases;
224
+ let targetStorage;
225
+ // Only fetch databases if database IDs are provided
226
+ if (parsedArgv.fromDbId && parsedArgv.toDbId) {
227
+ fromDb = (await controller.getDatabasesByIds([parsedArgv.fromDbId]))[0];
228
+ if (isRemote) {
229
+ if (!parsedArgv.remoteEndpoint ||
230
+ !parsedArgv.remoteProjectId ||
231
+ !parsedArgv.remoteApiKey) {
232
+ throw new Error("Remote transfer details are missing");
233
+ }
234
+ const remoteClient = getClient(parsedArgv.remoteEndpoint, parsedArgv.remoteProjectId, parsedArgv.remoteApiKey);
235
+ targetDatabases = new Databases(remoteClient);
236
+ targetStorage = new Storage(remoteClient);
237
+ const remoteDbs = await fetchAllDatabases(targetDatabases);
238
+ toDb = remoteDbs.find((db) => db.$id === parsedArgv.toDbId);
239
+ }
240
+ else {
241
+ toDb = (await controller.getDatabasesByIds([parsedArgv.toDbId]))[0];
242
+ }
243
+ if (!fromDb || !toDb) {
244
+ throw new Error("Source or target database not found");
245
+ }
230
246
  }
231
- const remoteClient = getClient(parsedArgv.remoteEndpoint, parsedArgv.remoteProjectId, parsedArgv.remoteApiKey);
232
- targetDatabases = new Databases(remoteClient);
233
- targetStorage = new Storage(remoteClient);
234
- const remoteDbs = await fetchAllDatabases(targetDatabases);
235
- toDb = remoteDbs.find((db) => db.$id === parsedArgv.toDbId);
236
- }
237
- else {
238
- toDb = (await controller.getDatabasesByIds([parsedArgv.toDbId]))[0];
239
- }
240
- if (!fromDb[0] || !toDb) {
241
- throw new Error("Source or target database not found");
242
- }
243
- let sourceBucket, targetBucket;
244
- if (parsedArgv.fromBucketId) {
245
- sourceBucket = await controller.storage?.getBucket(parsedArgv.fromBucketId);
246
- }
247
- if (parsedArgv.toBucketId) {
248
- if (isRemote) {
249
- targetBucket = await targetStorage?.getBucket(parsedArgv.toBucketId);
247
+ // Handle storage setup
248
+ let sourceBucket, targetBucket;
249
+ if (parsedArgv.fromBucketId) {
250
+ sourceBucket = await controller.storage?.getBucket(parsedArgv.fromBucketId);
251
+ }
252
+ if (parsedArgv.toBucketId) {
253
+ if (isRemote) {
254
+ if (!targetStorage) {
255
+ const remoteClient = getClient(parsedArgv.remoteEndpoint, parsedArgv.remoteProjectId, parsedArgv.remoteApiKey);
256
+ targetStorage = new Storage(remoteClient);
257
+ }
258
+ targetBucket = await targetStorage?.getBucket(parsedArgv.toBucketId);
259
+ }
260
+ else {
261
+ targetBucket = await controller.storage?.getBucket(parsedArgv.toBucketId);
262
+ }
250
263
  }
251
- else {
252
- targetBucket = await controller.storage?.getBucket(parsedArgv.toBucketId);
264
+ // Validate that at least one transfer type is specified
265
+ if (!fromDb && !sourceBucket) {
266
+ throw new Error("No source database or bucket specified for transfer");
253
267
  }
268
+ const transferOptions = {
269
+ isRemote,
270
+ fromDb,
271
+ targetDb: toDb,
272
+ transferEndpoint: parsedArgv.remoteEndpoint,
273
+ transferProject: parsedArgv.remoteProjectId,
274
+ transferKey: parsedArgv.remoteApiKey,
275
+ sourceBucket: sourceBucket,
276
+ targetBucket: targetBucket,
277
+ };
278
+ await controller.transferData(transferOptions);
254
279
  }
255
- const transferOptions = {
256
- isRemote,
257
- fromDb: fromDb[0],
258
- targetDb: toDb,
259
- transferEndpoint: parsedArgv.remoteEndpoint,
260
- transferProject: parsedArgv.remoteProjectId,
261
- transferKey: parsedArgv.remoteApiKey,
262
- sourceBucket: sourceBucket,
263
- targetBucket: targetBucket,
264
- };
265
- await controller.transferData(transferOptions);
266
280
  }
267
281
  }
268
282
  }
@@ -842,6 +842,7 @@ export class DataLoader {
842
842
  }
843
843
  }
844
844
  // Update the attribute mappings with any actions that need to be performed post-import
845
+ // We added the basePath to get the folder from the filePath
845
846
  const mappingsWithActions = this.getAttributeMappingsWithActions(importDef.attributeMappings, context, transformedItem);
846
847
  // Update the import definition with the new attribute mappings
847
848
  const newImportDef = {
@@ -947,6 +948,7 @@ export class DataLoader {
947
948
  continue;
948
949
  }
949
950
  // Update the attribute mappings with any actions that need to be performed post-import
951
+ // We added the basePath to get the folder from the filePath
950
952
  const mappingsWithActions = this.getAttributeMappingsWithActions(importDef.attributeMappings, context, transformedData);
951
953
  // Update the import definition with the new attribute mappings
952
954
  const newImportDef = {
@@ -1068,6 +1070,7 @@ export class DataLoader {
1068
1070
  continue;
1069
1071
  }
1070
1072
  // Update the attribute mappings with any actions that need to be performed post-import
1073
+ // We added the basePath to get the folder from the filePath
1071
1074
  const mappingsWithActions = this.getAttributeMappingsWithActions(importDef.attributeMappings, context, transformedData);
1072
1075
  // Update the import definition with the new attribute mappings
1073
1076
  const newImportDef = {
@@ -1084,21 +1087,21 @@ export class DataLoader {
1084
1087
  ...newMapping,
1085
1088
  postImportActions: [
1086
1089
  ...(existingMapping?.postImportActions || []),
1087
- ...(newMapping.postImportActions || [])
1088
- ]
1090
+ ...(newMapping.postImportActions || []),
1091
+ ],
1089
1092
  };
1090
1093
  });
1091
1094
  itemDataToUpdate.importDef = {
1092
1095
  ...newImportDef,
1093
- attributeMappings: mergedAttributeMappings
1096
+ attributeMappings: mergedAttributeMappings,
1094
1097
  };
1095
1098
  // Debug logging
1096
- if (mergedAttributeMappings.some(m => m.postImportActions?.length > 0)) {
1099
+ if (mergedAttributeMappings.some((m) => m.postImportActions?.length > 0)) {
1097
1100
  logger.info(`Post-import actions for ${collection.name}: ${JSON.stringify(mergedAttributeMappings
1098
- .filter(m => m.postImportActions?.length > 0)
1099
- .map(m => ({
1101
+ .filter((m) => m.postImportActions?.length > 0)
1102
+ .map((m) => ({
1100
1103
  targetKey: m.targetKey,
1101
- actions: m.postImportActions
1104
+ actions: m.postImportActions,
1102
1105
  })), null, 2)}`);
1103
1106
  }
1104
1107
  }
@@ -1174,7 +1177,40 @@ export class DataLoader {
1174
1177
  let mappingFilePath = this.importDataActions.resolveTemplate(mapping.fileData.path, context, item);
1175
1178
  // Ensure the file path is absolute if it doesn't start with "http"
1176
1179
  if (!mappingFilePath.toLowerCase().startsWith("http")) {
1177
- mappingFilePath = path.resolve(this.appwriteFolderPath, mappingFilePath);
1180
+ // First try the direct path
1181
+ let fullPath = path.resolve(this.appwriteFolderPath, mappingFilePath);
1182
+ // If file doesn't exist, search in subdirectories
1183
+ if (!fs.existsSync(fullPath)) {
1184
+ const findFileInDir = (dir) => {
1185
+ const files = fs.readdirSync(dir);
1186
+ for (const file of files) {
1187
+ const filePath = path.join(dir, file);
1188
+ const stat = fs.statSync(filePath);
1189
+ if (stat.isDirectory()) {
1190
+ // Recursively search subdirectories
1191
+ const found = findFileInDir(filePath);
1192
+ if (found)
1193
+ return found;
1194
+ }
1195
+ else if (file === path.basename(mappingFilePath)) {
1196
+ return filePath;
1197
+ }
1198
+ }
1199
+ return null;
1200
+ };
1201
+ const foundPath = findFileInDir(this.appwriteFolderPath);
1202
+ if (foundPath) {
1203
+ mappingFilePath = foundPath;
1204
+ }
1205
+ else {
1206
+ logger.warn(`File not found in any subdirectory: ${mappingFilePath}`);
1207
+ // Keep the original resolved path as fallback
1208
+ mappingFilePath = fullPath;
1209
+ }
1210
+ }
1211
+ else {
1212
+ mappingFilePath = fullPath;
1213
+ }
1178
1214
  }
1179
1215
  // Define the after-import action to create a file and update the field
1180
1216
  const afterImportAction = {
@@ -68,13 +68,22 @@ export class ImportController {
68
68
  }
69
69
  }
70
70
  async updateOthersToFinalData(updatedDb, targetDb) {
71
- await transferDatabaseLocalToLocal(this.database, updatedDb.$id, targetDb.$id);
72
- // Find the corresponding database configs
73
- const updatedDbConfig = this.config.databases.find((db) => db.$id === updatedDb.$id);
74
- const targetDbConfig = this.config.databases.find((db) => db.$id === targetDb.$id);
75
- // Transfer database-specific bucket if both databases have a bucket defined
76
- if (updatedDbConfig?.bucket && targetDbConfig?.bucket) {
77
- await transferStorageLocalToLocal(this.storage, updatedDbConfig.bucket.$id, targetDbConfig.bucket.$id);
71
+ if (this.database) {
72
+ await transferDatabaseLocalToLocal(this.database, updatedDb.$id, targetDb.$id);
73
+ }
74
+ if (this.storage) {
75
+ // Find the corresponding database configs
76
+ const updatedDbConfig = this.config.databases.find((db) => db.$id === updatedDb.$id);
77
+ const targetDbConfig = this.config.databases.find((db) => db.$id === targetDb.$id);
78
+ const sourceBucketId = updatedDbConfig?.bucket?.$id ||
79
+ (this.config.documentBucketId &&
80
+ `${this.config.documentBucketId}_${updatedDb.$id.toLowerCase().trim().replace(" ", "")}`);
81
+ const targetBucketId = targetDbConfig?.bucket?.$id ||
82
+ (this.config.documentBucketId &&
83
+ `${this.config.documentBucketId}_${targetDb.$id.toLowerCase().trim().replace(" ", "")}`);
84
+ if (sourceBucketId && targetBucketId) {
85
+ await transferStorageLocalToLocal(this.storage, sourceBucketId, targetBucketId);
86
+ }
78
87
  }
79
88
  }
80
89
  async importCollections(db, dataLoader, specificCollections) {
@@ -1,7 +1,7 @@
1
1
  import { Databases, Storage, type Models } from "node-appwrite";
2
2
  export interface TransferOptions {
3
- fromDb: Models.Database;
4
- targetDb: Models.Database;
3
+ fromDb: Models.Database | undefined;
4
+ targetDb: Models.Database | undefined;
5
5
  isRemote: boolean;
6
6
  collections?: string[];
7
7
  transferEndpoint?: string;
@@ -96,6 +96,8 @@ export class UtilsController {
96
96
  await this.init();
97
97
  if (!this.database)
98
98
  throw new Error("Database not initialized");
99
+ if (ids.length === 0)
100
+ return [];
99
101
  const dbs = await this.database.list([
100
102
  Query.limit(500),
101
103
  Query.equal("$id", ids),
@@ -199,13 +201,11 @@ export class UtilsController {
199
201
  return this.appwriteFolderPath;
200
202
  }
201
203
  async transferData(options) {
202
- if (!this.database) {
203
- throw new Error("Database is not initialized, is the config file correct & created?");
204
- }
204
+ // Remove database requirement check
205
205
  let sourceClient = this.database;
206
206
  let targetClient;
207
- let sourceDatabases;
208
- let targetDatabases;
207
+ let sourceDatabases = [];
208
+ let targetDatabases = [];
209
209
  if (options.isRemote) {
210
210
  if (!options.transferEndpoint ||
211
211
  !options.transferProject ||
@@ -213,34 +213,47 @@ export class UtilsController {
213
213
  throw new Error("Remote transfer options are missing");
214
214
  }
215
215
  const remoteClient = getClient(options.transferEndpoint, options.transferProject, options.transferKey);
216
- targetClient = new Databases(remoteClient);
217
- sourceDatabases = await fetchAllDatabases(sourceClient);
218
- targetDatabases = await fetchAllDatabases(targetClient);
216
+ if (this.database) {
217
+ targetClient = new Databases(remoteClient);
218
+ sourceDatabases = await fetchAllDatabases(sourceClient);
219
+ targetDatabases = await fetchAllDatabases(targetClient);
220
+ }
219
221
  }
220
- else {
222
+ else if (this.database) {
221
223
  targetClient = sourceClient;
222
224
  sourceDatabases = targetDatabases = await fetchAllDatabases(sourceClient);
223
225
  }
224
- // Validate that the provided databases exist in the fetched lists
225
- const fromDb = sourceDatabases.find((db) => db.$id === options.fromDb.$id);
226
- const targetDb = targetDatabases.find((db) => db.$id === options.targetDb.$id);
227
- if (!fromDb || !targetDb) {
228
- throw new Error("Source or target database not found");
229
- }
230
- if (options.isRemote) {
231
- // Remote transfer
232
- await transferDatabaseLocalToRemote(sourceClient, options.transferEndpoint, options.transferProject, options.transferKey, fromDb.$id, targetDb.$id);
233
- if (this.storage && options.sourceBucket && options.targetBucket) {
234
- await transferStorageLocalToRemote(this.storage, options.transferEndpoint, options.transferProject, options.transferKey, options.sourceBucket.$id, options.targetBucket.$id);
226
+ // Only validate databases if they're provided in options
227
+ if (options.fromDb && options.targetDb) {
228
+ const fromDb = sourceDatabases.find((db) => db.$id === options.fromDb.$id);
229
+ const targetDb = targetDatabases.find((db) => db.$id === options.targetDb.$id);
230
+ if (!fromDb || !targetDb) {
231
+ throw new Error("Source or target database not found");
232
+ }
233
+ if (options.isRemote && targetClient) {
234
+ await transferDatabaseLocalToRemote(sourceClient, options.transferEndpoint, options.transferProject, options.transferKey, fromDb.$id, targetDb.$id);
235
+ }
236
+ else if (targetClient) {
237
+ await transferDatabaseLocalToLocal(sourceClient, fromDb.$id, targetDb.$id);
235
238
  }
236
239
  }
237
- else {
238
- // Local transfer
239
- await transferDatabaseLocalToLocal(sourceClient, fromDb.$id, targetDb.$id);
240
- if (this.storage && options.sourceBucket && options.targetBucket) {
241
- await transferStorageLocalToLocal(this.storage, options.sourceBucket.$id, options.targetBucket.$id);
240
+ // Handle storage transfer separately
241
+ if (this.storage && (options.sourceBucket || options.fromDb)) {
242
+ const sourceBucketId = options.sourceBucket?.$id ||
243
+ (options.fromDb && this.config?.documentBucketId &&
244
+ `${this.config.documentBucketId}_${options.fromDb.$id.toLowerCase().trim().replace(" ", "")}`);
245
+ const targetBucketId = options.targetBucket?.$id ||
246
+ (options.targetDb && this.config?.documentBucketId &&
247
+ `${this.config.documentBucketId}_${options.targetDb.$id.toLowerCase().trim().replace(" ", "")}`);
248
+ if (sourceBucketId && targetBucketId) {
249
+ if (options.isRemote) {
250
+ await transferStorageLocalToRemote(this.storage, options.transferEndpoint, options.transferProject, options.transferKey, sourceBucketId, targetBucketId);
251
+ }
252
+ else {
253
+ await transferStorageLocalToLocal(this.storage, sourceBucketId, targetBucketId);
254
+ }
242
255
  }
243
256
  }
244
- console.log("Data transfer completed");
257
+ console.log("Transfer completed");
245
258
  }
246
259
  }
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.9.983",
4
+ "version": "0.9.984",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
Binary file
package/src/main.ts CHANGED
@@ -271,7 +271,6 @@ async function main() {
271
271
  }
272
272
  }
273
273
 
274
-
275
274
  if (options.generateSchemas) {
276
275
  await controller.generateSchemas();
277
276
  }
@@ -281,65 +280,91 @@ async function main() {
281
280
  }
282
281
 
283
282
  if (parsedArgv.transfer) {
284
- const isRemote = !!parsedArgv.remoteEndpoint;
285
- const fromDb = await controller.getDatabasesByIds([parsedArgv.fromDbId!]);
286
- let toDb: Models.Database | undefined;
287
- let targetDatabases: Databases | undefined;
288
- let targetStorage: Storage | undefined;
283
+ if (parsedArgv.transfer) {
284
+ const isRemote = !!parsedArgv.remoteEndpoint;
285
+ let fromDb, toDb: Models.Database | undefined;
286
+ let targetDatabases: Databases | undefined;
287
+ let targetStorage: Storage | undefined;
288
+
289
+ // Only fetch databases if database IDs are provided
290
+ if (parsedArgv.fromDbId && parsedArgv.toDbId) {
291
+ fromDb = (
292
+ await controller.getDatabasesByIds([parsedArgv.fromDbId])
293
+ )[0];
294
+
295
+ if (isRemote) {
296
+ if (
297
+ !parsedArgv.remoteEndpoint ||
298
+ !parsedArgv.remoteProjectId ||
299
+ !parsedArgv.remoteApiKey
300
+ ) {
301
+ throw new Error("Remote transfer details are missing");
302
+ }
303
+ const remoteClient = getClient(
304
+ parsedArgv.remoteEndpoint,
305
+ parsedArgv.remoteProjectId,
306
+ parsedArgv.remoteApiKey
307
+ );
308
+ targetDatabases = new Databases(remoteClient);
309
+ targetStorage = new Storage(remoteClient);
310
+ const remoteDbs = await fetchAllDatabases(targetDatabases);
311
+ toDb = remoteDbs.find((db) => db.$id === parsedArgv.toDbId);
312
+ } else {
313
+ toDb = (await controller.getDatabasesByIds([parsedArgv.toDbId]))[0];
314
+ }
289
315
 
290
- if (isRemote) {
291
- if (
292
- !parsedArgv.remoteEndpoint ||
293
- !parsedArgv.remoteProjectId ||
294
- !parsedArgv.remoteApiKey
295
- ) {
296
- throw new Error("Remote transfer details are missing");
316
+ if (!fromDb || !toDb) {
317
+ throw new Error("Source or target database not found");
318
+ }
297
319
  }
298
- const remoteClient = getClient(
299
- parsedArgv.remoteEndpoint,
300
- parsedArgv.remoteProjectId,
301
- parsedArgv.remoteApiKey
302
- );
303
- targetDatabases = new Databases(remoteClient);
304
- targetStorage = new Storage(remoteClient);
305
- const remoteDbs = await fetchAllDatabases(targetDatabases);
306
- toDb = remoteDbs.find((db) => db.$id === parsedArgv.toDbId);
307
- } else {
308
- toDb = (await controller.getDatabasesByIds([parsedArgv.toDbId!]))[0];
309
- }
310
320
 
311
- if (!fromDb[0] || !toDb) {
312
- throw new Error("Source or target database not found");
313
- }
321
+ // Handle storage setup
322
+ let sourceBucket, targetBucket;
323
+ if (parsedArgv.fromBucketId) {
324
+ sourceBucket = await controller.storage?.getBucket(
325
+ parsedArgv.fromBucketId
326
+ );
327
+ }
328
+ if (parsedArgv.toBucketId) {
329
+ if (isRemote) {
330
+ if (!targetStorage) {
331
+ const remoteClient = getClient(
332
+ parsedArgv.remoteEndpoint!,
333
+ parsedArgv.remoteProjectId!,
334
+ parsedArgv.remoteApiKey!
335
+ );
336
+ targetStorage = new Storage(remoteClient);
337
+ }
338
+ targetBucket = await targetStorage?.getBucket(
339
+ parsedArgv.toBucketId
340
+ );
341
+ } else {
342
+ targetBucket = await controller.storage?.getBucket(
343
+ parsedArgv.toBucketId
344
+ );
345
+ }
346
+ }
314
347
 
315
- let sourceBucket, targetBucket;
316
- if (parsedArgv.fromBucketId) {
317
- sourceBucket = await controller.storage?.getBucket(
318
- parsedArgv.fromBucketId
319
- );
320
- }
321
- if (parsedArgv.toBucketId) {
322
- if (isRemote) {
323
- targetBucket = await targetStorage?.getBucket(parsedArgv.toBucketId);
324
- } else {
325
- targetBucket = await controller.storage?.getBucket(
326
- parsedArgv.toBucketId
348
+ // Validate that at least one transfer type is specified
349
+ if (!fromDb && !sourceBucket) {
350
+ throw new Error(
351
+ "No source database or bucket specified for transfer"
327
352
  );
328
353
  }
329
- }
330
354
 
331
- const transferOptions: TransferOptions = {
332
- isRemote,
333
- fromDb: fromDb[0],
334
- targetDb: toDb,
335
- transferEndpoint: parsedArgv.remoteEndpoint,
336
- transferProject: parsedArgv.remoteProjectId,
337
- transferKey: parsedArgv.remoteApiKey,
338
- sourceBucket: sourceBucket,
339
- targetBucket: targetBucket,
340
- };
355
+ const transferOptions: TransferOptions = {
356
+ isRemote,
357
+ fromDb,
358
+ targetDb: toDb,
359
+ transferEndpoint: parsedArgv.remoteEndpoint,
360
+ transferProject: parsedArgv.remoteProjectId,
361
+ transferKey: parsedArgv.remoteApiKey,
362
+ sourceBucket: sourceBucket,
363
+ targetBucket: targetBucket,
364
+ };
341
365
 
342
- await controller.transferData(transferOptions);
366
+ await controller.transferData(transferOptions);
367
+ }
343
368
  }
344
369
  }
345
370
  }
@@ -1086,6 +1086,7 @@ export class DataLoader {
1086
1086
  }
1087
1087
  }
1088
1088
  // Update the attribute mappings with any actions that need to be performed post-import
1089
+ // We added the basePath to get the folder from the filePath
1089
1090
  const mappingsWithActions = this.getAttributeMappingsWithActions(
1090
1091
  importDef.attributeMappings,
1091
1092
  context,
@@ -1247,6 +1248,7 @@ export class DataLoader {
1247
1248
  continue;
1248
1249
  }
1249
1250
  // Update the attribute mappings with any actions that need to be performed post-import
1251
+ // We added the basePath to get the folder from the filePath
1250
1252
  const mappingsWithActions = this.getAttributeMappingsWithActions(
1251
1253
  importDef.attributeMappings,
1252
1254
  context,
@@ -1462,6 +1464,7 @@ export class DataLoader {
1462
1464
  }
1463
1465
 
1464
1466
  // Update the attribute mappings with any actions that need to be performed post-import
1467
+ // We added the basePath to get the folder from the filePath
1465
1468
  const mappingsWithActions = this.getAttributeMappingsWithActions(
1466
1469
  importDef.attributeMappings,
1467
1470
  context,
@@ -1480,36 +1483,41 @@ export class DataLoader {
1480
1483
  transformedData
1481
1484
  );
1482
1485
  itemDataToUpdate.context = context;
1483
-
1486
+
1484
1487
  // Fix: Ensure we properly merge the attribute mappings and their actions
1485
- const mergedAttributeMappings = newImportDef.attributeMappings.map((newMapping) => {
1486
- const existingMapping = itemDataToUpdate.importDef?.attributeMappings.find(
1487
- (m) => m.targetKey === newMapping.targetKey
1488
- );
1489
-
1490
- return {
1491
- ...newMapping,
1492
- postImportActions: [
1493
- ...(existingMapping?.postImportActions || []),
1494
- ...(newMapping.postImportActions || [])
1495
- ]
1496
- };
1497
- });
1498
-
1488
+ const mergedAttributeMappings = newImportDef.attributeMappings.map(
1489
+ (newMapping) => {
1490
+ const existingMapping =
1491
+ itemDataToUpdate.importDef?.attributeMappings.find(
1492
+ (m) => m.targetKey === newMapping.targetKey
1493
+ );
1494
+
1495
+ return {
1496
+ ...newMapping,
1497
+ postImportActions: [
1498
+ ...(existingMapping?.postImportActions || []),
1499
+ ...(newMapping.postImportActions || []),
1500
+ ],
1501
+ };
1502
+ }
1503
+ );
1504
+
1499
1505
  itemDataToUpdate.importDef = {
1500
1506
  ...newImportDef,
1501
- attributeMappings: mergedAttributeMappings
1507
+ attributeMappings: mergedAttributeMappings,
1502
1508
  };
1503
-
1509
+
1504
1510
  // Debug logging
1505
- if (mergedAttributeMappings.some(m => m.postImportActions?.length > 0)) {
1511
+ if (
1512
+ mergedAttributeMappings.some((m) => m.postImportActions?.length > 0)
1513
+ ) {
1506
1514
  logger.info(
1507
1515
  `Post-import actions for ${collection.name}: ${JSON.stringify(
1508
1516
  mergedAttributeMappings
1509
- .filter(m => m.postImportActions?.length > 0)
1510
- .map(m => ({
1517
+ .filter((m) => m.postImportActions?.length > 0)
1518
+ .map((m) => ({
1511
1519
  targetKey: m.targetKey,
1512
- actions: m.postImportActions
1520
+ actions: m.postImportActions,
1513
1521
  })),
1514
1522
  null,
1515
1523
  2
@@ -1614,10 +1622,42 @@ export class DataLoader {
1614
1622
  );
1615
1623
  // Ensure the file path is absolute if it doesn't start with "http"
1616
1624
  if (!mappingFilePath.toLowerCase().startsWith("http")) {
1617
- mappingFilePath = path.resolve(
1618
- this.appwriteFolderPath,
1619
- mappingFilePath
1620
- );
1625
+ // First try the direct path
1626
+ let fullPath = path.resolve(this.appwriteFolderPath, mappingFilePath);
1627
+
1628
+ // If file doesn't exist, search in subdirectories
1629
+ if (!fs.existsSync(fullPath)) {
1630
+ const findFileInDir = (dir: string): string | null => {
1631
+ const files = fs.readdirSync(dir);
1632
+
1633
+ for (const file of files) {
1634
+ const filePath = path.join(dir, file);
1635
+ const stat = fs.statSync(filePath);
1636
+
1637
+ if (stat.isDirectory()) {
1638
+ // Recursively search subdirectories
1639
+ const found = findFileInDir(filePath);
1640
+ if (found) return found;
1641
+ } else if (file === path.basename(mappingFilePath)) {
1642
+ return filePath;
1643
+ }
1644
+ }
1645
+ return null;
1646
+ };
1647
+
1648
+ const foundPath = findFileInDir(this.appwriteFolderPath);
1649
+ if (foundPath) {
1650
+ mappingFilePath = foundPath;
1651
+ } else {
1652
+ logger.warn(
1653
+ `File not found in any subdirectory: ${mappingFilePath}`
1654
+ );
1655
+ // Keep the original resolved path as fallback
1656
+ mappingFilePath = fullPath;
1657
+ }
1658
+ } else {
1659
+ mappingFilePath = fullPath;
1660
+ }
1621
1661
  }
1622
1662
  // Define the after-import action to create a file and update the field
1623
1663
  const afterImportAction = {