@frogfish/k2db 1.0.9 → 1.0.11

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.
package/data.d.ts CHANGED
@@ -33,13 +33,13 @@ export declare class K2Data {
33
33
  /**
34
34
  * Updates multiple documents based on criteria.
35
35
  */
36
- updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<{
36
+ updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>): Promise<{
37
37
  updated: number;
38
38
  }>;
39
39
  /**
40
40
  * Updates a single document by UUID.
41
41
  */
42
- update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<{
42
+ update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean): Promise<{
43
43
  updated: number;
44
44
  }>;
45
45
  /**
package/data.js CHANGED
@@ -44,16 +44,16 @@ class K2Data {
44
44
  /**
45
45
  * Updates multiple documents based on criteria.
46
46
  */
47
- async updateAll(collectionName, criteria, values, replace = false) {
47
+ async updateAll(collectionName, criteria, values) {
48
48
  // Ensure it returns { updated: number }
49
- return this.db.updateAll(collectionName, criteria, values, replace);
49
+ return this.db.updateAll(collectionName, criteria, values);
50
50
  }
51
51
  /**
52
52
  * Updates a single document by UUID.
53
53
  */
54
- async update(collectionName, id, data, replace = false, objectTypeName) {
54
+ async update(collectionName, id, data, replace = false) {
55
55
  // Ensure it returns { updated: number }
56
- return this.db.update(collectionName, id, data, replace, objectTypeName);
56
+ return this.db.update(collectionName, id, data, replace);
57
57
  }
58
58
  /**
59
59
  * Removes (soft deletes) multiple documents based on criteria.
package/db.d.ts CHANGED
@@ -74,9 +74,8 @@ export declare class K2DB {
74
74
  * @param collectionName - Name of the collection.
75
75
  * @param criteria - Update criteria.
76
76
  * @param values - Values to update or replace with.
77
- * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
78
77
  */
79
- updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<{
78
+ updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>): Promise<{
80
79
  updated: number;
81
80
  }>;
82
81
  /**
@@ -86,9 +85,8 @@ export declare class K2DB {
86
85
  * @param id - UUID string to identify the document.
87
86
  * @param data - Data to update or replace with.
88
87
  * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
89
- * @param objectTypeName - Optional object type name.
90
88
  */
91
- update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<{
89
+ update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean): Promise<{
92
90
  updated: number;
93
91
  }>;
94
92
  /**
package/db.js CHANGED
@@ -95,8 +95,6 @@ class K2DB {
95
95
  async findOne(collectionName, criteria, fields) {
96
96
  const collection = await this.getCollection(collectionName);
97
97
  const projection = {};
98
- // Ensure to include the _deleted filter in criteria
99
- criteria._deleted = { $ne: true };
100
98
  if (fields && fields.length > 0) {
101
99
  fields.forEach((field) => {
102
100
  projection[field] = 1;
@@ -268,26 +266,21 @@ class K2DB {
268
266
  * @param collectionName - Name of the collection.
269
267
  * @param criteria - Update criteria.
270
268
  * @param values - Values to update or replace with.
271
- * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
272
269
  */
273
- async updateAll(collectionName, criteria, values, replace = false) {
274
- // Return type changed to JSON object {updated: number}
270
+ async updateAll(collectionName, criteria, values) {
271
+ this.validateCollectionName(collectionName);
275
272
  const collection = await this.getCollection(collectionName);
276
273
  debug(`Updating ${collectionName} with criteria: ${JSON.stringify(criteria)}`);
277
274
  values._updated = Date.now();
278
- // Exclude soft-deleted documents unless _deleted is explicitly specified in criteria
279
- if (!("_deleted" in criteria)) {
280
- criteria = {
281
- ...criteria,
282
- _deleted: { $ne: true },
283
- };
284
- }
285
- // Determine update operation based on the replace flag
286
- const updateOperation = replace ? values : { $set: values };
275
+ criteria = {
276
+ ...criteria,
277
+ _deleted: { $ne: true },
278
+ };
287
279
  try {
288
- const res = await collection.updateMany(criteria, updateOperation);
289
- // Return the updated count in JSON format
290
- return { updated: res.modifiedCount };
280
+ const res = await collection.updateMany(criteria, { $set: values });
281
+ return {
282
+ updated: res.modifiedCount,
283
+ };
291
284
  }
292
285
  catch (err) {
293
286
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update1", this.normalizeError(err));
@@ -300,28 +293,48 @@ class K2DB {
300
293
  * @param id - UUID string to identify the document.
301
294
  * @param data - Data to update or replace with.
302
295
  * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
303
- * @param objectTypeName - Optional object type name.
304
296
  */
305
- async update(collectionName, id, data, replace = false, objectTypeName) {
297
+ async update(collectionName, id, data, replace = false) {
298
+ this.validateCollectionName(collectionName);
306
299
  const collection = await this.getCollection(collectionName);
307
300
  data._updated = Date.now(); // Set the _updated timestamp
308
301
  try {
309
- // Call updateAll with the UUID criteria and replace flag
310
- const res = await this.updateAll(collectionName, { _uuid: id }, data, replace);
302
+ let res;
303
+ // If replacing the document, first get the original document
304
+ if (replace) {
305
+ // Get the original document to preserve fields starting with underscore
306
+ const originalDoc = await this.get(collectionName, id);
307
+ // Override all fields starting with underscore from the original document
308
+ const fieldsToPreserve = Object.keys(originalDoc).reduce((acc, key) => {
309
+ if (key.startsWith("_")) {
310
+ acc[key] = originalDoc[key];
311
+ }
312
+ return acc;
313
+ }, {});
314
+ // Merge the preserved fields into the data
315
+ data = { ...data, ...fieldsToPreserve };
316
+ data._updated = Date.now();
317
+ // Now replace the document with the merged data
318
+ res = await collection.replaceOne({ _uuid: id }, data);
319
+ }
320
+ else {
321
+ // If patching, just update specific fields using $set
322
+ res = await collection.updateOne({ _uuid: id }, { $set: data });
323
+ }
311
324
  // Check if exactly one document was updated
312
- if (res.updated === 1) {
325
+ if (res.modifiedCount === 1) {
313
326
  return { updated: 1 };
314
327
  }
315
328
  // If no document was updated, throw a NOT_FOUND error
316
- if (res.updated === 0) {
329
+ if (res.modifiedCount === 0) {
317
330
  throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, `Object in ${collectionName} with UUID ${id} not found`, "sys_mdb_update_not_found");
318
331
  }
319
- // If more than one document was updated, throw a SYSTEM_ERROR
320
- if (res.updated > 1) {
332
+ // If more than one document was updated (though this should never happen with a single UUID), throw a SYSTEM_ERROR
333
+ if (res.modifiedCount > 1) {
321
334
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Multiple objects in ${collectionName} were updated when only one was expected`, "sys_mdb_update_multiple_found");
322
335
  }
323
- // Since we've handled all possible valid cases, we return undefined in case of unforeseen errors (for type safety)
324
- return { updated: 0 }; // Should never reach this line, here for type safety
336
+ // Return updated: 0 if no documents were modified (though this is unlikely)
337
+ return { updated: 0 };
325
338
  }
326
339
  catch (err) {
327
340
  if (err instanceof k2error_1.K2Error) {
@@ -337,33 +350,16 @@ class K2DB {
337
350
  * @param criteria - Removal criteria.
338
351
  */
339
352
  async deleteAll(collectionName, criteria) {
340
- const collection = await this.getCollection(collectionName);
341
- try {
342
- // Step 1: Count documents matching the original criteria (this is not strictly necessary if you only want the deleted count)
343
- const foundCount = await collection.countDocuments(criteria);
344
- }
345
- catch (err) {
346
- throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_count", this.normalizeError(err));
347
- }
348
- // Step 2: Modify criteria to exclude already deleted documents
349
- const modifiedCriteria = {
350
- ...criteria,
351
- _deleted: { $ne: true },
352
- };
353
- let result;
353
+ this.validateCollectionName(collectionName);
354
354
  try {
355
- // Perform the update to soft delete the documents
356
- result = await this.updateAll(collectionName, modifiedCriteria, {
355
+ let result = await this.updateAll(collectionName, criteria, {
357
356
  _deleted: true,
358
357
  });
358
+ return { deleted: result.updated };
359
359
  }
360
360
  catch (err) {
361
361
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_update", this.normalizeError(err));
362
362
  }
363
- // Step 3: Return the number of records that were marked as deleted
364
- return {
365
- deleted: result.updated, // Map the updated count to 'deleted'
366
- };
367
363
  }
368
364
  /**
369
365
  * Removes (soft deletes) a single document by UUID.
@@ -443,8 +439,6 @@ class K2DB {
443
439
  async count(collectionName, criteria) {
444
440
  const collection = await this.getCollection(collectionName);
445
441
  try {
446
- // Ensure to include the _deleted filter in criteria
447
- criteria._deleted = { $ne: true };
448
442
  const cnt = await collection.countDocuments(criteria);
449
443
  return { count: cnt };
450
444
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "A data handling library for K2 applications.",
5
5
  "main": "data.js",
6
6
  "types": "data.d.ts",