@frogfish/k2db 3.0.1 → 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.
- package/README.md +351 -13
- package/data.d.ts +28 -5
- package/data.js +45 -15
- package/db.d.ts +133 -22
- package/db.js +652 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ Deployment tips (Nomad, Lambda, etc.)
|
|
|
35
35
|
export const handler = async (event) => {
|
|
36
36
|
ready = ready || db.init();
|
|
37
37
|
await ready; // reused across warm invocations
|
|
38
|
-
const res = await db.find("hello", {
|
|
38
|
+
const res = await db.find("hello", {}, {}, 0, 10, event.userId);
|
|
39
39
|
return { statusCode: 200, body: JSON.stringify(res) };
|
|
40
40
|
};
|
|
41
41
|
```
|
|
@@ -100,7 +100,8 @@ Ownership (`_owner`)
|
|
|
100
100
|
- Purpose: `_owner` is not a tenant ID nor a technical auth scope. It’s a required, opinionated piece of metadata that records who a document belongs to (the data subject or system principal that created/owns it).
|
|
101
101
|
- Why it matters: Enables clear data lineage and supports privacy/jurisdiction workflows (GDPR/DSAR: “export all my data”, “delete my data”), audits, and stewardship.
|
|
102
102
|
- Typical values: a user’s UUID when a signed-in human creates the record; for automated/system operations use a stable identifier like `"system"`, `"service:mailer"`, or `"migration:2024-09-01"`.
|
|
103
|
-
- Not
|
|
103
|
+
- Not authentication: k2db does not authenticate callers. Your API/service still decides *who* the caller is and whether they are allowed to act.
|
|
104
|
+
- Optional enforcement: k2db can enforce owner scoping when you provide a per-call scope (see “Scope”), and can require scope on all calls when `ownershipMode: "strict"` is enabled.
|
|
104
105
|
- Multi-tenant setups: If you have tenants, keep a distinct `tenantId` (or similar) alongside `_owner`. `_owner` continues to model “who owns this record” rather than “which tenant it belongs to”.
|
|
105
106
|
|
|
106
107
|
Config
|
|
@@ -124,6 +125,343 @@ await db.init();
|
|
|
124
125
|
await db.ensureIndexes("myCollection");
|
|
125
126
|
```
|
|
126
127
|
|
|
128
|
+
### Scope config
|
|
129
|
+
|
|
130
|
+
- `ownershipMode?: "lax" | "strict"` (default: `"lax"`)
|
|
131
|
+
- `"lax"` (default): Passing a scope is optional. If you provide a scope, k2db enforces it as an owner filter; if omitted, behavior is unchanged from historical (no owner enforcement, but still enforces soft-delete).
|
|
132
|
+
- `"strict"`: Scope is *required* on all scopified methods (see “Scope” below). If you omit scope, k2db throws an error. This helps prevent accidental missing owner filters.
|
|
133
|
+
|
|
134
|
+
Example config with strict mode:
|
|
135
|
+
```ts
|
|
136
|
+
const db = new K2DB({
|
|
137
|
+
name: "mydb",
|
|
138
|
+
hosts: [{ host: "cluster0.example.mongodb.net" }],
|
|
139
|
+
user: process.env.DB_USER,
|
|
140
|
+
password: process.env.DB_PASS,
|
|
141
|
+
ownershipMode: "strict",
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Aggregation config
|
|
146
|
+
|
|
147
|
+
- `aggregationMode?: "loose" | "guarded" | "strict"` (default: `"loose"`)
|
|
148
|
+
- `"loose"`: No aggregation pipeline validation (all MongoDB stages permitted).
|
|
149
|
+
- `"guarded"`: Denies `$out`, `$merge`, `$function`, `$accumulator`; requires a positive limit, caps max limit, and adds `maxTimeMS`.
|
|
150
|
+
- `"strict"`: Allows only `$match`, `$project`, `$sort`, `$skip`, `$limit` stages; also requires and caps limit.
|
|
151
|
+
|
|
152
|
+
Example:
|
|
153
|
+
```ts
|
|
154
|
+
const db = new K2DB({
|
|
155
|
+
name: "mydb",
|
|
156
|
+
hosts: [{ host: "cluster0.example.mongodb.net" }],
|
|
157
|
+
aggregationMode: "guarded", // disables dangerous stages, enforces limit
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Scope
|
|
162
|
+
|
|
163
|
+
Scope is an optional per-call “owner filter” that k2db can apply to most data access and mutation methods. It provides a safety guardrail to help prevent missing `_owner` filters, especially in multi-tenant or user-data contexts.
|
|
164
|
+
|
|
165
|
+
**Scope is not authentication or authorization:** It does not decide *who* may act. Your API/service is still responsible for authenticating callers and deciding what scopes/owners they are allowed to access.
|
|
166
|
+
|
|
167
|
+
### Type
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
type Scope = string | "*";
|
|
171
|
+
```
|
|
172
|
+
- If a `string` is provided, only documents with `_owner === scope` are included or affected.
|
|
173
|
+
- If `"*"` is provided, *all* owners are included (no owner filter). This is intended for admin/service-to-service operations—never pass untrusted `"*"` from user input.
|
|
174
|
+
|
|
175
|
+
### Modes
|
|
176
|
+
|
|
177
|
+
- In `ownershipMode: "lax"` (default):
|
|
178
|
+
- If `scope` is omitted: No owner filtering is applied (historical behavior; still enforces soft-delete).
|
|
179
|
+
- If `scope` is provided: Only docs with `_owner === scope` (or all docs if `"*"`).
|
|
180
|
+
- In `ownershipMode: "strict"`:
|
|
181
|
+
- `scope` is *required* for all scopified methods (see below). If not provided, k2db throws an error.
|
|
182
|
+
- Use `"*"` for admin/system/service calls (do not invent a magic owner like `_system`).
|
|
183
|
+
|
|
184
|
+
### Methods that support scope
|
|
185
|
+
|
|
186
|
+
The following methods accept an optional `scope?: Scope | string` as their final argument:
|
|
187
|
+
- `get`
|
|
188
|
+
- `findOne`
|
|
189
|
+
- `find`
|
|
190
|
+
- `count`
|
|
191
|
+
- `update`
|
|
192
|
+
- `updateAll`
|
|
193
|
+
- `delete`
|
|
194
|
+
- `deleteAll`
|
|
195
|
+
- `restore`
|
|
196
|
+
- `purge`
|
|
197
|
+
- `purgeDeletedOlderThan`
|
|
198
|
+
- `drop` and `dropDatabase`: For destructive/admin operations, use `"*"` as scope to indicate you intend to operate without owner restriction.
|
|
199
|
+
|
|
200
|
+
### Examples
|
|
201
|
+
|
|
202
|
+
#### a) User reads own docs
|
|
203
|
+
```ts
|
|
204
|
+
// Only docs owned by this user
|
|
205
|
+
await db.find("posts", {}, {}, 0, 20, userId);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### b) Admin/service reads or mutates all docs
|
|
209
|
+
```ts
|
|
210
|
+
// Read all posts (no owner restriction)
|
|
211
|
+
await db.find("posts", {}, {}, 0, 20, "*");
|
|
212
|
+
|
|
213
|
+
// Delete a user (admin only)
|
|
214
|
+
await db.delete("users", id, "*");
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### c) Strict mode with HTTP header mapping
|
|
218
|
+
Suppose your API receives:
|
|
219
|
+
```
|
|
220
|
+
X-Scope: <userId> | *
|
|
221
|
+
```
|
|
222
|
+
You should map this header to the `scope` argument:
|
|
223
|
+
```ts
|
|
224
|
+
const scope = req.headers["x-scope"];
|
|
225
|
+
// Only allow "*" for trusted admin/service credentials!
|
|
226
|
+
await db.find("posts", {}, {}, 0, 20, scope);
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Rules of thumb
|
|
230
|
+
- Never pass untrusted `"*"` as a scope—restrict this to trusted admin/service credentials only.
|
|
231
|
+
- Prefer passing a scope everywhere; enabling strict mode helps you catch missing owner filters.
|
|
232
|
+
- Normalize `_owner` values consistently—prefer lowercase and a single convention (e.g., always userId as lowercase UUID).
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
## Aggregation
|
|
236
|
+
|
|
237
|
+
Aggregation in k2db lets you run MongoDB pipelines with guardrails for soft-delete, secure fields, and pipeline safety.
|
|
238
|
+
|
|
239
|
+
### `aggregate(collection, pipeline, skip?, limit?)`
|
|
240
|
+
|
|
241
|
+
Runs an aggregation pipeline on the given collection, returning an array of documents. k2db automatically injects filters and enforces restrictions for safety and consistency.
|
|
242
|
+
|
|
243
|
+
#### What k2db enforces automatically
|
|
244
|
+
|
|
245
|
+
- **Soft-delete enforcement:** Automatically inserts a `$match: { _deleted: { $ne: true } }` stage near the start of your pipeline so only non-deleted documents are returned. For pipelines beginning with `$search`, `$geoNear`, or `$vectorSearch`, the filter is injected after the first stage to avoid breaking those operators.
|
|
246
|
+
- **Nested enforcement:** For `$lookup`, `$unionWith`, `$graphLookup`, and `$facet`, any sub-pipeline is rewritten to ensure the non-deleted filter applies to foreign collections as well. Simple `$lookup` with `localField`/`foreignField` is rewritten to pipeline form so the non-deleted filter can be injected.
|
|
247
|
+
- **Pagination:** If you pass `skip`/`limit`, those are appended to the pipeline. In `"guarded"`/`"strict"` aggregationMode, a positive limit is required and capped to a safe maximum, and `maxTimeMS` is set to prevent long-running queries.
|
|
248
|
+
- **Secure fields:** If `secureFieldPrefixes` is configured (e.g. `["#"]`), any pipeline referencing a secure-prefixed field (such as `"#passport_number"`) is rejected, even in expressions. Returned documents are also stripped of any keys beginning with a secure prefix.
|
|
249
|
+
|
|
250
|
+
#### What you cannot do (by default)
|
|
251
|
+
|
|
252
|
+
- **Return soft-deleted documents:** The injected `$match: { _deleted: { $ne: true } }` filter means aggregate will never return soft-deleted docs, regardless of your pipeline.
|
|
253
|
+
- **Use secure fields in aggregate:** Any pipeline referencing a field like `"#passport_number"` (including in `$project`, `$addFields`, or expressions) is rejected with an error. Even if a document contains a secure-prefixed field, it is stripped from the output.
|
|
254
|
+
- **Use dangerous or non-allowlisted stages:** In `"guarded"` mode, you cannot use `$out`, `$merge`, `$function`, or `$accumulator`. In `"strict"` mode, only `$match`, `$project`, `$sort`, `$skip`, and `$limit` are allowed.
|
|
255
|
+
|
|
256
|
+
#### Filtering level: root vs nested pipelines
|
|
257
|
+
|
|
258
|
+
- The root pipeline always gets the non-deleted filter injected (unless the first stage is `$search`, `$geoNear`, or `$vectorSearch`, in which case it's injected after).
|
|
259
|
+
- For nested pipelines in `$lookup`, `$unionWith`, `$graphLookup`, and `$facet`, k2db rewrites or injects the non-deleted filter into each sub-pipeline, so deleted foreign documents are excluded.
|
|
260
|
+
- If a `$lookup` uses the simple `localField`/`foreignField` form, k2db rewrites it to a pipeline `$lookup` so filtering can be enforced.
|
|
261
|
+
|
|
262
|
+
#### Examples
|
|
263
|
+
|
|
264
|
+
**1) Basic aggregation: injected soft-delete filter**
|
|
265
|
+
|
|
266
|
+
Suppose you call:
|
|
267
|
+
```js
|
|
268
|
+
const pipeline = [
|
|
269
|
+
{ $match: { status: "active" } },
|
|
270
|
+
{ $project: { name: 1, status: 1 } }
|
|
271
|
+
];
|
|
272
|
+
await db.aggregate("users", pipeline);
|
|
273
|
+
```
|
|
274
|
+
**Effective pipeline after k2db injection:**
|
|
275
|
+
```js
|
|
276
|
+
[
|
|
277
|
+
{ $match: { _deleted: { $ne: true } } }, // injected
|
|
278
|
+
{ $match: { status: "active" } },
|
|
279
|
+
{ $project: { name: 1, status: 1 } }
|
|
280
|
+
]
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
**2) `$lookup` rewritten for non-deleted foreign docs**
|
|
284
|
+
|
|
285
|
+
Original pipeline:
|
|
286
|
+
```js
|
|
287
|
+
[
|
|
288
|
+
{
|
|
289
|
+
$lookup: {
|
|
290
|
+
from: "orders",
|
|
291
|
+
localField: "_uuid",
|
|
292
|
+
foreignField: "user_id",
|
|
293
|
+
as: "orders"
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
]
|
|
297
|
+
```
|
|
298
|
+
k2db rewrites this to:
|
|
299
|
+
```js
|
|
300
|
+
[
|
|
301
|
+
{
|
|
302
|
+
$lookup: {
|
|
303
|
+
from: "orders",
|
|
304
|
+
let: { local_id: "$_uuid" },
|
|
305
|
+
pipeline: [
|
|
306
|
+
{ $match: { _deleted: { $ne: true } } }, // injected for foreign docs
|
|
307
|
+
{ $match: { $expr: { $eq: ["$user_id", "$$local_id"] } } }
|
|
308
|
+
],
|
|
309
|
+
as: "orders"
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
**3) Secure field reference is rejected**
|
|
316
|
+
|
|
317
|
+
Pipeline:
|
|
318
|
+
```js
|
|
319
|
+
[
|
|
320
|
+
{ $project: { name: 1, passport: "$#passport_number" } }
|
|
321
|
+
]
|
|
322
|
+
```
|
|
323
|
+
Result: **Throws an error** — referencing a secure-prefixed field (`"#passport_number"`) is not allowed in aggregate pipelines.
|
|
324
|
+
|
|
325
|
+
**4) Attempting to aggregate deleted docs returns nothing**
|
|
326
|
+
|
|
327
|
+
Pipeline:
|
|
328
|
+
```js
|
|
329
|
+
[
|
|
330
|
+
{ $match: { _deleted: true } }
|
|
331
|
+
]
|
|
332
|
+
```
|
|
333
|
+
Result: Returns **no documents** — k2db injects `{ _deleted: { $ne: true } }` before your match, so the result set is always empty.
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
> **Note:** k2db's aggregation guardrails ensure you cannot accidentally leak deleted or secure data, or run dangerous stages in stricter modes.
|
|
338
|
+
|
|
339
|
+
## Secure fields and encryption at rest
|
|
340
|
+
|
|
341
|
+
k2db supports **secure fields**: fields whose keys start with a configurable prefix (recommended: `#`). Secure fields are designed to be hard to accidentally leak in bulk queries and hard to casually access in code.
|
|
342
|
+
|
|
343
|
+
This feature has two layers:
|
|
344
|
+
|
|
345
|
+
1) **Guardrails (always):** Secure fields are stripped from multi-record reads (`find`, `aggregate`) and cannot be explicitly projected. Aggregation pipelines are also rejected if they reference secure fields.
|
|
346
|
+
2) **Encryption at rest (optional):** When enabled, secure-field values are encrypted before being written to MongoDB and decrypted on single-record reads.
|
|
347
|
+
|
|
348
|
+
### Why `#` (friction) instead of `__somethingNice`
|
|
349
|
+
|
|
350
|
+
The goal is to make secure fields “visually wrong” and **ergonomically annoying** to access on purpose:
|
|
351
|
+
|
|
352
|
+
- `doc["#passport_number"]` is explicit and reviewable.
|
|
353
|
+
- `doc.#passport_number` is impossible (forces bracket access).
|
|
354
|
+
- It discourages lazy DTO copying and casual destructuring that can accidentally leak secrets.
|
|
355
|
+
|
|
356
|
+
Using `__private` looks “normal”, which increases the chance developers will treat it like any other field and accidentally return it in list endpoints.
|
|
357
|
+
|
|
358
|
+
Also, `_...` is reserved for k2db’s metadata (`_uuid`, `_owner`, `_created`, …), so `#...` cleanly avoids that namespace.
|
|
359
|
+
|
|
360
|
+
### Configuration
|
|
361
|
+
|
|
362
|
+
Enable secure fields by setting `secureFieldPrefixes`. To also encrypt them at rest, provide a base64 AES-256 key and a key id.
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
const db = new K2DB({
|
|
366
|
+
name: "mydb",
|
|
367
|
+
hosts: [{ host: "cluster0.example.mongodb.net" }],
|
|
368
|
+
|
|
369
|
+
// Treat "#..." fields as secure
|
|
370
|
+
secureFieldPrefixes: ["#"],
|
|
371
|
+
|
|
372
|
+
// Optional encryption-at-rest for secure fields:
|
|
373
|
+
// - must decode to 32 bytes (AES-256)
|
|
374
|
+
// - values are stored as "<keyid>:<payload>"
|
|
375
|
+
secureFieldEncryptionKeyId: "k1",
|
|
376
|
+
secureFieldEncryptionKey: process.env.K2DB_SECURE_KEY_B64,
|
|
377
|
+
});
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Key requirements:
|
|
381
|
+
- `secureFieldEncryptionKey` must be **base64** and decode to **32 bytes**.
|
|
382
|
+
- Encryption is enabled only when **both** `secureFieldEncryptionKey` and `secureFieldEncryptionKeyId` are provided.
|
|
383
|
+
|
|
384
|
+
> Note: Today k2db decrypts only ciphertexts whose `keyid` matches the configured `secureFieldEncryptionKeyId`. If the stored `keyid` differs, the encrypted string is returned as-is (useful during key rotation rollout, but plan your rotation strategy accordingly).
|
|
385
|
+
|
|
386
|
+
### Storage format
|
|
387
|
+
|
|
388
|
+
When encryption is enabled, each secure field value is stored as a single string:
|
|
389
|
+
|
|
390
|
+
```
|
|
391
|
+
<keyid>:<ivB64>.<tagB64>.<ctB64>
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
- Algorithm: AES-256-GCM
|
|
395
|
+
- Plaintext: `JSON.stringify(value)` (so secure fields may be strings, numbers, objects, arrays, etc.)
|
|
396
|
+
|
|
397
|
+
### Behavior by method
|
|
398
|
+
|
|
399
|
+
With `secureFieldPrefixes: ["#"]`:
|
|
400
|
+
|
|
401
|
+
- **create / update / updateAll**:
|
|
402
|
+
- If encryption is enabled, `#...` values are encrypted before write.
|
|
403
|
+
- If encryption is disabled, values are stored as provided.
|
|
404
|
+
- **get / findOne** (single-record reads):
|
|
405
|
+
- If encryption is enabled, `#...` values are decrypted and returned in the object.
|
|
406
|
+
- If encryption is disabled, values are returned as stored.
|
|
407
|
+
- **find** (multi-record reads):
|
|
408
|
+
- Secure fields are **stripped** from every returned document (even if encryption is disabled).
|
|
409
|
+
- **aggregate**:
|
|
410
|
+
- Secure fields are **not allowed to be referenced** in the pipeline (k2db throws).
|
|
411
|
+
- Secure fields are also **stripped** from returned documents as a second safety net.
|
|
412
|
+
- **projections**:
|
|
413
|
+
- `findOne(fields=[...])` and `find(params.filter=[...])` cannot include secure fields (throws).
|
|
414
|
+
|
|
415
|
+
### Examples
|
|
416
|
+
|
|
417
|
+
#### 1) Store a secure field
|
|
418
|
+
|
|
419
|
+
```ts
|
|
420
|
+
const owner = userId.toLowerCase();
|
|
421
|
+
|
|
422
|
+
const { id } = await db.create("profiles", owner, {
|
|
423
|
+
name: "Ada",
|
|
424
|
+
"#passport_number": "123456789",
|
|
425
|
+
"#home_address": { line1: "1 Example St", city: "Perth" },
|
|
426
|
+
});
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
- If encryption is enabled, MongoDB stores `"#passport_number"` and `"#home_address"` as encrypted strings.
|
|
430
|
+
- If encryption is disabled, they are stored as plaintext (still guarded on reads).
|
|
431
|
+
|
|
432
|
+
#### 2) Single-record read returns secure fields
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
const profile = await db.get("profiles", id, owner);
|
|
436
|
+
|
|
437
|
+
// Explicit access (friction by design)
|
|
438
|
+
console.log(profile["#passport_number"]);
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
#### 3) Multi-record read strips secure fields
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
const list = await db.find("profiles", {}, {}, 0, 50, owner);
|
|
445
|
+
|
|
446
|
+
// "#..." fields are removed from each document in list
|
|
447
|
+
console.log(list[0]["#passport_number"]); // undefined
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
#### 4) Aggregation cannot reference secure fields
|
|
451
|
+
|
|
452
|
+
```ts
|
|
453
|
+
await db.aggregate("profiles", [
|
|
454
|
+
{ $project: { name: 1, passport: "$#passport_number" } },
|
|
455
|
+
]);
|
|
456
|
+
// throws: secure-prefixed field referenced in pipeline
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### What this is (and isn’t)
|
|
460
|
+
|
|
461
|
+
- This is a **data safety guardrail** and optional encryption-at-rest mechanism.
|
|
462
|
+
- It is **not authentication/authorization**: you still must decide who the caller is and what they’re allowed to do.
|
|
463
|
+
- For admin/service operations, combine this with **Scope** rules: do not accept `"*"` from untrusted callers.
|
|
464
|
+
|
|
127
465
|
Environment loader
|
|
128
466
|
```ts
|
|
129
467
|
const conf = K2DB.fromEnv(); // K2DB_NAME (logical db), K2DB_HOSTS, K2DB_USER, K2DB_PASSWORD, K2DB_AUTH_SOURCE, K2DB_REPLICASET, K2DB_SLOW_MS
|
|
@@ -305,19 +643,19 @@ db.clearSchema("hello");
|
|
|
305
643
|
- `VersionInfo`: `{ _uuid: string; _v: number; _at: number }`
|
|
306
644
|
|
|
307
645
|
Returns by method
|
|
308
|
-
- `get(collection, id)`: `Promise<BaseDocument>`
|
|
309
|
-
- `find(collection, filter, params?, skip?, limit?)`: `Promise<BaseDocument[]>`
|
|
310
|
-
- `findOne(collection, criteria, fields?)`: `Promise<BaseDocument|null>`
|
|
646
|
+
- `get(collection, id, scope?)`: `Promise<BaseDocument>`
|
|
647
|
+
- `find(collection, filter, params?, skip?, limit?, scope?)`: `Promise<BaseDocument[]>`
|
|
648
|
+
- `findOne(collection, criteria, fields?, scope?)`: `Promise<BaseDocument|null>`
|
|
311
649
|
- `aggregate(collection, pipeline, skip?, limit?)`: `Promise<BaseDocument[]>`
|
|
312
650
|
- `create(collection, owner, data)`: `Promise<CreateResult>`
|
|
313
|
-
- `update(collection, id, data, replace?)`: `Promise<UpdateResult>`
|
|
314
|
-
- `updateAll(collection, criteria, values)`: `Promise<UpdateResult>`
|
|
315
|
-
- `delete(collection, id)`: `Promise<DeleteResult>`
|
|
316
|
-
- `deleteAll(collection, criteria)`: `Promise<DeleteResult>`
|
|
317
|
-
- `purge(collection, id)`: `Promise<PurgeResult>`
|
|
318
|
-
- `restore(collection, criteria)`: `Promise<RestoreResult>`
|
|
319
|
-
- `count(collection, criteria)`: `Promise<CountResult>`
|
|
320
|
-
- `drop(collection)`: `Promise<DropResult>`
|
|
651
|
+
- `update(collection, id, data, replace?, scope?)`: `Promise<UpdateResult>`
|
|
652
|
+
- `updateAll(collection, criteria, values, scope?)`: `Promise<UpdateResult>`
|
|
653
|
+
- `delete(collection, id, scope?)`: `Promise<DeleteResult>`
|
|
654
|
+
- `deleteAll(collection, criteria, scope?)`: `Promise<DeleteResult>`
|
|
655
|
+
- `purge(collection, id, scope?)`: `Promise<PurgeResult>`
|
|
656
|
+
- `restore(collection, criteria, scope?)`: `Promise<RestoreResult>`
|
|
657
|
+
- `count(collection, criteria, scope?)`: `Promise<CountResult>`
|
|
658
|
+
- `drop(collection, scope?)`: `Promise<DropResult>`
|
|
321
659
|
- `ensureIndexes(collection, opts?)`: `Promise<void>`
|
|
322
660
|
- `ensureHistoryIndexes(collection)`: `Promise<void>`
|
|
323
661
|
- `updateVersioned(collection, id, data, replace?, maxVersions?)`: `Promise<VersionedUpdateResult[]>`
|
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
|
-
|
|
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
|
*/
|