@frogfish/k2db 1.0.9 → 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 +44 -53
- 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
|
@@ -76,10 +76,7 @@ class K2DB {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
async get(collectionName, uuid) {
|
|
79
|
-
const res = await this.findOne(collectionName, {
|
|
80
|
-
_uuid: uuid,
|
|
81
|
-
_deleted: { $ne: true },
|
|
82
|
-
});
|
|
79
|
+
const res = await this.findOne(collectionName, { _uuid: uuid });
|
|
83
80
|
if (!res) {
|
|
84
81
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Error getting the document with provided identity", "sys_mdb_get");
|
|
85
82
|
}
|
|
@@ -95,8 +92,6 @@ class K2DB {
|
|
|
95
92
|
async findOne(collectionName, criteria, fields) {
|
|
96
93
|
const collection = await this.getCollection(collectionName);
|
|
97
94
|
const projection = {};
|
|
98
|
-
// Ensure to include the _deleted filter in criteria
|
|
99
|
-
criteria._deleted = { $ne: true };
|
|
100
95
|
if (fields && fields.length > 0) {
|
|
101
96
|
fields.forEach((field) => {
|
|
102
97
|
projection[field] = 1;
|
|
@@ -268,26 +263,21 @@ class K2DB {
|
|
|
268
263
|
* @param collectionName - Name of the collection.
|
|
269
264
|
* @param criteria - Update criteria.
|
|
270
265
|
* @param values - Values to update or replace with.
|
|
271
|
-
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
272
266
|
*/
|
|
273
|
-
async updateAll(collectionName, criteria, values
|
|
274
|
-
|
|
267
|
+
async updateAll(collectionName, criteria, values) {
|
|
268
|
+
this.validateCollectionName(collectionName);
|
|
275
269
|
const collection = await this.getCollection(collectionName);
|
|
276
270
|
debug(`Updating ${collectionName} with criteria: ${JSON.stringify(criteria)}`);
|
|
277
271
|
values._updated = Date.now();
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
_deleted: { $ne: true },
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
// Determine update operation based on the replace flag
|
|
286
|
-
const updateOperation = replace ? values : { $set: values };
|
|
272
|
+
criteria = {
|
|
273
|
+
...criteria,
|
|
274
|
+
_deleted: { $ne: true },
|
|
275
|
+
};
|
|
287
276
|
try {
|
|
288
|
-
const res = await collection.updateMany(criteria,
|
|
289
|
-
|
|
290
|
-
|
|
277
|
+
const res = await collection.updateMany(criteria, { $set: values });
|
|
278
|
+
return {
|
|
279
|
+
updated: res.modifiedCount,
|
|
280
|
+
};
|
|
291
281
|
}
|
|
292
282
|
catch (err) {
|
|
293
283
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update1", this.normalizeError(err));
|
|
@@ -300,28 +290,48 @@ class K2DB {
|
|
|
300
290
|
* @param id - UUID string to identify the document.
|
|
301
291
|
* @param data - Data to update or replace with.
|
|
302
292
|
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
303
|
-
* @param objectTypeName - Optional object type name.
|
|
304
293
|
*/
|
|
305
|
-
async update(collectionName, id, data, replace = false
|
|
294
|
+
async update(collectionName, id, data, replace = false) {
|
|
295
|
+
this.validateCollectionName(collectionName);
|
|
306
296
|
const collection = await this.getCollection(collectionName);
|
|
307
297
|
data._updated = Date.now(); // Set the _updated timestamp
|
|
308
298
|
try {
|
|
309
|
-
|
|
310
|
-
|
|
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
|
+
}
|
|
311
321
|
// Check if exactly one document was updated
|
|
312
|
-
if (res.
|
|
322
|
+
if (res.modifiedCount === 1) {
|
|
313
323
|
return { updated: 1 };
|
|
314
324
|
}
|
|
315
325
|
// If no document was updated, throw a NOT_FOUND error
|
|
316
|
-
if (res.
|
|
326
|
+
if (res.modifiedCount === 0) {
|
|
317
327
|
throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, `Object in ${collectionName} with UUID ${id} not found`, "sys_mdb_update_not_found");
|
|
318
328
|
}
|
|
319
|
-
// If more than one document was updated, throw a SYSTEM_ERROR
|
|
320
|
-
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) {
|
|
321
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");
|
|
322
332
|
}
|
|
323
|
-
//
|
|
324
|
-
return { updated: 0 };
|
|
333
|
+
// Return updated: 0 if no documents were modified (though this is unlikely)
|
|
334
|
+
return { updated: 0 };
|
|
325
335
|
}
|
|
326
336
|
catch (err) {
|
|
327
337
|
if (err instanceof k2error_1.K2Error) {
|
|
@@ -337,33 +347,16 @@ class K2DB {
|
|
|
337
347
|
* @param criteria - Removal criteria.
|
|
338
348
|
*/
|
|
339
349
|
async deleteAll(collectionName, criteria) {
|
|
340
|
-
|
|
350
|
+
this.validateCollectionName(collectionName);
|
|
341
351
|
try {
|
|
342
|
-
|
|
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;
|
|
354
|
-
try {
|
|
355
|
-
// Perform the update to soft delete the documents
|
|
356
|
-
result = await this.updateAll(collectionName, modifiedCriteria, {
|
|
352
|
+
let result = await this.updateAll(collectionName, criteria, {
|
|
357
353
|
_deleted: true,
|
|
358
354
|
});
|
|
355
|
+
return { deleted: result.updated };
|
|
359
356
|
}
|
|
360
357
|
catch (err) {
|
|
361
358
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_update", this.normalizeError(err));
|
|
362
359
|
}
|
|
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
360
|
}
|
|
368
361
|
/**
|
|
369
362
|
* Removes (soft deletes) a single document by UUID.
|
|
@@ -443,8 +436,6 @@ class K2DB {
|
|
|
443
436
|
async count(collectionName, criteria) {
|
|
444
437
|
const collection = await this.getCollection(collectionName);
|
|
445
438
|
try {
|
|
446
|
-
// Ensure to include the _deleted filter in criteria
|
|
447
|
-
criteria._deleted = { $ne: true };
|
|
448
439
|
const cnt = await collection.countDocuments(criteria);
|
|
449
440
|
return { count: cnt };
|
|
450
441
|
}
|