@frogfish/k2db 2.0.7 → 3.0.1

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/dist/README.md DELETED
@@ -1,315 +0,0 @@
1
- # k2db
2
-
3
- Lightweight MongoDB data layer that stays schemaless by default while enforcing consistent metadata and soft-delete.
4
-
5
- Why this, not an ORM?
6
- - Thin driver wrapper: Sits directly atop the official MongoDB driver. No models, decorators, or migrations to manage.
7
- - Loose by default: Store arbitrary data while consistently enforcing `_uuid`, `_owner`, `_created`, `_updated`, `_deleted`.
8
- - Opinionated guardrails: Soft‑delete and metadata are enforced everywhere, including aggregates, without changing your payloads.
9
- - Opt‑in structure: Add Zod schemas per collection only if you want validation/coercion; skip entirely if you don’t.
10
- - Predictable behavior: No hidden population, no query magic, and explicit return types. What you query is what runs.
11
-
12
- What you get
13
- - Concrete API for Mongo: Avoid re‑implementing the same metadata, soft‑delete, and versioning patterns across services. This wrapper centralizes those policies so teams don’t write boilerplate.
14
- - Guardrails without heavy ORM: Prisma/Mongoose add ceremony (models, migrations, plugins) that can be overkill in microservices/serverless. k2db gives you just enough safety with minimal overhead.
15
- - Soft deletes done properly: Automatically enforced everywhere (including aggregates and joins), so you don’t accidentally leak or operate on deleted data.
16
- - Versioning baked in: `updateVersioned`, `listVersions`, and `revertToVersion` provide low‑friction “undo to N levels” that many DALs lack, increasing confidence in production changes.
17
-
18
- Where it fits in the stack
19
- - Below your API/service layer and above the MongoDB driver.
20
- - Use it as a shared data access layer (DAL) across services that need flexible shapes but strict lifecycle rules.
21
- - Keep ownership/authorization in your API; this library only guarantees metadata and deletion semantics.
22
- - Designed for microservices and edge computing: tiny footprint, fast cold starts, and no heavy runtime dependencies.
23
-
24
- Deployment tips (Nomad, Lambda, etc.)
25
- - Environments: Targets Node.js runtimes (Node 18/20). Not suitable for non‑TCP “edge JS” (e.g., Cloudflare Workers) that cannot open Mongo sockets.
26
- - Connection reuse: Create one `K2DB` instance per process and reuse it.
27
- - Lambda: instantiate outside the handler and `await init()` lazily once.
28
- - Nomad: init on service start; reuse for the lifetime of the task.
29
- - Example (AWS Lambda):
30
- ```ts
31
- import { K2DB } from "@frogfish/k2db";
32
- const db = new K2DB(K2DB.fromEnv());
33
- let ready: Promise<void> | null = null;
34
- export const handler = async (event) => {
35
- ready = ready || db.init();
36
- await ready; // reused across warm invocations
37
- const res = await db.find("hello", { _owner: event.userId }, {}, 0, 10);
38
- return { statusCode: 200, body: JSON.stringify(res) };
39
- };
40
- ```
41
- - Pooling and timeouts: The driver manages a small pool by default.
42
- - Serverless: keep `minPoolSize=0` (default), consider `maxIdleTimeMS` to drop idle sockets faster.
43
- - Long‑lived services (Nomad): you can tune pool sizing if needed.
44
- - You can adjust `connectTimeoutMS/serverSelectionTimeoutMS` in the code if your environment needs higher values.
45
- - Networking:
46
- - Atlas from Lambda: prefer VPC + PrivateLink or NAT egress; ensure security groups allow outbound to Atlas.
47
- - Nomad: ensure egress to Atlas/DB, or run Mongo in the same network; set DNS to resolve SRV if using SRV.
48
- - Secrets:
49
- - Lambda: use AWS Secrets Manager/Parameter Store → env vars consumed by `K2DB.fromEnv()`.
50
- - Nomad: pair with HashiCorp Vault templates/env inject; keep credentials out of images.
51
- - Health/readiness:
52
- - Use `db.isHealthy()` in readiness checks (Nomad) and `db.release()` on shutdown. For Lambda there’s no explicit shutdown.
53
- - Bundling:
54
- - ESM friendly; keep dependencies minimal. If you bundle, exclude native modules you don’t use.
55
-
56
- When to pick this
57
- - You want Mongo’s flexibility with light, reliable guardrails (soft delete, timestamps, owner, UUID).
58
- - You’d rather not pull in a heavier ORM (Mongoose/Prisma) and prefer direct control of queries and indexes.
59
- - Your payloads vary by tenant/feature and rigid schemas get in the way, but you still want optional validation.
60
-
61
- When to consider something else
62
- - Rich modeling, relations, and ecosystem plugins → Mongoose.
63
- - Cross‑DB modeling, migrations, and schema‑first DX → Prisma.
64
- - If you already standardized on an ORM and like its trade‑offs, this library aims to stay out of your way.
65
-
66
- Core invariants
67
- - _uuid: unique identifier generated on create (unique among non-deleted by default).
68
- - _created/_updated: timestamps managed by the library.
69
- - _owner: required on create; preserved across updates.
70
- - _deleted: soft-delete flag. All reads exclude deleted by default; writes do not touch deleted docs. Purge hard-deletes only when `_deleted: true`.
71
-
72
- Modernized behavior
73
- - ESM build, TypeScript, NodeNext resolution.
74
- - Soft-delete enforced across find, findOne, count, update, updateAll.
75
- - Aggregate never returns deleted documents (also enforced across `$lookup`, `$unionWith`, `$graphLookup`, `$facet`).
76
- - Reserved fields: user input cannot set keys starting with `_`; the library owns all underscore-prefixed metadata.
77
- - Slow query logging: operations slower than `slowQueryMs` (default 200ms) are logged via `debug("k2:db")`.
78
- - Hooks: optional `beforeQuery(op, details)` and `afterQuery(op, details, durationMs)` for observability.
79
- - Index helper: `ensureIndexes(collection, { uuidPartialUnique: true, ownerIndex: true, deletedIndex: true })`.
80
-
81
- Ownership (`_owner`)
82
- - 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).
83
- - Why it matters: Enables clear data lineage and supports privacy/jurisdiction workflows (GDPR/DSAR: “export all my data”, “delete my data”), audits, and stewardship.
84
- - 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"`.
85
- - Not authorization: The library does not enforce access control based on `_owner`. Apply your authorization rules in the API/service layer using `_owner` as a helpful filter or join key.
86
- - 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”.
87
-
88
- Config
89
- ```ts
90
- import { K2DB } from "@frogfish/k2db";
91
-
92
- const db = new K2DB({
93
- name: "mydb",
94
- hosts: [{ host: "cluster0.example.mongodb.net" }], // SRV if single host without port
95
- user: process.env.DB_USER,
96
- password: process.env.DB_PASS,
97
- slowQueryMs: 300,
98
- hooks: {
99
- beforeQuery: (op, d) => {},
100
- afterQuery: (op, d, ms) => {},
101
- },
102
- });
103
-
104
- await db.init();
105
- await db.ensureIndexes("myCollection");
106
- ```
107
-
108
- Environment loader
109
- ```ts
110
- const conf = K2DB.fromEnv(); // K2DB_NAME, K2DB_HOSTS, K2DB_USER, K2DB_PASSWORD, K2DB_REPLICASET, K2DB_SLOW_MS
111
- ```
112
-
113
- Tips
114
- - Use `restore()` to clear `_deleted`.
115
- - Use `purge()` to hard-delete; only works on soft-deleted docs.
116
- - For aggregates with joins, the library automatically injects non-deleted filters in root and nested pipelines.
117
-
118
- Versioning (optional)
119
- - Per-document history is stored in a sibling collection named `<collection>__history`.
120
- - Use `updateVersioned()` to snapshot the previous state before updating.
121
- - Use `listVersions()` to see available versions and `revertToVersion()` to roll back (preserves metadata like `_uuid`, `_owner`, `_created`).
122
-
123
- Example:
124
- ```ts
125
- // Save previous version and keep up to 20 versions
126
- await db.ensureHistoryIndexes("hello");
127
- await db.updateVersioned("hello", id, { message: "Hello v2" }, false, 20);
128
-
129
- // List latest 5 versions
130
- const versions = await db.listVersions("hello", id, 0, 5);
131
-
132
- // Revert to a specific version
133
- await db.revertToVersion("hello", id, versions[0]._v);
134
- ```
135
-
136
- Further examples:
137
- ```ts
138
- // Versioned replace
139
- await db.updateVersioned("hello", id, { message: "replace payload" }, true);
140
-
141
- // Keep only the most recent prior state (maxVersions = 1)
142
- await db.updateVersioned("hello", id, { message: "v3" }, false, 1);
143
- ```
144
-
145
- **MongoDB Atlas**
146
- - Create a Database User in Atlas and allow your IP under Network Access.
147
- - Find your cluster address (looks like `cluster0.xxxxxx.mongodb.net`).
148
- - Minimal config uses SRV (no port) when a single host is provided.
149
-
150
- Example (direct config):
151
- ```ts
152
- import { K2DB } from "@frogfish/k2db";
153
-
154
- const db = new K2DB({
155
- name: "mydb", // your database name
156
- hosts: [{ host: "cluster0.xxxxxx.mongodb.net" }], // Atlas SRV host
157
- user: process.env.DB_USER, // Atlas DB user
158
- password: process.env.DB_PASS, // Atlas DB password
159
- slowQueryMs: 300,
160
- });
161
-
162
- await db.init();
163
- ```
164
-
165
- Example (env-based):
166
- ```bash
167
- export K2DB_NAME=mydb
168
- export K2DB_HOSTS=cluster0.xxxxxx.mongodb.net
169
- export K2DB_USER=your_user
170
- export K2DB_PASSWORD=your_pass
171
- node hello.mjs
172
- ```
173
- ```ts
174
- // hello.mjs (Node 18+, ESM)
175
- import { K2DB } from "@frogfish/k2db";
176
-
177
- const conf = K2DB.fromEnv();
178
- const db = new K2DB(conf);
179
- await db.init();
180
- ```
181
-
182
- **Hello World**
183
- - Connect to Atlas, insert into `hello` collection, then read it back.
184
-
185
- ```ts
186
- // hello-world.mjs
187
- import { K2DB } from "@frogfish/k2db";
188
-
189
- // Configure via env or inline config
190
- const db = new K2DB({
191
- name: "mydb",
192
- hosts: [{ host: "cluster0.xxxxxx.mongodb.net" }],
193
- user: process.env.DB_USER,
194
- password: process.env.DB_PASS,
195
- });
196
-
197
- await db.init();
198
- await db.ensureIndexes("hello"); // unique _uuid among non-deleted, plus helpful indexes
199
-
200
- // Create a document (owner is required)
201
- const { id } = await db.create("hello", "demo-owner", { message: "Hello, world!" });
202
- console.log("Inserted id:", id);
203
-
204
- // Read it back
205
- const doc = await db.get("hello", id); // excludes soft-deleted by default
206
- console.log("Retrieved:", doc);
207
-
208
- // Soft delete it (optional)
209
- await db.delete("hello", id);
210
-
211
- // Restore it (optional)
212
- await db.restore("hello", { _uuid: id });
213
-
214
- await db.release();
215
- ```
216
-
217
- Notes
218
- - Use Node 18+ (preferably Node 20+) for ESM + JSON imports.
219
- - Atlas SRV requires only the cluster hostname (no port); the client handles TLS and topology.
220
-
221
- Updates
222
- - Patch vs Replace
223
- - `update(collection, id, data)` patches by default using `$set` (fields you pass are updated, others remain).
224
- - `update(collection, id, data, true)` replaces non‑metadata fields (PUT‑like). Metadata (`_uuid`, `_owner`, `_created`, `_updated`, `_deleted`) is preserved.
225
- - Underscore‑prefixed fields in your input are ignored; `_updated` is refreshed automatically.
226
-
227
- Examples:
228
- ```ts
229
- // Patch specific fields
230
- await db.update("hello", id, { message: "patched value" });
231
-
232
- // Replace (preserves metadata, overwrites non‑underscore fields)
233
- await db.update("hello", id, { message: "entire new doc", count: 1 }, true);
234
- ```
235
-
236
- Schemas (optional, Zod)
237
- - You can register a Zod schema per collection at runtime; it validates and (optionally) strips unknown fields on writes. Nothing is stored in DB.
238
- - Modes: `strict` (reject unknown fields), `strip` (remove unknown; default), `passthrough` (allow unknown).
239
-
240
- Example:
241
- ```ts
242
- import { z } from "zod";
243
-
244
- // Define
245
- const Hello = z
246
- .object({
247
- message: z.string(),
248
- count: z.number().int().default(0),
249
- })
250
- .strip(); // default unknown-key behavior
251
-
252
- // Register (in-memory for this instance)
253
- db.setSchema("hello", Hello, { mode: "strip" });
254
-
255
- // On create: full schema validation; on patch: partial validation
256
- await db.create("hello", ownerId, { message: "hey", extra: "ignored" });
257
- await db.update("hello", id, { count: 2 }); // partial OK
258
-
259
- // To clear
260
- db.clearSchema("hello");
261
- ```
262
-
263
- **Type Reference (Cheat Sheet)**
264
- - `BaseDocument`: Core shape enforced by the library; apps may extend.
265
- - `CreateResult`: `{ id: string }`
266
- - `UpdateResult`: `{ updated: number }`
267
- - `DeleteResult`: `{ deleted: number }`
268
- - `RestoreResult`: `{ status: string; modified: number }`
269
- - `CountResult`: `{ count: number }`
270
- - `DropResult`: `{ status: string }`
271
- - `PurgeResult`: `{ id: string }`
272
- - `VersionedUpdateResult`: `{ updated: number; versionSaved: number }`
273
- - `VersionInfo`: `{ _uuid: string; _v: number; _at: number }`
274
-
275
- Returns by method
276
- - `get(collection, id)`: `Promise<BaseDocument>`
277
- - `find(collection, filter, params?, skip?, limit?)`: `Promise<BaseDocument[]>`
278
- - `findOne(collection, criteria, fields?)`: `Promise<BaseDocument|null>`
279
- - `aggregate(collection, pipeline, skip?, limit?)`: `Promise<BaseDocument[]>`
280
- - `create(collection, owner, data)`: `Promise<CreateResult>`
281
- - `update(collection, id, data, replace?)`: `Promise<UpdateResult>`
282
- - `updateAll(collection, criteria, values)`: `Promise<UpdateResult>`
283
- - `delete(collection, id)`: `Promise<DeleteResult>`
284
- - `deleteAll(collection, criteria)`: `Promise<DeleteResult>`
285
- - `purge(collection, id)`: `Promise<PurgeResult>`
286
- - `restore(collection, criteria)`: `Promise<RestoreResult>`
287
- - `count(collection, criteria)`: `Promise<CountResult>`
288
- - `drop(collection)`: `Promise<DropResult>`
289
- - `ensureIndexes(collection, opts?)`: `Promise<void>`
290
- - `ensureHistoryIndexes(collection)`: `Promise<void>`
291
- - `updateVersioned(collection, id, data, replace?, maxVersions?)`: `Promise<VersionedUpdateResult[]>`
292
- - `listVersions(collection, id, skip?, limit?)`: `Promise<VersionInfo[]>`
293
- - `revertToVersion(collection, id, version)`: `Promise<UpdateResult>`
294
- - Zod registry:
295
- - `setSchema(collection, zodSchema, { mode }?)`: `void`
296
- - `clearSchema(collection)`: `void`
297
- - `clearSchemas()`: `void`
298
-
299
- ## UUID
300
-
301
- _uuid = Crockford Base32 encoded UUID V7, Uppercase, with hyphens
302
-
303
- 0J4F2-H6M8Q-7RX4V-9D3TN-8K2WZ
304
-
305
- // Canonical uppercase form with hyphens
306
- const CROCKFORD_ID_REGEX = /^[0-9A-HJKMNP-TV-Z]{5}-[0-9A-HJKMNP-TV-Z]{5}-[0-9A-HJKMNP-TV-Z]{5}-[0-9A-HJKMNP-TV-Z]{5}-[0-9A-HJKMNP-TV-Z]{6}$/;
307
-
308
- // Example usage:
309
- const id = "0J4F2-H6M8Q-7RX4V-9D3TN-8K2WZ";
310
- console.log(CROCKFORD_ID_REGEX.test(id)); // true
311
-
312
- Usage examples:
313
-
314
- import { isK2ID, K2DB } from '@frogfish/k2db'
315
- isK2ID('01HZY2AB-3JKM-4NPQ-5RST-6VWXYZ')
package/dist/package.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "name": "@frogfish/k2db",
3
- "version": "2.0.7",
4
- "description": "A data handling library for K2 applications.",
5
- "type": "module",
6
- "main": "data.js",
7
- "types": "data.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./data.d.ts",
11
- "import": "./data.js"
12
- },
13
- "./db": {
14
- "types": "./db.d.ts",
15
- "import": "./db.js"
16
- }
17
- },
18
- "license": "GPL-3.0-only",
19
- "author": "El'Diablo",
20
- "dependencies": {
21
- "@frogfish/k2error": "^1.0.5",
22
- "debug": "^4.3.7",
23
- "mongodb": "^6.9.0",
24
- "uuid": "^10.0.0",
25
- "zod": "^3.23.8"
26
- },
27
- "peerDependencies": {},
28
- "optionalDependencies": {},
29
- "publishConfig": {
30
- "access": "public"
31
- }
32
- }
File without changes
File without changes