@frogfish/k2db 1.0.7 → 1.0.8
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 +38 -20
- 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
|
@@ -266,6 +266,7 @@ class K2DB {
|
|
|
266
266
|
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
267
267
|
*/
|
|
268
268
|
async updateAll(collectionName, criteria, values, replace = false) {
|
|
269
|
+
// Return type changed to JSON object {updated: number}
|
|
269
270
|
const collection = await this.getCollection(collectionName);
|
|
270
271
|
debug(`Updating ${collectionName} with criteria: ${JSON.stringify(criteria)}`);
|
|
271
272
|
values._updated = Date.now();
|
|
@@ -280,11 +281,8 @@ class K2DB {
|
|
|
280
281
|
const updateOperation = replace ? values : { $set: values };
|
|
281
282
|
try {
|
|
282
283
|
const res = await collection.updateMany(criteria, updateOperation);
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
modified: res.modifiedCount,
|
|
286
|
-
updated: res.acknowledged,
|
|
287
|
-
};
|
|
284
|
+
// Return the updated count in JSON format
|
|
285
|
+
return { updated: res.modifiedCount };
|
|
288
286
|
}
|
|
289
287
|
catch (err) {
|
|
290
288
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update1", this.normalizeError(err));
|
|
@@ -301,22 +299,31 @@ class K2DB {
|
|
|
301
299
|
*/
|
|
302
300
|
async update(collectionName, id, data, replace = false, objectTypeName) {
|
|
303
301
|
const collection = await this.getCollection(collectionName);
|
|
304
|
-
data._updated = Date.now();
|
|
302
|
+
data._updated = Date.now(); // Set the _updated timestamp
|
|
305
303
|
try {
|
|
306
304
|
// Call updateAll with the UUID criteria and replace flag
|
|
307
305
|
const res = await this.updateAll(collectionName, { _uuid: id }, data, replace);
|
|
308
|
-
if
|
|
309
|
-
|
|
306
|
+
// Check if exactly one document was updated
|
|
307
|
+
if (res.updated === 1) {
|
|
308
|
+
return { updated: 1 };
|
|
309
|
+
}
|
|
310
|
+
// If no document was updated, throw a NOT_FOUND error
|
|
311
|
+
if (res.updated === 0) {
|
|
312
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, `Object in ${collectionName} with UUID ${id} not found`, "sys_mdb_update_not_found");
|
|
310
313
|
}
|
|
311
|
-
|
|
312
|
-
|
|
314
|
+
// If more than one document was updated, throw a SYSTEM_ERROR
|
|
315
|
+
if (res.updated > 1) {
|
|
316
|
+
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");
|
|
313
317
|
}
|
|
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
|
|
314
320
|
}
|
|
315
321
|
catch (err) {
|
|
316
322
|
if (err instanceof k2error_1.K2Error) {
|
|
317
323
|
throw err;
|
|
318
324
|
}
|
|
319
|
-
|
|
325
|
+
// Catch any other unhandled errors and throw a system error
|
|
326
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_update_error", this.normalizeError(err));
|
|
320
327
|
}
|
|
321
328
|
}
|
|
322
329
|
/**
|
|
@@ -326,10 +333,9 @@ class K2DB {
|
|
|
326
333
|
*/
|
|
327
334
|
async deleteAll(collectionName, criteria) {
|
|
328
335
|
const collection = await this.getCollection(collectionName);
|
|
329
|
-
let foundCount;
|
|
330
336
|
try {
|
|
331
|
-
// Step 1: Count documents matching the original criteria
|
|
332
|
-
foundCount = await collection.countDocuments(criteria);
|
|
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);
|
|
333
339
|
}
|
|
334
340
|
catch (err) {
|
|
335
341
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_count", this.normalizeError(err));
|
|
@@ -341,7 +347,7 @@ class K2DB {
|
|
|
341
347
|
};
|
|
342
348
|
let result;
|
|
343
349
|
try {
|
|
344
|
-
// Perform the update
|
|
350
|
+
// Perform the update to soft delete the documents
|
|
345
351
|
result = await this.updateAll(collectionName, modifiedCriteria, {
|
|
346
352
|
_deleted: true,
|
|
347
353
|
});
|
|
@@ -349,10 +355,9 @@ class K2DB {
|
|
|
349
355
|
catch (err) {
|
|
350
356
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Error updating ${collectionName}`, "sys_mdb_deleteall_update", this.normalizeError(err));
|
|
351
357
|
}
|
|
352
|
-
// Step 3: Return the
|
|
358
|
+
// Step 3: Return the number of records that were marked as deleted
|
|
353
359
|
return {
|
|
354
|
-
|
|
355
|
-
found: foundCount, // Use the original count here
|
|
360
|
+
deleted: result.updated, // Map the updated count to 'deleted'
|
|
356
361
|
};
|
|
357
362
|
}
|
|
358
363
|
/**
|
|
@@ -362,8 +367,21 @@ class K2DB {
|
|
|
362
367
|
*/
|
|
363
368
|
async delete(collectionName, id) {
|
|
364
369
|
try {
|
|
365
|
-
|
|
366
|
-
|
|
370
|
+
// Call deleteAll to soft delete the document by UUID
|
|
371
|
+
const result = await this.deleteAll(collectionName, { _uuid: id });
|
|
372
|
+
// Check the result of the deleteAll operation
|
|
373
|
+
if (result.deleted === 1) {
|
|
374
|
+
// Successfully deleted one document
|
|
375
|
+
return { deleted: 1 };
|
|
376
|
+
}
|
|
377
|
+
else if (result.deleted === 0) {
|
|
378
|
+
// No document was found to delete
|
|
379
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.NOT_FOUND, "Document not found", "sys_mdb_remove_not_found");
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
// More than one document was deleted, which is unexpected
|
|
383
|
+
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Multiple documents deleted when only one was expected", "sys_mdb_remove_multiple_deleted");
|
|
384
|
+
}
|
|
367
385
|
}
|
|
368
386
|
catch (err) {
|
|
369
387
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Error removing object from collection", "sys_mdb_remove_upd", this.normalizeError(err));
|