@aneuhold/be-ts-db-lib 4.2.18 → 4.2.20

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.
@@ -1,22 +1,17 @@
1
1
  /* eslint-disable */
2
2
  // @ts-nocheck
3
- import {
4
- ApiKey,
5
- DashboardTask,
6
- DashboardUserConfig,
7
- DocumentService,
8
- NonogramKatanaItem,
9
- NonogramKatanaUpgrade,
10
- User
11
- } from '@aneuhold/core-ts-db-lib';
12
3
  import { DR } from '@aneuhold/core-ts-lib';
13
- import { v7 as uuidv7 } from 'uuid';
14
- import ApiKeyRepository from '../repositories/common/ApiKeyRepository.js';
15
4
  import UserRepository from '../repositories/common/UserRepository.js';
16
- import DashboardNonogramKatanaItemRepository from '../repositories/dashboard/DashboardNonogramKatanaItemRepository.js';
17
- import DashboardNonogramKatanaUpgradeRepository from '../repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js';
18
- import DashboardTaskRepository from '../repositories/dashboard/DashboardTaskRepository.js';
19
- import DashboardUserConfigRepository from '../repositories/dashboard/DashboardUserConfigRepository.js';
5
+
6
+ /**
7
+ * The default values for each project access field. New fields should be
8
+ * added here and defaulted to `false` so existing users don't gain access
9
+ * automatically.
10
+ */
11
+ const PROJECT_ACCESS_DEFAULTS = {
12
+ dashboard: false,
13
+ workout: false
14
+ };
20
15
 
