@frogfish/k2db 3.0.2 → 3.0.3

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.
Files changed (3) hide show
  1. package/data.d.ts +28 -5
  2. package/data.js +45 -15
  3. package/package.json +1 -1
package/data.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { K2DB } from "./db.js";
2
- import type { BaseDocument, CreateResult, UpdateResult, DeleteResult, RestoreResult, CountResult, DropResult, VersionedUpdateResult, VersionInfo } from "./db.js";
2
+ import type { BaseDocument, CreateResult, UpdateResult, DeleteResult, RestoreResult, CountResult, DropResult, PurgeResult, PurgeManyResult, VersionedUpdateResult, VersionInfo } from "./db.js";
3
3
  export declare class K2Data {
4
4
  private db;
5
5
  private owner;
@@ -68,9 +68,11 @@ export declare class K2Data {
68
68
  /**
69
69
  * Permanently deletes a document that has been soft-deleted.
70
70
  */
71
- purge(collectionName: string, id: string): Promise<{
72
- id: string;
73
- }>;
71
+ purge(collectionName: string, id: string): Promise<PurgeResult>;
72
+ /**
73
+ * Permanently deletes all soft-deleted documents older than a threshold.
74
+ */
75
+ purgeDeletedOlderThan(collectionName: string, olderThanMs: number): Promise<PurgeManyResult>;
74
76
  /**
75
77
  * Restores a soft-deleted document.
76
78
  */
@@ -83,6 +85,15 @@ export declare class K2Data {
83
85
  * Drops an entire collection.
84
86
  */
85
87
  drop(collectionName: string): Promise<DropResult>;
88
+ /**
89
+ * Ensure commonly needed indexes exist.
90
+ */
91
+ ensureIndexes(collectionName: string, opts?: {
92
+ uuidUnique?: boolean;
93
+ uuidPartialUnique?: boolean;
94
+ ownerIndex?: boolean;
95
+ deletedIndex?: boolean;
96
+ }): Promise<void>;
86
97
  /**
87
98
  * Executes a transaction with the provided operations.
88
99
  */
@@ -95,6 +106,18 @@ export declare class K2Data {
95
106
  * Drops the entire database.
96
107
  */
97
108
  dropDatabase(): Promise<void>;
109
+ /**
110
+ * Releases the MongoDB connection.
111
+ */
112
+ release(): Promise<void>;
113
+ /**
114
+ * Closes the MongoDB connection.
115
+ */
116
+ close(): void;
117
+ /**
118
+ * Validates the MongoDB collection name.
119
+ */
120
+ validateCollectionName(collectionName: string): void;
98
121
  /**
99
122
  * Checks the health of the database connection.
100
123
  */
@@ -102,4 +125,4 @@ export declare class K2Data {
102
125
  }
103
126
  export { K2DB } from "./db.js";
104
127
  export declare const isK2ID: (id: string) => boolean;
105
- export type { DatabaseConfig, BaseDocument, CreateResult, UpdateResult, DeleteResult, RestoreResult, CountResult, DropResult, VersionedUpdateResult, VersionInfo, } from "./db.js";
128
+ export type { DatabaseConfig, BaseDocument, CreateResult, UpdateResult, DeleteResult, RestoreResult, CountResult, DropResult, PurgeResult, PurgeManyResult, VersionedUpdateResult, VersionInfo, } from "./db.js";
package/data.js CHANGED
@@ -12,7 +12,7 @@ export class K2Data {
12
12
  * @param uuid - UUID of the document.
13
13
  */
14
14
  async get(collectionName, uuid) {
15
- return this.db.get(collectionName, uuid);
15
+ return this.db.get(collectionName, uuid, this.owner);
16
16
  }
17
17
  /**
18
18
  * Retrieves a single document matching the criteria.
@@ -21,19 +21,19 @@ export class K2Data {
21
21
  * @param fields - Optional fields to include.
22
22
  */
23
23
  async findOne(collectionName, criteria, fields) {
24
- return this.db.findOne(collectionName, criteria, fields);
24
+ return this.db.findOne(collectionName, criteria, fields, this.owner);
25
25
  }
26
26
  /**
27
27
  * Finds documents based on filter with optional parameters and pagination.
28
28
  */
29
29
  async find(collectionName, filter, params, skip, limit) {
30
- return this.db.find(collectionName, filter, params, skip, limit);
30
+ return this.db.find(collectionName, filter, params, skip, limit, this.owner);
31
31
  }
32
32
  /**
33
33
  * Aggregates documents based on criteria with pagination support.
34
34
  */
35
35
  async aggregate(collectionName, criteria, skip, limit) {
36
- return this.db.aggregate(collectionName, criteria, skip, limit);
36
+ return this.db.aggregate(collectionName, criteria, skip, limit, this.owner);
37
37
  }
38
38
  /**
39
39
  * Creates a new document in the collection.
@@ -46,28 +46,28 @@ export class K2Data {
46
46
  */
47
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, this.owner);
50
50
  }
51
51
  /**
52
52
  * Updates a single document by UUID.
53
53
  */
54
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, this.owner);
57
57
  }
58
58
  /**
59
59
  * Updates a single document by UUID and saves the previous version to history.
60
60
  */
61
61
  async updateVersioned(collectionName, id, data, replace = false, maxVersions) {
62
- return this.db.updateVersioned(collectionName, id, data, replace, maxVersions);
62
+ return this.db.updateVersioned(collectionName, id, data, replace, maxVersions, this.owner);
63
63
  }
64
64
  /** List versions for a document (latest first). */
65
65
  async listVersions(collectionName, id, skip, limit) {
66
- return this.db.listVersions(collectionName, id, skip, limit);
66
+ return this.db.listVersions(collectionName, id, skip, limit, this.owner);
67
67
  }
68
68
  /** Revert a document to a prior version. */
69
69
  async revertToVersion(collectionName, id, version) {
70
- return this.db.revertToVersion(collectionName, id, version);
70
+ return this.db.revertToVersion(collectionName, id, version, this.owner);
71
71
  }
72
72
  /** Ensure history collection indexes exist. */
73
73
  async ensureHistoryIndexes(collectionName) {
@@ -90,37 +90,49 @@ export class K2Data {
90
90
  */
91
91
  async deleteAll(collectionName, criteria) {
92
92
  // Ensure it returns { deleted: number }
93
- return this.db.deleteAll(collectionName, criteria);
93
+ return this.db.deleteAll(collectionName, criteria, this.owner);
94
94
  }
95
95
  /**
96
96
  * Removes (soft deletes) a single document by UUID.
97
97
  */
98
98
  async delete(collectionName, id) {
99
- return this.db.delete(collectionName, id);
99
+ return this.db.delete(collectionName, id, this.owner);
100
100
  }
101
101
  /**
102
102
  * Permanently deletes a document that has been soft-deleted.
103
103
  */
104
104
  async purge(collectionName, id) {
105
- return this.db.purge(collectionName, id);
105
+ return this.db.purge(collectionName, id, this.owner);
106
+ }
107
+ /**
108
+ * Permanently deletes all soft-deleted documents older than a threshold.
109
+ */
110
+ async purgeDeletedOlderThan(collectionName, olderThanMs) {
111
+ return this.db.purgeDeletedOlderThan(collectionName, olderThanMs, this.owner);
106
112
  }
107
113
  /**
108
114
  * Restores a soft-deleted document.
109
115
  */
110
116
  async restore(collectionName, criteria) {
111
- return this.db.restore(collectionName, criteria);
117
+ return this.db.restore(collectionName, criteria, this.owner);
112
118
  }
113
119
  /**
114
120
  * Counts documents based on criteria.
115
121
  */
116
122
  async count(collectionName, criteria) {
117
- return this.db.count(collectionName, criteria);
123
+ return this.db.count(collectionName, criteria, this.owner);
118
124
  }
119
125
  /**
120
126
  * Drops an entire collection.
121
127
  */
122
128
  async drop(collectionName) {
123
- return this.db.drop(collectionName);
129
+ return this.db.drop(collectionName, this.owner);
130
+ }
131
+ /**
132
+ * Ensure commonly needed indexes exist.
133
+ */
134
+ async ensureIndexes(collectionName, opts) {
135
+ return this.db.ensureIndexes(collectionName, opts);
124
136
  }
125
137
  /**
126
138
  * Executes a transaction with the provided operations.
@@ -140,6 +152,24 @@ export class K2Data {
140
152
  async dropDatabase() {
141
153
  return this.db.dropDatabase();
142
154
  }
155
+ /**
156
+ * Releases the MongoDB connection.
157
+ */
158
+ async release() {
159
+ return this.db.release();
160
+ }
161
+ /**
162
+ * Closes the MongoDB connection.
163
+ */
164
+ close() {
165
+ this.db.close();
166
+ }
167
+ /**
168
+ * Validates the MongoDB collection name.
169
+ */
170
+ validateCollectionName(collectionName) {
171
+ return this.db.validateCollectionName(collectionName);
172
+ }
143
173
  /**
144
174
  * Checks the health of the database connection.
145
175
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "A data handling library for K2 applications.",
5
5
  "type": "module",
6
6
  "main": "data.js",