appwrite-utils-cli 0.0.255 → 0.0.256
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.
@@ -231,8 +231,6 @@ export class DataLoader {
|
|
231
231
|
for (const collectionConfig of this.config.collections) {
|
232
232
|
const collectionKey = this.getCollectionKey(collectionConfig.name);
|
233
233
|
const collectionData = this.importMap.get(collectionKey);
|
234
|
-
let isUsersCollection = this.getCollectionKey(this.config.usersCollectionName) ===
|
235
|
-
this.getCollectionKey(collectionConfig.name);
|
236
234
|
if (!collectionData || !collectionData.data)
|
237
235
|
continue;
|
238
236
|
// Iterate over each data item in the current collection
|
@@ -244,58 +242,35 @@ export class DataLoader {
|
|
244
242
|
if (importDef.idMappings) {
|
245
243
|
// Iterate over each idMapping defined for the current import definition
|
246
244
|
for (const idMapping of importDef.idMappings) {
|
247
|
-
// Use the context for source and target field values
|
248
245
|
const oldId = item.context[idMapping.sourceField];
|
249
246
|
if (oldId) {
|
250
247
|
let newIdForOldId;
|
251
|
-
|
252
|
-
this.getCollectionKey(idMapping.targetCollection) ===
|
253
|
-
this.getCollectionKey(this.config.usersCollectionName)) {
|
254
|
-
for (const [newUserId, oldIds,] of this.mergedUserMap.entries()) {
|
255
|
-
// Check if the oldId is in the list of oldIds
|
256
|
-
// We only care if there's more than one cause all get one
|
257
|
-
if (oldIds.includes(`${oldId}`) && oldIds.length > 1) {
|
258
|
-
newIdForOldId = newUserId;
|
259
|
-
break;
|
260
|
-
}
|
261
|
-
}
|
262
|
-
}
|
248
|
+
// Your existing logic to resolve newIdForOldId here...
|
263
249
|
if (newIdForOldId) {
|
264
|
-
|
265
|
-
|
250
|
+
// Determine the field to update, handling arrays appropriately
|
251
|
+
const fieldToUpdate = idMapping.fieldToSet || idMapping.targetField;
|
252
|
+
if (Array.isArray(item.finalData[fieldToUpdate])) {
|
253
|
+
// If the target field is an array, append the new ID
|
254
|
+
item.finalData[fieldToUpdate].push(newIdForOldId);
|
266
255
|
}
|
267
256
|
else {
|
268
|
-
|
257
|
+
// Otherwise, directly set the new ID
|
258
|
+
item.finalData[fieldToUpdate] = newIdForOldId;
|
269
259
|
}
|
270
|
-
console.log(`
|
260
|
+
console.log(`Updated ${oldId} to ${newIdForOldId}`);
|
271
261
|
needsUpdate = true;
|
272
262
|
}
|
273
|
-
else {
|
274
|
-
// Find the target collection's oldIdToNewIdMap
|
275
|
-
const targetCollectionKey = this.getCollectionKey(idMapping.targetCollection);
|
276
|
-
const targetOldIdToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(targetCollectionKey);
|
277
|
-
if (targetOldIdToNewIdMap &&
|
278
|
-
targetOldIdToNewIdMap.has(`${oldId}`)) {
|
279
|
-
if (idMapping.fieldToSet) {
|
280
|
-
// Update the item's context with the new ID from the target collection
|
281
|
-
item.finalData[idMapping.fieldToSet] =
|
282
|
-
targetOldIdToNewIdMap.get(`${oldId}`);
|
283
|
-
}
|
284
|
-
else {
|
285
|
-
// Update the item's context with the new ID from the target collection
|
286
|
-
item.finalData[idMapping.targetField] =
|
287
|
-
targetOldIdToNewIdMap.get(`${oldId}`);
|
288
|
-
}
|
289
|
-
needsUpdate = true;
|
290
|
-
}
|
291
|
-
}
|
292
263
|
}
|
293
264
|
}
|
294
265
|
}
|
295
266
|
}
|
296
267
|
}
|
297
|
-
// If updates were made, set the updated item back in the importMap
|
298
268
|
if (needsUpdate) {
|
269
|
+
// Re-transform the item's finalData using its attribute mappings
|
270
|
+
const importDef = item.importDef; // Assuming importDef is available in the item
|
271
|
+
if (importDef && importDef.attributeMappings) {
|
272
|
+
item.finalData = await this.transformData(item.finalData, importDef.attributeMappings);
|
273
|
+
}
|
299
274
|
this.importMap.set(collectionKey, collectionData);
|
300
275
|
}
|
301
276
|
}
|
@@ -102,6 +102,12 @@ export class ImportController {
|
|
102
102
|
if (dataLoader.userExistsMap.has(userId)) {
|
103
103
|
// We only are storing the existing user ID's as true, so we need to check for that
|
104
104
|
if (!(dataLoader.userExistsMap.get(userId) === true)) {
|
105
|
+
const userId = userBatch.finalData.userId ||
|
106
|
+
userBatch.context.userId ||
|
107
|
+
userBatch.context.docId;
|
108
|
+
if (!userBatch.finalData.userId) {
|
109
|
+
userBatch.finalData.userId = userId;
|
110
|
+
}
|
105
111
|
return usersController
|
106
112
|
.createUserAndReturn(userBatch.finalData)
|
107
113
|
.then(() => console.log("Created user"))
|
@@ -143,9 +149,9 @@ export class ImportController {
|
|
143
149
|
const batches = dataSplit[i];
|
144
150
|
console.log(`Processing batch ${i + 1} of ${dataSplit.length}`);
|
145
151
|
const batchPromises = batches.map((item) => {
|
146
|
-
const id = item.
|
147
|
-
item.context.docId ||
|
152
|
+
const id = item.context.docId ||
|
148
153
|
item.context.userId ||
|
154
|
+
item.finalData.docId ||
|
149
155
|
item.finalData.userId;
|
150
156
|
return this.database
|
151
157
|
.createDocument(db.$id, collection.$id, id, item.finalData)
|
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.
|
4
|
+
"version": "0.0.256",
|
5
5
|
"main": "src/main.ts",
|
6
6
|
"type": "module",
|
7
7
|
"repository": {
|
@@ -301,9 +301,6 @@ export class DataLoader {
|
|
301
301
|
for (const collectionConfig of this.config.collections) {
|
302
302
|
const collectionKey = this.getCollectionKey(collectionConfig.name);
|
303
303
|
const collectionData = this.importMap.get(collectionKey);
|
304
|
-
let isUsersCollection =
|
305
|
-
this.getCollectionKey(this.config.usersCollectionName) ===
|
306
|
-
this.getCollectionKey(collectionConfig.name);
|
307
304
|
|
308
305
|
if (!collectionData || !collectionData.data) continue;
|
309
306
|
|
@@ -317,66 +314,24 @@ export class DataLoader {
|
|
317
314
|
if (importDef.idMappings) {
|
318
315
|
// Iterate over each idMapping defined for the current import definition
|
319
316
|
for (const idMapping of importDef.idMappings) {
|
320
|
-
// Use the context for source and target field values
|
321
317
|
const oldId = item.context[idMapping.sourceField];
|
322
318
|
if (oldId) {
|
323
319
|
let newIdForOldId: string | undefined;
|
324
|
-
|
325
|
-
|
326
|
-
this.getCollectionKey(idMapping.targetCollection) ===
|
327
|
-
this.getCollectionKey(this.config.usersCollectionName)
|
328
|
-
) {
|
329
|
-
for (const [
|
330
|
-
newUserId,
|
331
|
-
oldIds,
|
332
|
-
] of this.mergedUserMap.entries()) {
|
333
|
-
// Check if the oldId is in the list of oldIds
|
334
|
-
// We only care if there's more than one cause all get one
|
335
|
-
if (oldIds.includes(`${oldId}`) && oldIds.length > 1) {
|
336
|
-
newIdForOldId = newUserId;
|
337
|
-
break;
|
338
|
-
}
|
339
|
-
}
|
340
|
-
}
|
320
|
+
// Your existing logic to resolve newIdForOldId here...
|
321
|
+
|
341
322
|
if (newIdForOldId) {
|
342
|
-
|
343
|
-
|
323
|
+
// Determine the field to update, handling arrays appropriately
|
324
|
+
const fieldToUpdate =
|
325
|
+
idMapping.fieldToSet || idMapping.targetField;
|
326
|
+
if (Array.isArray(item.finalData[fieldToUpdate])) {
|
327
|
+
// If the target field is an array, append the new ID
|
328
|
+
item.finalData[fieldToUpdate].push(newIdForOldId);
|
344
329
|
} else {
|
345
|
-
|
330
|
+
// Otherwise, directly set the new ID
|
331
|
+
item.finalData[fieldToUpdate] = newIdForOldId;
|
346
332
|
}
|
347
|
-
console.log(
|
348
|
-
`MERGED USER ID UPDATE: Updated ${oldId} to ${
|
349
|
-
item.finalData[
|
350
|
-
idMapping.fieldToSet || idMapping.targetField
|
351
|
-
]
|
352
|
-
}`
|
353
|
-
);
|
333
|
+
console.log(`Updated ${oldId} to ${newIdForOldId}`);
|
354
334
|
needsUpdate = true;
|
355
|
-
} else {
|
356
|
-
// Find the target collection's oldIdToNewIdMap
|
357
|
-
const targetCollectionKey = this.getCollectionKey(
|
358
|
-
idMapping.targetCollection
|
359
|
-
);
|
360
|
-
const targetOldIdToNewIdMap =
|
361
|
-
this.oldIdToNewIdPerCollectionMap.get(
|
362
|
-
targetCollectionKey
|
363
|
-
);
|
364
|
-
|
365
|
-
if (
|
366
|
-
targetOldIdToNewIdMap &&
|
367
|
-
targetOldIdToNewIdMap.has(`${oldId}`)
|
368
|
-
) {
|
369
|
-
if (idMapping.fieldToSet) {
|
370
|
-
// Update the item's context with the new ID from the target collection
|
371
|
-
item.finalData[idMapping.fieldToSet] =
|
372
|
-
targetOldIdToNewIdMap.get(`${oldId}`);
|
373
|
-
} else {
|
374
|
-
// Update the item's context with the new ID from the target collection
|
375
|
-
item.finalData[idMapping.targetField] =
|
376
|
-
targetOldIdToNewIdMap.get(`${oldId}`);
|
377
|
-
}
|
378
|
-
needsUpdate = true;
|
379
|
-
}
|
380
335
|
}
|
381
336
|
}
|
382
337
|
}
|
@@ -384,8 +339,15 @@ export class DataLoader {
|
|
384
339
|
}
|
385
340
|
}
|
386
341
|
|
387
|
-
// If updates were made, set the updated item back in the importMap
|
388
342
|
if (needsUpdate) {
|
343
|
+
// Re-transform the item's finalData using its attribute mappings
|
344
|
+
const importDef = item.importDef; // Assuming importDef is available in the item
|
345
|
+
if (importDef && importDef.attributeMappings) {
|
346
|
+
item.finalData = await this.transformData(
|
347
|
+
item.finalData,
|
348
|
+
importDef.attributeMappings
|
349
|
+
);
|
350
|
+
}
|
389
351
|
this.importMap.set(collectionKey, collectionData);
|
390
352
|
}
|
391
353
|
}
|
@@ -162,6 +162,13 @@ export class ImportController {
|
|
162
162
|
if (dataLoader.userExistsMap.has(userId)) {
|
163
163
|
// We only are storing the existing user ID's as true, so we need to check for that
|
164
164
|
if (!(dataLoader.userExistsMap.get(userId) === true)) {
|
165
|
+
const userId =
|
166
|
+
userBatch.finalData.userId ||
|
167
|
+
userBatch.context.userId ||
|
168
|
+
userBatch.context.docId;
|
169
|
+
if (!userBatch.finalData.userId) {
|
170
|
+
userBatch.finalData.userId = userId;
|
171
|
+
}
|
165
172
|
return usersController
|
166
173
|
.createUserAndReturn(userBatch.finalData)
|
167
174
|
.then(() => console.log("Created user"))
|
@@ -219,9 +226,9 @@ export class ImportController {
|
|
219
226
|
console.log(`Processing batch ${i + 1} of ${dataSplit.length}`);
|
220
227
|
const batchPromises = batches.map((item) => {
|
221
228
|
const id =
|
222
|
-
item.finalData.docId ||
|
223
229
|
item.context.docId ||
|
224
230
|
item.context.userId ||
|
231
|
+
item.finalData.docId ||
|
225
232
|
item.finalData.userId;
|
226
233
|
return this.database
|
227
234
|
.createDocument(db.$id, collection.$id, id, item.finalData)
|