21
16
  /**
22
17
  * A service for migrating the DB to a new state after an existing document
@@ -38,429 +33,51 @@ export default class MigrationService {
38
33
  static async migrateDb(dryRun = false): Promise<void> {
39
34
  DR.logger.info('Starting migration...');
40
35
 
41
- // 1. Load all documents
42
36
  const userRepo = UserRepository.getRepo();
43
- const apiKeyRepo = ApiKeyRepository.getRepo();
44
- const taskRepo = DashboardTaskRepository.getRepo();
45
- const configRepo = DashboardUserConfigRepository.getRepo();
46
- const nonogramItemRepo = DashboardNonogramKatanaItemRepository.getRepo();
47
- const nonogramUpgradesRepo = DashboardNonogramKatanaUpgradeRepository.getRepo();
48
-
49
37
  const users = await userRepo.getAll();
50
- const apiKeys = await apiKeyRepo.getAll();
51
- const tasks = await taskRepo.getAll();
52
- const configs = await configRepo.getAll();
53
- const nonogramItems = await nonogramItemRepo.getAll();
54
- const nonogramUpgrades = await nonogramUpgradesRepo.getAll();
55
-
56
- DR.logger.info(
57
- `Loaded ${users.length} users, ${apiKeys.length} API keys, ${tasks.length} tasks, ${configs.length} configs, ${nonogramItems.length} nonogram items, and ${nonogramUpgrades.length} nonogram upgrades.`
58
- );
59
38
 
60
- const countObjectIds = (docs: any[]) => {
61
- docs.forEach((doc) => {
62
- // Get the type of the _id field
63
- console.log(`Document ID type: ${typeof doc._id}, value: ${doc._id}`);
64
- });
65
- };
66
-
67
- // Create a map of document IDs to new UUIDs
68
- const allDocs = [
69
- ...users,
70
- ...apiKeys,
71
- ...tasks,
72
- ...configs,
73
- ...nonogramItems,
74
- ...nonogramUpgrades
75
- ];
76
- const legacyDocs = allDocs.filter((doc) => typeof doc._id === 'object');
77
- const newDocs = allDocs.filter((doc) => typeof doc._id === 'string');
39
+ // Find users that are missing projectAccess entirely or are missing
40
+ // any of the expected fields.
41
+ const usersToUpdate = users.filter((user) => {
42
+ if (!user.projectAccess) return true;
43
+ return Object.keys(PROJECT_ACCESS_DEFAULTS).some(
44
+ (key) => user.projectAccess[key] === undefined
45
+ );
46
+ });
78
47
 
79
48
  DR.logger.info(
80
- `Found ${legacyDocs.length} documents with legacy ObjectId IDs and ${newDocs.length} documents with string IDs.`
49
+ `Found ${usersToUpdate.length} of ${users.length} users with missing projectAccess fields.`
81
50
  );
82
51
 
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`);
148
- }
149
- }
150
- // We started with 1204 docs
151
- }
152
- }
153
-
154
- async function Migration2025UUID() {
155
- // Now create the actual map
156
- const mapFromObjectIdToUUID = new Map<string, UUID>();
157
-
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
- );
52
+ if (usersToUpdate.length === 0) {
53
+ DR.logger.success('No migration needed.');
54
+ return;
191
55
  }
192
- };
193
56
 
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.` +
201
- ` The created document is ${JSON.stringify(mapFromUUIDToDocument.get(mapFromObjectIdToUUID.get(oldDoc._id.toString())))}`
202
- );
57
+ if (dryRun) {
58
+ DR.logger.info('Dry run: Would update the following users:');
59
+ usersToUpdate.forEach((user) => {
60
+ const existing = user.projectAccess ?? {};
61
+ const merged = { ...PROJECT_ACCESS_DEFAULTS, ...existing };
62
+ DR.logger.info(
63
+ ` - ${user.userName} (${user._id}): ${JSON.stringify(existing)} ${JSON.stringify(merged)}`
64
+ );
65
+ });
203
66
  return;
204
67
  }
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
- }
212
-
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);
242
- });
243
-
244
- // 2. Create new Tasks
245
- const userTasks = tasks.filter((t) => t.userId.toString() === oldUserIdStr);
246
-
247
- const taskChildrenMap = new Map<string, DashboardTask[]>();
248
- const rootTasks: DashboardTask[] = [];
249
- const userTaskIds = new Set(userTasks.map((t) => t._id.toString()));
250
-
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
- });
262
68
 
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
- };
313
- }
314
-
315
- verifyDoc(oldTask, newTask);
316
- newTasksToCreate.push(newTask);
317
- }
318
-
319
- const children = taskChildrenMap.get(oldTask._id.toString());
320
- if (children) {
321
- queue.push(...children);
322
- }
69
+ for (const user of usersToUpdate) {
70
+ const existing = user.projectAccess ?? {};
71
+ const merged = { ...PROJECT_ACCESS_DEFAULTS, ...existing };
72
+ await userRepo.update({
73
+ _id: user._id,
74
+ projectAccess: merged
75
+ });
76
+ DR.logger.info(
77
+ `Updated user ${user.userName} (${user._id}): ${JSON.stringify(existing)} ${JSON.stringify(merged)}`
78
+ );
323
79
  }
324
80
 
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
338
- };
339
- newConfig.taskListSortSettings[category] = newSettings;
340
- }
341
- );
342
- }
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
- }
352
- );
353
- }
354
- verifyDoc(oldConfig, newConfig);
355
- newConfigsToCreate.push(newConfig);
356
- });
357
-
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
- });
367
-
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
- }
380
-
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}.`
402
- );
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
- );
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
- );
81
+ DR.logger.success(`Migration complete. Updated ${usersToUpdate.length} users.`);
421
82
  }
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 ${
431
- newUsersToCreate.length +
432
- newApiKeysToCreate.length +
433
- newTasksToCreate.length +
434
- newConfigsToCreate.length +
435
- newNonogramItemsToCreate.length +
436
- newNonogramUpgradesToCreate.length
437
- } documents for insertion.`
438
- );
439
-
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);
444
- }
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.');
463
- }
464
-
465
- return;
466
83
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@aneuhold/be-ts-db-lib",
3
3
  "author": "Anton G. Neuhold Jr.",
4
4
  "license": "MIT",
5
- "version": "4.2.18",
5
+ "version": "4.2.20",
6
6
  "description": "A backend database library meant to actually interact with various databases in personal projects",
7
7
  "packageManager": "pnpm@10.25.0",
8
8
  "type": "module",
@@ -58,8 +58,8 @@
58
58
  "MongoDB"
59
59
  ],
60
60
  "dependencies": {
61
- "@aneuhold/be-ts-lib": "^3.1.5",
62
- "@aneuhold/core-ts-db-lib": "^5.0.1",
61
+ "@aneuhold/be-ts-lib": "^3.1.7",
62
+ "@aneuhold/core-ts-db-lib": "^5.0.2",
63
63
  "@aneuhold/core-ts-lib": "^2.4.3",
64
64
  "bson": "^7.0.0",
65
65
  "google-auth-library": "^10.6.1",