@frogfish/k2db 1.0.8 → 1.0.10

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
@@ -263,26 +263,21 @@ class K2DB {
263
263
  * @param collectionName - Name of the collection.
264
264
  * @param criteria - Update criteria.
265
265
  * @param values - Values to update or replace with.
266
- * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
267
266
  */
268
- async updateAll(collectionName, criteria, values, replace = false) {
269
- // Return type changed to JSON object {updated: number}
267
+ async updateAll(collectionName, criteria, values) {
268
+ this.validateCollectionName(collectionName);
270
269
  const collection = await this.getCollection(collectionName);
271
270
  debug(`Updating ${collectionName} with criteria: ${JSON.stringify(criteria)}`);
272
271
  values._updated = Date.now();
273
- // Exclude soft-deleted documents unless _deleted is explicitly specified in criteria
274
- if (!("_deleted" in criteria)) {
275
- criteria = {
276
- ...criteria,
277
- _deleted: { $ne: true },
278
- };
279
- }
280
- // Determine update operation based on the replace flag
281
- const updateOperation = replace ? values : { $set: values };
272
+ criteria = {
273
+ ...criteria,
274
+ _deleted: { $ne: true },
275
+ };
282
276
  try {
283
- const res = await collection.updateMany(criteria, updateOperation);
284
- // Return the updated count in JSON format
285
- return { updated: res.modifiedCount };
277
+ const res = await collection.updateMany(criteria, { $set: values });
278
+ return {
279
+ updated: res.modifiedCount,
280
+ };
286
281
  }
287
282
  catch (err) {
288
283
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update1", this.normalizeError(err));
@@ -295,28 +290,48 @@ class K2DB {
295
290
  * @param id - UUID string to identify the document.
296
291
  * @param data - Data to update or replace with.
297
292
  * @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
298
- * @param objectTypeName - Optional object type name.
299
293
  */
300
- async update(collectionName, id, data, replace = false, objectTypeName) {
294
+ async update(collectionName, id, data, replace = false) {
295
+ this.validateCollectionName(collectionName);
301
296
  const collection = await this.getCollection(collectionName);
302
297
  data._updated = Date.now(); // Set the _updated timestamp
303
298
  try {
304
- // Call updateAll with the UUID criteria and replace flag
305
- const res = await this.updateAll(collectionName, { _uuid: id }, data, replace);
299
+ let res;
300
+ // If replacing the document, first get the original document
301
+ if (replace) {
302
+ // Get the original document to preserve fields starting with underscore
303
+ const originalDoc = await this.get(collectionName, id);
304
+ // Override all fields starting with underscore from the original document
305
+ const fieldsToPreserve = Object.keys(originalDoc).reduce((acc, key) => {
306
+ if (key.startsWith("_")) {
307
+ acc[key] = originalDoc[key];
308
+ }
309
+ return acc;
310
+ }, {});
311
+ // Merge the preserved fields into the data
312
+ data = { ...data, ...fieldsToPreserve };
313
+ data._updated = Date.now();
314
+ // Now replace the document with the merged data
315
+ res = await collection.replaceOne({ _uuid: id }, data);
316
+ }
317
+ else {
318
+ // If patching, just update specific fields using $set
319
+ res = await collection.updateOne({ _uuid: id }, { $set: data });
320
+ }
306
321
  // Check if exactly one document was updated
307
- if (res.updated === 1) {
322
+ if (res.modifiedCount === 1) {
308
323
  return { updated: 1 };
309
324
  }
310
325
  // If no document was updated, throw a NOT_FOUND error
311
- if (res.updated === 0) {
326
+ if (res.modifiedCount === 0) {
312
327
  throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, `Object in ${collectionName} with UUID ${id} not found`, "sys_mdb_update_not_found");
313
328
  }
314
- // If more than one document was updated, throw a SYSTEM_ERROR
315
- if (res.updated > 1) {
329
+ // If more than one document was updated (though this should never happen with a single UUID), throw a SYSTEM_ERROR
330
+ if (res.modifiedCount > 1) {
316
331
  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");
317
332
  }
318
- // Since we've handled all possible valid cases, we return undefined in case of unforeseen errors (for type safety)
319
- return { updated: 0 }; // Should never reach this line, here for type safety
333
+ // Return updated: 0 if no documents were modified (though this is unlikely)
334
+ return { updated: 0 };
320
335
  }
321
336
  catch (err) {
322
337
  if (err instanceof k2error_1.K2Error) {
@@ -332,33 +347,16 @@ class K2DB {
332
347
  * @param criteria - Removal criteria.
333
348
  */
334
349
  async deleteAll(collectionName, criteria) {
335
- const collection = await this.getCollection(collectionName);
336
- try {
337
- // Step 1: Count documents matching the original criteria (this is not strictly necessary if you only want the deleted count)
338
- const foundCount = await collection.countDocuments(criteria);
339
- }
340
- catch (err) {
341
- throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_count", this.normalizeError(err));
342
- }
343
- // Step 2: Modify criteria to exclude already deleted documents
344
- const modifiedCriteria = {
345
- ...criteria,
346
- _deleted: { $ne: true },
347
- };
348
- let result;
350
+ this.validateCollectionName(collectionName);
349
351
  try {
350
- // Perform the update to soft delete the documents
351
- result = await this.updateAll(collectionName, modifiedCriteria, {
352
+ let result = await this.updateAll(collectionName, criteria, {
352
353
  _deleted: true,
353
354
  });
355
+ return { deleted: result.updated };
354
356
  }
355
357
  catch (err) {
356
358
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_update", this.normalizeError(err));
357
359
  }
358
- // Step 3: Return the number of records that were marked as deleted
359
- return {
360
- deleted: result.updated, // Map the updated count to 'deleted'
361
- };
362
360
  }
363
361
  /**
364
362
  * Removes (soft deletes) a single document by UUID.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "A data handling library for K2 applications.",
5
5
  "main": "data.js",
6
6
  "types": "data.d.ts",