@aneuhold/be-ts-db-lib 3.0.2 → 3.0.3

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.
@@ -80,374 +80,387 @@ export default class MigrationService {
80
80
  `Found ${legacyDocs.length} documents with legacy ObjectId IDs and ${newDocs.length} documents with string IDs.`
81
81
  );
82
82
 
83
- // Delete all existing new docs (with string IDs) to ensure fresh creation
84
- if (!dryRun && newDocs.length > 0) {
85
- DR.logger.info(`Deleting ${newDocs.length} existing migrated documents...`);
86
- const newDocIds = newDocs.map((doc) => doc._id);
87
-
88
- const userIdsToDelete = users.filter((u) => typeof u._id === 'string').map((u) => u._id);
89
- const apiKeyIdsToDelete = apiKeys.filter((k) => typeof k._id === 'string').map((k) => k._id);
90
- const taskIdsToDelete = tasks.filter((t) => typeof t._id === 'string').map((t) => t._id);
91
- const configIdsToDelete = configs.filter((c) => typeof c._id === 'string').map((c) => c._id);
92
- const nonogramItemIdsToDelete = nonogramItems.filter((i) => typeof i._id === 'string').map((i) => i._id);
93
- const nonogramUpgradeIdsToDelete = nonogramUpgrades.filter((u) => typeof u._id === 'string').map((u) => u._id);
94
-
95
- if (userIdsToDelete.length > 0) {
96
- const result = await (await userRepo.getCollection()).deleteMany({ _id: { $in: userIdsToDelete } });
97
- DR.logger.info(`Deleted ${result.deletedCount} users.`);
98
- }
99
- if (apiKeyIdsToDelete.length > 0) {
100
- const result = await (await apiKeyRepo.getCollection()).deleteMany({ _id: { $in: apiKeyIdsToDelete } });
101
- DR.logger.info(`Deleted ${result.deletedCount} API keys.`);
102
- }
103
- if (taskIdsToDelete.length > 0) {
104
- const result = await (await taskRepo.getCollection()).deleteMany({ _id: { $in: taskIdsToDelete } });
105
- DR.logger.info(`Deleted ${result.deletedCount} tasks.`);
106
- }
107
- if (configIdsToDelete.length > 0) {
108
- const result = await (await configRepo.getCollection()).deleteMany({ _id: { $in: configIdsToDelete } });
109
- DR.logger.info(`Deleted ${result.deletedCount} configs.`);
110
- }
111
- if (nonogramItemIdsToDelete.length > 0) {
112
- const result = await (await nonogramItemRepo.getCollection()).deleteMany({ _id: { $in: nonogramItemIdsToDelete } });
113
- DR.logger.info(`Deleted ${result.deletedCount} nonogram items.`);
114
- }
115
- if (nonogramUpgradeIdsToDelete.length > 0) {
116
- const result = await (await nonogramUpgradesRepo.getCollection()).deleteMany({ _id: { $in: nonogramUpgradeIdsToDelete } });
117
- DR.logger.info(`Deleted ${result.deletedCount} nonogram upgrades.`);
83
+ // Delete all existing legacy docs (with object IDs)
84
+ if (legacyDocs.length > 0) {
85
+ const userIdsToDelete = users.filter((u) => typeof u._id === 'object').map((u) => u._id);
86
+ const apiKeyIdsToDelete = apiKeys.filter((k) => typeof k._id === 'object').map((k) => k._id);
87
+ const taskIdsToDelete = tasks.filter((t) => typeof t._id === 'object').map((t) => t._id);
88
+ const configIdsToDelete = configs.filter((c) => typeof c._id === 'object').map((c) => c._id);
89
+ const nonogramItemIdsToDelete = nonogramItems
90
+ .filter((i) => typeof i._id === 'object')
91
+ .map((i) => i._id);
92
+ const nonogramUpgradeIdsToDelete = nonogramUpgrades
93
+ .filter((u) => typeof u._id === 'object')
94
+ .map((u) => u._id);
95
+
96
+ if (!dryRun) {
97
+ DR.logger.info(`Deleting ${legacyDocs.length} legacy documents...`);
98
+
99
+ if (userIdsToDelete.length > 0) {
100
+ const result = await (
101
+ await userRepo.getCollection()
102
+ ).deleteMany({ _id: { $in: userIdsToDelete } });
103
+ DR.logger.info(`Deleted ${result.deletedCount} users.`);
104
+ }
105
+ if (apiKeyIdsToDelete.length > 0) {
106
+ const result = await (
107
+ await apiKeyRepo.getCollection()
108
+ ).deleteMany({ _id: { $in: apiKeyIdsToDelete } });
109
+ DR.logger.info(`Deleted ${result.deletedCount} API keys.`);
110
+ }
111
+ if (taskIdsToDelete.length > 0) {
112
+ const result = await (
113
+ await taskRepo.getCollection()
114
+ ).deleteMany({ _id: { $in: taskIdsToDelete } });
115
+ DR.logger.info(`Deleted ${result.deletedCount} tasks.`);
116
+ }
117
+ if (configIdsToDelete.length > 0) {
118
+ const result = await (
119
+ await configRepo.getCollection()
120
+ ).deleteMany({ _id: { $in: configIdsToDelete } });
121
+ DR.logger.info(`Deleted ${result.deletedCount} configs.`);
122
+ }
123
+ if (nonogramItemIdsToDelete.length > 0) {
124
+ const result = await (
125
+ await nonogramItemRepo.getCollection()
126
+ ).deleteMany({ _id: { $in: nonogramItemIdsToDelete } });
127
+ DR.logger.info(`Deleted ${result.deletedCount} nonogram items.`);
128
+ }
129
+ if (nonogramUpgradeIdsToDelete.length > 0) {
130
+ const result = await (
131
+ await nonogramUpgradesRepo.getCollection()
132
+ ).deleteMany({ _id: { $in: nonogramUpgradeIdsToDelete } });
133
+ DR.logger.info(`Deleted ${result.deletedCount} nonogram upgrades.`);
134
+ }
135
+
136
+ DR.logger.info('Deletion complete.');
137
+ } else {
138
+ DR.logger.info(`Dry run: Would delete ${legacyDocs.length} legacy documents.`);
139
+ if (userIdsToDelete.length > 0) DR.logger.info(` - ${userIdsToDelete.length} users`);
140
+ if (apiKeyIdsToDelete.length > 0)
141
+ DR.logger.info(` - ${apiKeyIdsToDelete.length} API keys`);
142
+ if (taskIdsToDelete.length > 0) DR.logger.info(` - ${taskIdsToDelete.length} tasks`);
143
+ if (configIdsToDelete.length > 0) DR.logger.info(` - ${configIdsToDelete.length} configs`);
144
+ if (nonogramItemIdsToDelete.length > 0)
145
+ DR.logger.info(` - ${nonogramItemIdsToDelete.length} nonogram items`);
146
+ if (nonogramUpgradeIdsToDelete.length > 0)
147
+ DR.logger.info(` - ${nonogramUpgradeIdsToDelete.length} nonogram upgrades`);
118
148
  }
119
-
120
- DR.logger.info('Deletion complete. Starting fresh migration...');
121
- } else if (dryRun && newDocs.length > 0) {
122
- DR.logger.info(`Dry run: Would delete ${newDocs.length} existing migrated documents.`);
123
149
  }
150
+ // We started with 1204 docs
151
+ }
152
+ }
124
153
 
