@nxgt/mongo 0.1.0

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.
@@ -0,0 +1,27 @@
1
+ import type { ValidationAction, ValidationLevel } from '../definition/define-collection';
2
+ /** A collection's validation, as `listCollections` reports it in `options`. */
3
+ export interface LiveValidation {
4
+ validator?: Record<string, unknown>;
5
+ validationLevel?: string;
6
+ validationAction?: string;
7
+ }
8
+ /** The validation a definition asks for. `validator` is absent for `off`. */
9
+ export interface WantedValidation {
10
+ validator: Record<string, unknown> | undefined;
11
+ level: ValidationLevel;
12
+ action: ValidationAction;
13
+ }
14
+ /** Has a collection a validator at all? An empty one is no validator. */
15
+ export declare function hasValidator(live: LiveValidation): boolean;
16
+ /**
17
+ * Does the collection already validate the way the definition says?
18
+ *
19
+ * MongoDB reads a `$jsonSchema` back exactly as it was sent, so the two are
20
+ * compared whole. What it does not read back is a validator that was removed:
21
+ * `collMod` with `validator: {}` leaves **no `validator` key at all**, while
22
+ * `validationLevel` and `validationAction` stay behind. And a collection
23
+ * created without one reports `options: {}`, where the level is `strict` and
24
+ * the action `error` by default.
25
+ */
26
+ export declare function validationMatches(wanted: WantedValidation, live: LiveValidation): boolean;
27
+ //# sourceMappingURL=validator-diff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator-diff.d.ts","sourceRoot":"","sources":["../../src/sync/validator-diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,gBAAgB,EAChB,eAAe,EACf,MAAM,iCAAiC,CAAC;AAEzC,+EAA+E;AAC/E,MAAM,WAAW,cAAc;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,6EAA6E;AAC7E,MAAM,WAAW,gBAAgB;IAChC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAC/C,KAAK,EAAE,eAAe,CAAC;IACvB,MAAM,EAAE,gBAAgB,CAAC;CACzB;AAYD,yEAAyE;AACzE,wBAAgB,YAAY,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAE1D;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,gBAAgB,EACxB,IAAI,EAAE,cAAc,GAClB,OAAO,CAQT"}
@@ -0,0 +1,34 @@
1
+ import type { ClientSession, MongoClient, TransactionOptions } from 'mongodb';
2
+ /** What a transaction can be started from: a client, or a session. */
3
+ export type TransactionHost = MongoClient | ClientSession;
4
+ /**
5
+ * Runs `fn` in a transaction: committed when it resolves, aborted when it
6
+ * throws, and a MongoDB error turned into a `DataError` on the way out. The
7
+ * session is the argument, and **every operation inside has to be given it**:
8
+ * MongoDB has no ambient session, so an operation without one runs outside the
9
+ * transaction and is not rolled back. `repository.with(session)` is how a
10
+ * repository takes it.
11
+ *
12
+ * ```ts
13
+ * await withTransaction(client, async (session) => {
14
+ * const team = await teams.with(session).create({ name: 'Core' });
15
+ * await users.with(session).update(userId, { teamId: team._id });
16
+ * });
17
+ * ```
18
+ *
19
+ * Given a session that is already in a transaction, it **joins** it: `fn` runs
20
+ * with that session and nothing is committed until the outer one commits.
21
+ * MongoDB has no savepoints, so an inner failure cannot be rolled back on its
22
+ * own, and starting a second transaction on one session throws
23
+ * `MongoTransactionError`.
24
+ *
25
+ * Two things the driver does that are easy to be surprised by:
26
+ *
27
+ * - it **retries `fn`** from the start on a `TransientTransactionError`, and
28
+ * the commit alone on an `UnknownTransactionCommitResult`, until 120 seconds
29
+ * have passed. `fn` must therefore be safe to run twice.
30
+ * - `session.abortTransaction()` inside `fn` ends the transaction without
31
+ * throwing: `withTransaction` then resolves.
32
+ */
33
+ export declare function withTransaction<T>(host: TransactionHost, fn: (session: ClientSession) => Promise<T>, options?: TransactionOptions): Promise<T>;
34
+ //# sourceMappingURL=with-transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-transaction.d.ts","sourceRoot":"","sources":["../../src/transaction/with-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAG9E,sEAAsE;AACtE,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,aAAa,CAAC;AAO1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACtC,IAAI,EAAE,eAAe,EACrB,EAAE,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,EAC1C,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,CAAC,CAAC,CA0BZ"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@nxgt/mongo",
3
+ "version": "0.1.0",
4
+ "description": "A typed MongoDB collection from a Zod schema: its indexes and $jsonSchema validator synced idempotently, a typed repository with pagination, transactions, optimistic locking, soft delete and its own errors",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "package.json",
13
+ "LICENSE"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "keywords": [
24
+ "mongodb",
25
+ "zod",
26
+ "repository",
27
+ "pagination",
28
+ "jsonschema",
29
+ "typescript"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/softistx/nxgt-data.git",
34
+ "directory": "packages/mongo"
35
+ },
36
+ "publishConfig": {
37
+ "registry": "https://registry.npmjs.org",
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "build": "bun run ../../build.ts",
42
+ "test": "bun test src",
43
+ "typecheck": "tsc --noEmit"
44
+ },
45
+ "nxgt": {
46
+ "entrypoints": [
47
+ "src/index.ts"
48
+ ]
49
+ },
50
+ "devDependencies": {
51
+ "@types/bun": "^1.4.0",
52
+ "mongodb": "7.6.0",
53
+ "mongodb-memory-server-core": "11.2.0",
54
+ "zod": "4.6.5"
55
+ },
56
+ "peerDependencies": {
57
+ "mongodb": ">=7.0.0 <8",
58
+ "typescript": "^6.0.3",
59
+ "zod": ">=4.6.5 <5"
60
+ }
61
+ }