@frogfish/k2db 1.0.7 → 1.0.9
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 +10 -4
- package/data.js +5 -3
- package/db.d.ts +10 -4
- package/db.js +46 -21
- package/package.json +1 -1
package/data.d.ts
CHANGED
|
@@ -33,20 +33,26 @@ 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>, replace?: boolean): Promise<{
|
|
37
|
+
updated: number;
|
|
38
|
+
}>;
|
|
37
39
|
/**
|
|
38
40
|
* Updates a single document by UUID.
|
|
39
41
|
*/
|
|
40
|
-
update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<
|
|
42
|
+
update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<{
|
|
43
|
+
updated: number;
|
|
44
|
+
}>;
|
|
41
45
|
/**
|
|
42
46
|
* Removes (soft deletes) multiple documents based on criteria.
|
|
43
47
|
*/
|
|
44
|
-
deleteAll(collectionName: string, criteria: any): Promise<
|
|
48
|
+
deleteAll(collectionName: string, criteria: any): Promise<{
|
|
49
|
+
deleted: number;
|
|
50
|
+
}>;
|
|
45
51
|
/**
|
|
46
52
|
* Removes (soft deletes) a single document by UUID.
|
|
47
53
|
*/
|
|
48
54
|
delete(collectionName: string, id: string): Promise<{
|
|
49
|
-
|
|
55
|
+
deleted: number;
|
|
50
56
|
}>;
|
|
51
57
|
/**
|
|
52
58
|
* Permanently deletes a document that has been soft-deleted.
|
package/data.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// src/Data.ts
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.K2Data = void 0;
|
|
5
4
|
class K2Data {
|
|
@@ -45,19 +44,22 @@ class K2Data {
|
|
|
45
44
|
/**
|
|
46
45
|
* Updates multiple documents based on criteria.
|
|
47
46
|
*/
|
|
48
|
-
async updateAll(collectionName, criteria, values, replace) {
|
|
47
|
+
async updateAll(collectionName, criteria, values, replace = false) {
|
|
48
|
+
// Ensure it returns { updated: number }
|
|
49
49
|
return this.db.updateAll(collectionName, criteria, values, replace);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Updates a single document by UUID.
|
|
53
53
|
*/
|
|
54
|
-
async update(collectionName, id, data, replace, objectTypeName) {
|
|
54
|
+
async update(collectionName, id, data, replace = false, objectTypeName) {
|
|
55
|
+
// Ensure it returns { updated: number }
|
|
55
56
|
return this.db.update(collectionName, id, data, replace, objectTypeName);
|
|
56
57
|
}
|
|
57
58
|
/**
|
|
58
59
|
* Removes (soft deletes) multiple documents based on criteria.
|
|
59
60
|
*/
|
|
60
61
|
async deleteAll(collectionName, criteria) {
|
|
62
|
+
// Ensure it returns { deleted: number }
|
|
61
63
|
return this.db.deleteAll(collectionName, criteria);
|
|
62
64
|
}
|
|
63
65
|
/**
|
package/db.d.ts
CHANGED
|
@@ -76,7 +76,9 @@ export declare class K2DB {
|
|
|
76
76
|
* @param values - Values to update or replace with.
|
|
77
77
|
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
78
78
|
*/
|
|
79
|
-
updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<
|
|
79
|
+
updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<{
|
|
80
|
+
updated: number;
|
|
81
|
+
}>;
|
|
80
82
|
/**
|
|
81
83
|
* Updates a single document by UUID.
|
|
82
84
|
* Can either replace the document or patch it.
|
|
@@ -86,20 +88,24 @@ export declare class K2DB {
|
|
|
86
88
|
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
87
89
|
* @param objectTypeName - Optional object type name.
|
|
88
90
|
*/
|
|
89
|
-
update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<
|
|
91
|
+
update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<{
|
|
92
|
+
updated: number;
|
|
93
|
+
}>;
|
|
90
94
|
/**
|
|
91
95
|
* Removes (soft deletes) multiple documents based on criteria.
|
|
92
96
|
* @param collectionName - Name of the collection.
|
|
93
97
|
* @param criteria - Removal criteria.
|
|
94
98
|
*/
|
|
95
|
-
deleteAll(collectionName: string, criteria: any): Promise<
|
|
99
|
+
deleteAll(collectionName: string, criteria: any): Promise<{
|
|
100
|
+
deleted: number;
|
|
101
|
+
}>;
|
|
96
102
|
/**
|
|
97
103
|
* Removes (soft deletes) a single document by UUID.
|
|
98
104
|
* @param collectionName - Name of the collection.
|
|
99
105
|
* @param id - UUID of the document.
|
|
100
106
|
*/
|
|
101
107
|
delete(collectionName: string, id: string): Promise<{
|
|
102
|
-
|
|
108
|
+
deleted: number;
|
|
103
109
|
}>;
|
|
104
110
|
/**
|
|
105
111
|
* Permanently deletes a document that has been soft-deleted.
|
package/db.js
CHANGED
|
@@ -76,7 +76,10 @@ class K2DB {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
async get(collectionName, uuid) {
|
|
79
|
-
const res = await this.findOne(collectionName, {
|
|
79
|
+
const res = await this.findOne(collectionName, {
|
|
80
|
+
_uuid: uuid,
|
|
81
|
+
_deleted: { $ne: true },
|
|
82
|
+
});
|
|
80
83
|
if (!res) {
|
|
81
84
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Error getting the document with provided identity", "sys_mdb_get");
|
|
82
85
|
}
|
|
@@ -92,6 +95,8 @@ class K2DB {
|
|
|
92
95
|
async findOne(collectionName, criteria, fields) {
|
|
93
96
|
const collection = await this.getCollection(collectionName);
|
|
94
97
|
const projection = {};
|
|
98
|
+
// Ensure to include the _deleted filter in criteria
|
|
99
|
+
criteria._deleted = { $ne: true };
|
|
95
100
|
if (fields && fields.length > 0) {
|
|
96
101
|
fields.forEach((field) => {
|
|
97
102
|
projection[field] = 1;
|
|
@@ -266,6 +271,7 @@ class K2DB {
|
|
|
266
271
|
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
267
272
|
*/
|
|
268
273
|
async updateAll(collectionName, criteria, values, replace = false) {
|
|
274
|
+
// Return type changed to JSON object {updated: number}
|
|
269
275
|
const collection = await this.getCollection(collectionName);
|
|
270
276
|
debug(`Updating ${collectionName} with criteria: ${JSON.stringify(criteria)}`);
|
|
271
277
|
values._updated = Date.now();
|
|
@@ -280,11 +286,8 @@ class K2DB {
|
|
|
280
286
|
const updateOperation = replace ? values : { $set: values };
|
|
281
287
|
try {
|
|
282
288
|
const res = await collection.updateMany(criteria, updateOperation);
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
modified: res.modifiedCount,
|
|
286
|
-
updated: res.acknowledged,
|
|
287
|
-
};
|
|
289
|
+
// Return the updated count in JSON format
|
|
290
|
+
return { updated: res.modifiedCount };
|
|
288
291
|
}
|
|
289
292
|
catch (err) {
|
|
290
293
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update1", this.normalizeError(err));
|
|
@@ -301,22 +304,31 @@ class K2DB {
|
|
|
301
304
|
*/
|
|
302
305
|
async update(collectionName, id, data, replace = false, objectTypeName) {
|
|
303
306
|
const collection = await this.getCollection(collectionName);
|
|
304
|
-
data._updated = Date.now();
|
|
307
|
+
data._updated = Date.now(); // Set the _updated timestamp
|
|
305
308
|
try {
|
|
306
309
|
// Call updateAll with the UUID criteria and replace flag
|
|
307
310
|
const res = await this.updateAll(collectionName, { _uuid: id }, data, replace);
|
|
308
|
-
if
|
|
309
|
-
|
|
311
|
+
// Check if exactly one document was updated
|
|
312
|
+
if (res.updated === 1) {
|
|
313
|
+
return { updated: 1 };
|
|
310
314
|
}
|
|
311
|
-
|
|
312
|
-
|
|
315
|
+
// If no document was updated, throw a NOT_FOUND error
|
|
316
|
+
if (res.updated === 0) {
|
|
317
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, `Object in ${collectionName} with UUID ${id} not found`, "sys_mdb_update_not_found");
|
|
313
318
|
}
|
|
319
|
+
// If more than one document was updated, throw a SYSTEM_ERROR
|
|
320
|
+
if (res.updated > 1) {
|
|
321
|
+
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
|
+
}
|
|
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
|
|
314
325
|
}
|
|
315
326
|
catch (err) {
|
|
316
327
|
if (err instanceof k2error_1.K2Error) {
|
|
317
328
|
throw err;
|
|
318
329
|
}
|
|
319
|
-
|
|
330
|
+
// Catch any other unhandled errors and throw a system error
|
|
331
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update_error", this.normalizeError(err));
|
|
320
332
|
}
|
|
321
333
|
}
|
|
322
334
|
/**
|
|
@@ -326,10 +338,9 @@ class K2DB {
|
|
|
326
338
|
*/
|
|
327
339
|
async deleteAll(collectionName, criteria) {
|
|
328
340
|
const collection = await this.getCollection(collectionName);
|
|
329
|
-
let foundCount;
|
|
330
341
|
try {
|
|
331
|
-
// Step 1: Count documents matching the original criteria
|
|
332
|
-
foundCount = await collection.countDocuments(criteria);
|
|
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);
|
|
333
344
|
}
|
|
334
345
|
catch (err) {
|
|
335
346
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_count", this.normalizeError(err));
|
|
@@ -341,7 +352,7 @@ class K2DB {
|
|
|
341
352
|
};
|
|
342
353
|
let result;
|
|
343
354
|
try {
|
|
344
|
-
// Perform the update
|
|
355
|
+
// Perform the update to soft delete the documents
|
|
345
356
|
result = await this.updateAll(collectionName, modifiedCriteria, {
|
|
346
357
|
_deleted: true,
|
|
347
358
|
});
|
|
@@ -349,10 +360,9 @@ class K2DB {
|
|
|
349
360
|
catch (err) {
|
|
350
361
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_update", this.normalizeError(err));
|
|
351
362
|
}
|
|
352
|
-
// Step 3: Return the
|
|
363
|
+
// Step 3: Return the number of records that were marked as deleted
|
|
353
364
|
return {
|
|
354
|
-
|
|
355
|
-
found: foundCount, // Use the original count here
|
|
365
|
+
deleted: result.updated, // Map the updated count to 'deleted'
|
|
356
366
|
};
|
|
357
367
|
}
|
|
358
368
|
/**
|
|
@@ -362,8 +372,21 @@ class K2DB {
|
|
|
362
372
|
*/
|
|
363
373
|
async delete(collectionName, id) {
|
|
364
374
|
try {
|
|
365
|
-
|
|
366
|
-
|
|
375
|
+
// Call deleteAll to soft delete the document by UUID
|
|
376
|
+
const result = await this.deleteAll(collectionName, { _uuid: id });
|
|
377
|
+
// Check the result of the deleteAll operation
|
|
378
|
+
if (result.deleted === 1) {
|
|
379
|
+
// Successfully deleted one document
|
|
380
|
+
return { deleted: 1 };
|
|
381
|
+
}
|
|
382
|
+
else if (result.deleted === 0) {
|
|
383
|
+
// No document was found to delete
|
|
384
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, "Document not found", "sys_mdb_remove_not_found");
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
// More than one document was deleted, which is unexpected
|
|
388
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Multiple documents deleted when only one was expected", "sys_mdb_remove_multiple_deleted");
|
|
389
|
+
}
|
|
367
390
|
}
|
|
368
391
|
catch (err) {
|
|
369
392
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Error removing object from collection", "sys_mdb_remove_upd", this.normalizeError(err));
|
|
@@ -420,6 +443,8 @@ class K2DB {
|
|
|
420
443
|
async count(collectionName, criteria) {
|
|
421
444
|
const collection = await this.getCollection(collectionName);
|
|
422
445
|
try {
|
|
446
|
+
// Ensure to include the _deleted filter in criteria
|
|
447
|
+
criteria._deleted = { $ne: true };
|
|
423
448
|
const cnt = await collection.countDocuments(criteria);
|
|
424
449
|
return { count: cnt };
|
|
425
450
|
}
|