125
- // Now create the actual map
126
- const mapFromObjectIdToUUID = new Map<string, UUID>();
127
-
128
- const newUsersToCreate: User[] = [];
129
- const newApiKeysToCreate: ApiKey[] = [];
130
- const newTasksToCreate: DashboardTask[] = [];
131
- const newConfigsToCreate: DashboardUserConfig[] = [];
132
- const newNonogramItemsToCreate: NonogramKatanaItem[] = [];
133
- const newNonogramUpgradesToCreate: NonogramKatanaUpgrade[] = [];
134
-
135
- /**
136
- * Gets a UUID for a given legacy ObjectId. If one already exists in the map,
137
- * it is returned. Otherwise, a new one is generated and added to the map.
138
- */
139
- const getUUID = (oldId: ObjectId): UUID => {
140
- const oldIdStr = oldId.toString();
141
- if (mapFromObjectIdToUUID.has(oldIdStr)) {
142
- return mapFromObjectIdToUUID.get(oldIdStr);
143
- }
144
- const newId = uuidv7();
145
- mapFromObjectIdToUUID.set(oldIdStr, newId);
146
- return newId;
147
- };
154
+ async function Migration2025UUID() {
155
+ // Now create the actual map
156
+ const mapFromObjectIdToUUID = new Map<string, UUID>();
148
157
 
149
- // Create a map of UUID to documents
150
- const mapFromUUIDToDocument = new Map<UUID, any>();
151
-
152
- const verifyDoc = (oldDoc: any, newDoc: any) => {
153
- const oldJson = JSON.stringify(oldDoc, null, 2);
154
- const newJson = JSON.stringify(newDoc, null, 2);
155
- const oldLines = oldJson.split('\n').length;
156
- const newLines = newJson.split('\n').length;
157
- if (newLines !== oldLines + 1) {
158
- DR.logger.warn(
159
- `Line count mismatch for ${oldDoc.docType || 'doc'} ${oldDoc._id}: Old=${oldLines}, New=${newLines}.`
160
- );
161
- }
162
- };
163
-
164
- function createNewDoc(oldDoc) {
165
- if (mapFromObjectIdToUUID.has(oldDoc._id.toString()) && mapFromUUIDToDocument.has(mapFromObjectIdToUUID.get(oldDoc._id.toString()))) {
166
- console.warn(
167
- `Skipping document ${oldDoc._id} as it was already migrated.` +
158
+ const newUsersToCreate: User[] = [];
159
+ const newApiKeysToCreate: ApiKey[] = [];
160
+ const newTasksToCreate: DashboardTask[] = [];
161
+ const newConfigsToCreate: DashboardUserConfig[] = [];
162
+ const newNonogramItemsToCreate: NonogramKatanaItem[] = [];
163
+ const newNonogramUpgradesToCreate: NonogramKatanaUpgrade[] = [];
164
+
165
+ /**
166
+ * Gets a UUID for a given legacy ObjectId. If one already exists in the map,
167
+ * it is returned. Otherwise, a new one is generated and added to the map.
168
+ */
169
+ const getUUID = (oldId: ObjectId): UUID => {
170
+ const oldIdStr = oldId.toString();
171
+ if (mapFromObjectIdToUUID.has(oldIdStr)) {
172
+ return mapFromObjectIdToUUID.get(oldIdStr);
173
+ }
174
+ const newId = uuidv7();
175
+ mapFromObjectIdToUUID.set(oldIdStr, newId);
176
+ return newId;
177
+ };
178
+
179
+ // Create a map of UUID to documents
180
+ const mapFromUUIDToDocument = new Map<UUID, any>();
181
+
182
+ const verifyDoc = (oldDoc: any, newDoc: any) => {
183
+ const oldJson = JSON.stringify(oldDoc, null, 2);
184
+ const newJson = JSON.stringify(newDoc, null, 2);
185
+ const oldLines = oldJson.split('\n').length;
186
+ const newLines = newJson.split('\n').length;
187
+ if (newLines !== oldLines + 1) {
188
+ DR.logger.warn(
189
+ `Line count mismatch for ${oldDoc.docType || 'doc'} ${oldDoc._id}: Old=${oldLines}, New=${newLines}.`
190
+ );
191
+ }
192
+ };
193
+
194
+ function createNewDoc(oldDoc) {
195
+ if (
196
+ mapFromObjectIdToUUID.has(oldDoc._id.toString()) &&
197
+ mapFromUUIDToDocument.has(mapFromObjectIdToUUID.get(oldDoc._id.toString()))
198
+ ) {
199
+ console.warn(
200
+ `Skipping document ${oldDoc._id} as it was already migrated.` +
168
201
  ` The created document is ${JSON.stringify(mapFromUUIDToDocument.get(mapFromObjectIdToUUID.get(oldDoc._id.toString())))}`
169
- );
170
- return;
171
- }
172
- const newDocId = getUUID(oldDoc._id);
173
- const newDoc = DocumentService.deepCopy(oldDoc);
174
- newDoc._id = newDocId;
175
- newDoc.oldOId = oldDoc._id.toString();
176
- mapFromUUIDToDocument.set(newDocId, newDoc);
177
- return newDoc;
202
+ );
203
+ return;
178
204
  }
205
+ const newDocId = getUUID(oldDoc._id);
206
+ const newDoc = DocumentService.deepCopy(oldDoc);
207
+ newDoc._id = newDocId;
208
+ newDoc.oldOId = oldDoc._id.toString();
209
+ mapFromUUIDToDocument.set(newDocId, newDoc);
210
+ return newDoc;
211
+ }
179
212
 
180
- // Filter to only legacy users with the chosen usernames
181
- const userNames = ['demoUser1', 'demoUser2', "testUser", "testUser2"];
182
- // const userNames = ["testUser"];
183
- const legacyUsers = users.filter((u) => userNames.includes(u.userName) && typeof u._id === 'object');
184
- DR.logger.info(`Found ${legacyUsers.length} legacy users to migrate.`);
185
-
186
- // First, create all user documents and generate their UUIDs that way relationships between
187
- // users in the collaborators field can be mapped correctly.
188
- legacyUsers.forEach((oldUserDoc) => {
189
- const newUser = createNewDoc(oldUserDoc);
190
- if (!newUser) return;
191
- verifyDoc(oldUserDoc, newUser);
192
- newUsersToCreate.push(newUser);
213
+ // Filter to only legacy users with the chosen usernames
214
+ // const userNames = ['demoUser1', 'demoUser2', "testUser", "testUser2"];
215
+ // const userNames = ["testUser"];
216
+ // const legacyUsers = users.filter((u) => userNames.includes(u.userName) && typeof u._id === 'object');
217
+ const legacyUsers = users.filter((u) => typeof u._id === 'object');
218
+ DR.logger.info(`Found ${legacyUsers.length} legacy users to migrate.`);
219
+
220
+ // First, create all user documents and generate their UUIDs that way relationships between
221
+ // users in the collaborators field can be mapped correctly.
222
+ legacyUsers.forEach((oldUserDoc) => {
223
+ const newUser = createNewDoc(oldUserDoc);
224
+ if (!newUser) return;
225
+ verifyDoc(oldUserDoc, newUser);
226
+ newUsersToCreate.push(newUser);
227
+ });
228
+
229
+ // Create the function that will flesh out new documents for a given user document
230
+ function createNewDocsForUser(oldUserDoc: User) {
231
+ const oldUserIdStr = oldUserDoc._id.toString();
232
+ const newUserId = getUUID(oldUserDoc._id);
233
+
234
+ // 1. Create new API keys
235
+ const userApiKeys = apiKeys.filter((k) => k.userId.toString() === oldUserIdStr);
236
+ userApiKeys.forEach((oldKey) => {
237
+ const newKey = createNewDoc(oldKey);
238
+ if (!newKey) return;
239
+ newKey.userId = newUserId;
240
+ verifyDoc(oldKey, newKey);
241
+ newApiKeysToCreate.push(newKey);
193
242
  });
194
243
 
195
- // Create the function that will flesh out new documents for a given user document
196
- function createNewDocsForUser(oldUserDoc: User) {
197
- const oldUserIdStr = oldUserDoc._id.toString();
198
- const newUserId = getUUID(oldUserDoc._id);
199
-
200
- // 1. Create new API keys
201
- const userApiKeys = apiKeys.filter((k) => k.userId.toString() === oldUserIdStr);
202
- userApiKeys.forEach((oldKey) => {
203
- const newKey = createNewDoc(oldKey);
204
- if (!newKey) return;
205
- newKey.userId = newUserId;
206
- verifyDoc(oldKey, newKey);
207
- newApiKeysToCreate.push(newKey);
208
- });
244
+ // 2. Create new Tasks
245
+ const userTasks = tasks.filter((t) => t.userId.toString() === oldUserIdStr);
209
246
 
210
- // 2. Create new Tasks
211
- const userTasks = tasks.filter(
212
- (t) => t.userId.toString() === oldUserIdStr
213
- );
247
+ const taskChildrenMap = new Map<string, DashboardTask[]>();
248
+ const rootTasks: DashboardTask[] = [];
249
+ const userTaskIds = new Set(userTasks.map((t) => t._id.toString()));
214
250
 
215
- const taskChildrenMap = new Map<string, DashboardTask[]>();
216
- const rootTasks: DashboardTask[] = [];
217
- const userTaskIds = new Set(userTasks.map((t) => t._id.toString()));
251
+ userTasks.forEach((task) => {
252
+ const pId = task.parentTaskId?.toString();
253
+ if (pId && userTaskIds.has(pId)) {
254
+ if (!taskChildrenMap.has(pId)) {
255
+ taskChildrenMap.set(pId, []);
256
+ }
257
+ taskChildrenMap.get(pId).push(task);
258
+ } else {
259
+ rootTasks.push(task);
260
+ }
261
+ });
218
262
 
219
- userTasks.forEach((task) => {
220
- const pId = task.parentTaskId?.toString();
221
- if (pId && userTaskIds.has(pId)) {
222
- if (!taskChildrenMap.has(pId)) {
223
- taskChildrenMap.set(pId, []);
224
- }
225
- taskChildrenMap.get(pId).push(task);
226
- } else {
227
- rootTasks.push(task);
263
+ const queue = [...rootTasks];
264
+ while (queue.length > 0) {
265
+ const oldTask = queue.shift()!;
266
+ const newTask = createNewDoc(oldTask);
267
+
268
+ if (newTask) {
269
+ newTask.userId = newUserId;
270
+
271
+ // Map user IDs in tags keys
272
+ const newTags: any = {};
273
+ Object.keys(oldTask.tags).forEach((tagUserId) => {
274
+ const newTagUserId = getUUID(tagUserId);
275
+ newTags[newTagUserId] = oldTask.tags[tagUserId];
276
+ });
277
+ newTask.tags = newTags;
278
+
279
+ // Map user IDs in filterSettings keys
280
+ const newFilterSettings: any = {};
281
+ Object.keys(oldTask.filterSettings).forEach((fsUserId) => {
282
+ const newFsUserId = getUUID(fsUserId);
283
+ newFilterSettings[newFsUserId] = {
284
+ ...oldTask.filterSettings[fsUserId],
285
+ userId: newFsUserId
286
+ };
287
+ });
288
+ newTask.filterSettings = newFilterSettings;
289
+
290
+ // Map user IDs in sortSettings keys
291
+ const newSortSettings: any = {};
292
+ Object.keys(oldTask.sortSettings).forEach((ssUserId) => {
293
+ const newSsUserId = getUUID(ssUserId);
294
+ newSortSettings[newSsUserId] = {
295
+ ...oldTask.sortSettings[ssUserId],
296
+ userId: newSsUserId
297
+ };
298
+ });
299
+ newTask.sortSettings = newSortSettings;
300
+
301
+ newTask.sharedWith = oldTask.sharedWith.map((id: any) => getUUID(id));
302
+ if (oldTask.assignedTo) {
303
+ newTask.assignedTo = getUUID(oldTask.assignedTo);
304
+ }
305
+ if (oldTask.parentTaskId) {
306
+ newTask.parentTaskId = getUUID(oldTask.parentTaskId);
307
+ }
308
+ if (oldTask.parentRecurringTaskInfo) {
309
+ newTask.parentRecurringTaskInfo = {
310
+ ...oldTask.parentRecurringTaskInfo,
311
+ taskId: getUUID(oldTask.parentRecurringTaskInfo.taskId)
312
+ };
228
313
  }
229
- });
230
314
 
231
- const queue = [...rootTasks];
232
- while (queue.length > 0) {
233
- const oldTask = queue.shift()!;
234
- const newTask = createNewDoc(oldTask);
235
-
236
- if (newTask) {
237
- newTask.userId = newUserId;
238
-
239
- // Map user IDs in tags keys
240
- const newTags: any = {};
241
- Object.keys(oldTask.tags).forEach((tagUserId) => {
242
- const newTagUserId = getUUID(tagUserId);
243
- newTags[newTagUserId] = oldTask.tags[tagUserId];
244
- });
245
- newTask.tags = newTags;
246
-
247
- // Map user IDs in filterSettings keys
248
- const newFilterSettings: any = {};
249
- Object.keys(oldTask.filterSettings).forEach((fsUserId) => {
250
- const newFsUserId = getUUID(fsUserId);
251
- newFilterSettings[newFsUserId] = {
252
- ...oldTask.filterSettings[fsUserId],
253
- userId: newFsUserId
254
- };
255
- });
256
- newTask.filterSettings = newFilterSettings;
257
-
258
- // Map user IDs in sortSettings keys
259
- const newSortSettings: any = {};
260
- Object.keys(oldTask.sortSettings).forEach((ssUserId) => {
261
- const newSsUserId = getUUID(ssUserId);
262
- newSortSettings[newSsUserId] = {
263
- ...oldTask.sortSettings[ssUserId],
264
- userId: newSsUserId
265
- };
266
- });
267
- newTask.sortSettings = newSortSettings;
315
+ verifyDoc(oldTask, newTask);
316
+ newTasksToCreate.push(newTask);
317
+ }
268
318
 
269
- newTask.sharedWith = oldTask.sharedWith.map((id: any) => getUUID(id));
270
- if (oldTask.assignedTo) {
271
- newTask.assignedTo = getUUID(oldTask.assignedTo);
272
- }
273
- if (oldTask.parentTaskId) {
274
- newTask.parentTaskId = getUUID(oldTask.parentTaskId);
275
- }
276
- if (oldTask.parentRecurringTaskInfo) {
277
- newTask.parentRecurringTaskInfo = {
278
- ...oldTask.parentRecurringTaskInfo,
279
- taskId: getUUID(oldTask.parentRecurringTaskInfo.taskId)
319
+ const children = taskChildrenMap.get(oldTask._id.toString());
320
+ if (children) {
321
+ queue.push(...children);
322
+ }
323
+ }
324
+
325
+ // 3. Create new Configs
326
+ const userConfigs = configs.filter((c) => c.userId.toString() === oldUserIdStr);
327
+ userConfigs.forEach((oldConfig) => {
328
+ const newConfig = createNewDoc(oldConfig);
329
+ if (!newConfig) return;
330
+ newConfig.userId = newUserId;
331
+ newConfig.collaborators = (oldConfig.collaborators || []).map((id) => getUUID(id));
332
+ if (newConfig.taskListSortSettings) {
333
+ Object.entries(newConfig.taskListSortSettings).forEach(
334
+ ([category, settings]: [string, any]) => {
335
+ const newSettings = {
336
+ userId: getUUID(settings.userId),
337
+ sortList: settings.sortList
280
338
  };
339
+ newConfig.taskListSortSettings[category] = newSettings;
281
340
  }
282
-
283
- verifyDoc(oldTask, newTask);
284
- newTasksToCreate.push(newTask);
285
- }
286
-
287
- const children = taskChildrenMap.get(oldTask._id.toString());
288
- if (children) {
289
- queue.push(...children);
290
- }
341
+ );
291
342
  }
292
-
293
- // 3. Create new Configs
294
- const userConfigs = configs.filter((c) => c.userId.toString() === oldUserIdStr);
295
- userConfigs.forEach((oldConfig) => {
296
- const newConfig = createNewDoc(oldConfig);
297
- if (!newConfig) return;
298
- newConfig.userId = newUserId;
299
- newConfig.collaborators = (oldConfig.collaborators || []).map((id) =>
300
- getUUID(id)
343
+ if (newConfig.taskListFilterSettings) {
344
+ Object.entries(newConfig.taskListFilterSettings).forEach(
345
+ ([category, settings]: [string, any]) => {
346
+ const newSettings = {
347
+ ...settings,
348
+ userId: getUUID(settings.userId)
349
+ };
350
+ newConfig.taskListFilterSettings[category] = newSettings;
351
+ }
301
352
  );
302
- if (newConfig.taskListSortSettings) {
303
- Object.entries(newConfig.taskListSortSettings).forEach(
304
- ([category, settings]: [string, any]) => {
305
- const newSettings = {
306
- userId: getUUID(settings.userId),
307
- sortList: settings.sortList
308
- };
309
- newConfig.taskListSortSettings[category] = newSettings;
310
- }
311
- );
312
- }
313
- if (newConfig.taskListFilterSettings) {
314
- Object.entries(newConfig.taskListFilterSettings).forEach(
315
- ([category, settings]: [string, any]) => {
316
- const newSettings = {
317
- ...settings,
318
- userId: getUUID(settings.userId)
319
- };
320
- newConfig.taskListFilterSettings[category] = newSettings;
321
- }
322
- );
323
- }
324
- verifyDoc(oldConfig, newConfig);
325
- newConfigsToCreate.push(newConfig);
326
- });
327
-
328
- // 4. Nonogram Items
329
- const userNonogramItems = nonogramItems.filter((i) => i.userId.toString() === oldUserIdStr);
330
- userNonogramItems.forEach((oldItem) => {
331
- const newItem = createNewDoc(oldItem);
332
- if (!newItem) return;
333
- newItem.userId = newUserId;
334
- verifyDoc(oldItem, newItem);
335
- newNonogramItemsToCreate.push(newItem);
336
- });
353
+ }
354
+ verifyDoc(oldConfig, newConfig);
355
+ newConfigsToCreate.push(newConfig);
356
+ });
337
357
 
338
- // 5. Nonogram Upgrades
339
- const userNonogramUpgrades = nonogramUpgrades.filter((u) => u.userId.toString() === oldUserIdStr);
340
- userNonogramUpgrades.forEach((oldUpgrade) => {
341
- const newUpgrade = createNewDoc(oldUpgrade);
342
- if (!newUpgrade) return;
343
- newUpgrade.userId = newUserId;
344
- verifyDoc(oldUpgrade, newUpgrade);
345
- newNonogramUpgradesToCreate.push(newUpgrade);
346
- });
347
- }
358
+ // 4. Nonogram Items
359
+ const userNonogramItems = nonogramItems.filter((i) => i.userId.toString() === oldUserIdStr);
360
+ userNonogramItems.forEach((oldItem) => {
361
+ const newItem = createNewDoc(oldItem);
362
+ if (!newItem) return;
363
+ newItem.userId = newUserId;
364
+ verifyDoc(oldItem, newItem);
365
+ newNonogramItemsToCreate.push(newItem);
366
+ });
348
367
 
349
- // Now create related documents for each user
350
- legacyUsers.forEach((u) => createNewDocsForUser(u));
351
-
352
- const legacyUserIds = new Set(legacyUsers.map((u) => u._id.toString()));
353
- const expectedUsers = legacyUsers.length;
354
- const expectedApiKeys = apiKeys.filter((k) =>
355
- legacyUserIds.has(k.userId.toString())
356
- ).length;
357
- const expectedTasks = tasks.filter((t) =>
358
- legacyUserIds.has(t.userId.toString())
359
- ).length;
360
- const expectedConfigs = configs.filter((c) =>
361
- legacyUserIds.has(c.userId.toString())
362
- ).length;
363
- const expectedNonogramItems = nonogramItems.filter((i) =>
364
- legacyUserIds.has(i.userId.toString())
365
- ).length;
366
- const expectedNonogramUpgrades = nonogramUpgrades.filter((u) =>
367
- legacyUserIds.has(u.userId.toString())
368
- ).length;
369
-
370
- if (newUsersToCreate.length !== expectedUsers) {
371
- DR.logger.error(
372
- `Expected ${expectedUsers} users, but prepped ${newUsersToCreate.length}.`
373
- );
374
- }
375
- if (newApiKeysToCreate.length !== expectedApiKeys) {
376
- DR.logger.error(
377
- `Expected ${expectedApiKeys} API keys, but prepped ${newApiKeysToCreate.length}.`
378
- );
379
- }
380
- if (newTasksToCreate.length !== expectedTasks) {
381
- DR.logger.error(
382
- `Expected ${expectedTasks} tasks, but prepped ${newTasksToCreate.length}.`
383
- );
384
- }
385
- if (newConfigsToCreate.length !== expectedConfigs) {
386
- DR.logger.error(
387
- `Expected ${expectedConfigs} configs, but prepped ${newConfigsToCreate.length}.`
388
- );
389
- }
390
- if (newNonogramItemsToCreate.length !== expectedNonogramItems) {
391
- DR.logger.error(
392
- `Expected ${expectedNonogramItems} nonogram items, but prepped ${newNonogramItemsToCreate.length}.`
393
- );
394
- }
395
- if (newNonogramUpgradesToCreate.length !== expectedNonogramUpgrades) {
396
- DR.logger.error(
397
- `Expected ${expectedNonogramUpgrades} nonogram upgrades, but prepped ${newNonogramUpgradesToCreate.length}.`
398
- );
399
- }
368
+ // 5. Nonogram Upgrades
369
+ const userNonogramUpgrades = nonogramUpgrades.filter(
370
+ (u) => u.userId.toString() === oldUserIdStr
371
+ );
372
+ userNonogramUpgrades.forEach((oldUpgrade) => {
373
+ const newUpgrade = createNewDoc(oldUpgrade);
374
+ if (!newUpgrade) return;
375
+ newUpgrade.userId = newUserId;
376
+ verifyDoc(oldUpgrade, newUpgrade);
377
+ newNonogramUpgradesToCreate.push(newUpgrade);
378
+ });
379
+ }
400
380
 
401
- DR.logger.info(`Prepped ${newUsersToCreate.length} new users.`);
402
- DR.logger.info(`Prepped ${newApiKeysToCreate.length} new API keys.`);
403
- DR.logger.info(`Prepped ${newTasksToCreate.length} new tasks.`);
404
- DR.logger.info(`Prepped ${newConfigsToCreate.length} new configs.`);
405
- DR.logger.info(
406
- `Prepped ${newNonogramItemsToCreate.length} new nonogram items.`
381
+ // Now create related documents for each user
382
+ legacyUsers.forEach((u) => createNewDocsForUser(u));
383
+
384
+ const legacyUserIds = new Set(legacyUsers.map((u) => u._id.toString()));
385
+ const expectedUsers = legacyUsers.length;
386
+ const expectedApiKeys = apiKeys.filter((k) => legacyUserIds.has(k.userId.toString())).length;
387
+ const expectedTasks = tasks.filter((t) => legacyUserIds.has(t.userId.toString())).length;
388
+ const expectedConfigs = configs.filter((c) => legacyUserIds.has(c.userId.toString())).length;
389
+ const expectedNonogramItems = nonogramItems.filter((i) =>
390
+ legacyUserIds.has(i.userId.toString())
391
+ ).length;
392
+ const expectedNonogramUpgrades = nonogramUpgrades.filter((u) =>
393
+ legacyUserIds.has(u.userId.toString())
394
+ ).length;
395
+
396
+ if (newUsersToCreate.length !== expectedUsers) {
397
+ DR.logger.error(`Expected ${expectedUsers} users, but prepped ${newUsersToCreate.length}.`);
398
+ }
399
+ if (newApiKeysToCreate.length !== expectedApiKeys) {
400
+ DR.logger.error(
401
+ `Expected ${expectedApiKeys} API keys, but prepped ${newApiKeysToCreate.length}.`
407
402
  );
408
- DR.logger.info(
409
- `Prepped ${newNonogramUpgradesToCreate.length} new nonogram upgrades.`
403
+ }
404
+ if (newTasksToCreate.length !== expectedTasks) {
405
+ DR.logger.error(`Expected ${expectedTasks} tasks, but prepped ${newTasksToCreate.length}.`);
406
+ }
407
+ if (newConfigsToCreate.length !== expectedConfigs) {
408
+ DR.logger.error(
409
+ `Expected ${expectedConfigs} configs, but prepped ${newConfigsToCreate.length}.`
410
410
  );
411
- DR.logger.info(`In total, prepped ${
411
+ }
412
+ if (newNonogramItemsToCreate.length !== expectedNonogramItems) {
413
+ DR.logger.error(
414
+ `Expected ${expectedNonogramItems} nonogram items, but prepped ${newNonogramItemsToCreate.length}.`
415
+ );
416
+ }
417
+ if (newNonogramUpgradesToCreate.length !== expectedNonogramUpgrades) {
418
+ DR.logger.error(
419
+ `Expected ${expectedNonogramUpgrades} nonogram upgrades, but prepped ${newNonogramUpgradesToCreate.length}.`
420
+ );
421
+ }
422
+
423
+ DR.logger.info(`Prepped ${newUsersToCreate.length} new users.`);
424
+ DR.logger.info(`Prepped ${newApiKeysToCreate.length} new API keys.`);
425
+ DR.logger.info(`Prepped ${newTasksToCreate.length} new tasks.`);
426
+ DR.logger.info(`Prepped ${newConfigsToCreate.length} new configs.`);
427
+ DR.logger.info(`Prepped ${newNonogramItemsToCreate.length} new nonogram items.`);
428
+ DR.logger.info(`Prepped ${newNonogramUpgradesToCreate.length} new nonogram upgrades.`);
429
+ DR.logger.info(
430
+ `In total, prepped ${
412
431
  newUsersToCreate.length +
413
432
  newApiKeysToCreate.length +
414
433
  newTasksToCreate.length +
415
434
  newConfigsToCreate.length +
416
435
  newNonogramItemsToCreate.length +
417
436
  newNonogramUpgradesToCreate.length
418
- } documents for insertion.`);
437
+ } documents for insertion.`
438
+ );
419
439
 
420
- if (!dryRun) {
421
- DR.logger.info('Dry run is false. Inserting documents into DB...');
422
- if (newUsersToCreate.length > 0) {
423
- await (await userRepo.getCollection()).insertMany(newUsersToCreate);
424
- }
425
- if (newApiKeysToCreate.length > 0) {
426
- await (await apiKeyRepo.getCollection()).insertMany(newApiKeysToCreate);
427
- }
428
- if (newTasksToCreate.length > 0) {
429
- await (await taskRepo.getCollection()).insertMany(newTasksToCreate);
430
- }
431
- if (newConfigsToCreate.length > 0) {
432
- await (await configRepo.getCollection()).insertMany(newConfigsToCreate);
433
- }
434
- if (newNonogramItemsToCreate.length > 0) {
435
- await (
436
- await nonogramItemRepo.getCollection()
437
- ).insertMany(newNonogramItemsToCreate);
438
- }
439
- if (newNonogramUpgradesToCreate.length > 0) {
440
- await (
441
- await nonogramUpgradesRepo.getCollection()
442
- ).insertMany(newNonogramUpgradesToCreate);
443
- }
444
- DR.logger.info('Insertion complete.');
445
- } else {
446
- DR.logger.info('Dry run is true. Skipping DB insertion.');
440
+ if (!dryRun) {
441
+ DR.logger.info('Dry run is false. Inserting documents into DB...');
442
+ if (newUsersToCreate.length > 0) {
443
+ await(await userRepo.getCollection()).insertMany(newUsersToCreate);
447
444
  }
448
-
449
- return;
445
+ if (newApiKeysToCreate.length > 0) {
446
+ await(await apiKeyRepo.getCollection()).insertMany(newApiKeysToCreate);
447
+ }
448
+ if (newTasksToCreate.length > 0) {
449
+ await(await taskRepo.getCollection()).insertMany(newTasksToCreate);
450
+ }
451
+ if (newConfigsToCreate.length > 0) {
452
+ await(await configRepo.getCollection()).insertMany(newConfigsToCreate);
453
+ }
454
+ if (newNonogramItemsToCreate.length > 0) {
455
+ await(await nonogramItemRepo.getCollection()).insertMany(newNonogramItemsToCreate);
456
+ }
457
+ if (newNonogramUpgradesToCreate.length > 0) {
458
+ await(await nonogramUpgradesRepo.getCollection()).insertMany(newNonogramUpgradesToCreate);
459
+ }
460
+ DR.logger.info('Insertion complete.');
461
+ } else {
462
+ DR.logger.info('Dry run is true. Skipping DB insertion.');
450
463
  }
451
464
 
452
- // We started with 1204 docs
465
+ return;
453
466
  }