@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 +2 -2
- package/data.js +4 -4
- package/db.d.ts +2 -4
- package/db.js +43 -45
- package/package.json +1 -1
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
|
|
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
|
|
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
|
|
47
|
+
async updateAll(collectionName, criteria, values) {
|
|
48
48
|
// Ensure it returns { updated: number }
|
|
49
|
-
return this.db.updateAll(collectionName, criteria, values
|
|
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
|
|
54
|
+
async update(collectionName, id, data, replace = false) {
|
|
55
55
|
// Ensure it returns { updated: number }
|
|
56
|
-
return this.db.update(collectionName, id, data, replace
|
|
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
|
|
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
|
|
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
|
|
269
|
-
|
|
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
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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,
|
|
284
|
-
|
|
285
|
-
|
|
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
|
|
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
|
-
|
|
305
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
//
|
|
319
|
-
return { updated: 0 };
|
|
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
|
-
|
|
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
|
-
|
|
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.
|