@nxgt/mongo-kit 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,92 @@
1
+ import type { ActorOf, SyncOptions, SyncReport, TypedCollection } from '@nxgt/mongo';
2
+ import type { ClientSession, Db, MongoClient, TransactionOptions } from 'mongodb';
3
+ import type { CollectionsIn, CollectionsOf, DbName, KitConfig } from '../config/types';
4
+ /**
5
+ * A database with its collections on it: `db.users` is the typed collection,
6
+ * and everything the driver's `Db` answers to is still there.
7
+ */
8
+ export type DbScope<C> = {
9
+ readonly [K in keyof CollectionsOf<C>]: TypedCollection<CollectionsOf<C>[K]>;
10
+ } & Db;
11
+ /** Every definition the kit wires, whichever database it belongs to. */
12
+ type WiredDefinition<C> = {
13
+ [N in DbName<C>]: CollectionsOf<CollectionsIn<C, N>>[keyof CollectionsOf<CollectionsIn<C, N>>];
14
+ }[DbName<C>];
15
+ type UnionToIntersection<U> = (U extends unknown ? (u: U) => void : never) extends (i: infer I) => void ? I : never;
16
+ /**
17
+ * Who a kit writes as: the actor every collection that stamps one agrees on.
18
+ * A collection with no actor asks for nothing, and a kit whose collections
19
+ * all stamp none has no `as` to call. Two collections whose actors are of
20
+ * different types agree on nothing, so `as` cannot be called either — which
21
+ * is the honest answer, since one call could not stamp both.
22
+ */
23
+ export type KitActor<C> = [ActorOf<WiredDefinition<C>>] extends [never] ? never : UnionToIntersection<ActorOf<WiredDefinition<C>>>;
24
+ /**
25
+ * The scope of a kit that has exactly one database, and `never` for a kit
26
+ * that has several: with two, `kit.databases.main` is the one that says which.
27
+ */
28
+ export type SoleScope<C> = DbName<C> extends infer N extends DbName<C> ? [Exclude<DbName<C>, N>] extends [never] ? DbScope<CollectionsIn<C, N>> : never : never;
29
+ /** A transaction's options, and which database's client carries it. */
30
+ export type KitTransactionOptions<C> = TransactionOptions & {
31
+ /** Required once the kit holds more than one client. */
32
+ on?: DbName<C>;
33
+ };
34
+ /**
35
+ * An application's MongoDB, wired: the collections on their database, the
36
+ * clients, and what a request adds to them.
37
+ */
38
+ export interface MongoKit<C> extends AsyncDisposable {
39
+ /**
40
+ * The only database's scope. `never` when the kit has several, where
41
+ * reading it anyway — from JavaScript, or across an `any` — throws.
42
+ */
43
+ readonly db: SoleScope<C>;
44
+ /**
45
+ * Every database's scope, under the name the config gave it, or `default`
46
+ * when it named none.
47
+ */
48
+ readonly databases: {
49
+ readonly [N in DbName<C>]: DbScope<CollectionsIn<C, N>>;
50
+ };
51
+ /**
52
+ * The client of each database, under the same names; two databases on one
53
+ * URI share one, and must then be configured with the same `clientOptions`.
54
+ */
55
+ readonly clients: {
56
+ readonly [N in DbName<C>]: MongoClient;
57
+ };
58
+ /** What this kit stamps into the `*By` fields, if any. */
59
+ readonly actor: KitActor<C> | undefined;
60
+ /** The session every collection of this kit runs in, if any. */
61
+ readonly session: ClientSession | undefined;
62
+ /** The same kit, stamping this actor: one call for every collection. */
63
+ as(actor: KitActor<C>): MongoKit<C>;
64
+ /** The same kit, running in this session; `undefined` takes it away. */
65
+ withSession(session: ClientSession | undefined): MongoKit<C>;
66
+ /**
67
+ * Runs `fn` in a transaction, with a kit whose collections are all in it.
68
+ *
69
+ * The driver **retries `fn` from the start** on a transient error, so it
70
+ * must be safe to run twice. A transaction lives on one client: `on` says
71
+ * which database's, is required once the kit holds more than one, and only
72
+ * that client's databases can be used inside — the driver refuses a
73
+ * session another client owns. A call inside a transaction joins it, and
74
+ * takes no `on`.
75
+ */
76
+ transaction<T>(fn: (kit: MongoKit<C>) => Promise<T>, options?: KitTransactionOptions<C>): Promise<T>;
77
+ /**
78
+ * Syncs the wired collections, database by database, and reports each one
79
+ * under its name. The first database that throws stops the rest, so
80
+ * `dryRun` is the way to see everything at once.
81
+ */
82
+ sync(options?: SyncOptions): Promise<Record<DbName<C>, SyncReport[]>>;
83
+ /**
84
+ * Closes what this kit opened. Idempotent; a client the config gave is
85
+ * left alone, and only the kit `createKit` returned may be closed.
86
+ */
87
+ close(): Promise<void>;
88
+ }
89
+ /** What `createKit` takes: a config `defineConfig` checked. */
90
+ export type KitOf<Config> = Config extends KitConfig<infer C> ? MongoKit<C> : never;
91
+ export {};
92
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/kit/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,OAAO,EACP,WAAW,EACX,UAAU,EACV,eAAe,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACX,aAAa,EACb,EAAE,EACF,WAAW,EACX,kBAAkB,EAClB,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EACX,aAAa,EACb,aAAa,EACb,MAAM,EACN,SAAS,EACT,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;IACxB,QAAQ,EAAE,CAAC,IAAI,MAAM,aAAa,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC5E,GAAG,EAAE,CAAC;AAEP,wEAAwE;AACxE,KAAK,eAAe,CAAC,CAAC,IAAI;KACxB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,aAAa,CACvE,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CACnB,CAAC;CACF,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEb,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAC7B,CAAC,SAAS,OAAO,GACd,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GACd,KAAK,CACR,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,GAC3B,CAAC,GACD,KAAK,CAAC;AAET;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,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S,CAAC;IAE5C,wEAAwE;IACxE,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IACpC,wEAAwE;IACxE,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC7D;;;;;;;;;OASG;IACH,WAAW,CAAC,CAAC,EACZ,EAAE,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EACpC,OAAO,CAAC,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAChC,OAAO,CAAC,CAAC,CAAC,CAAC;IACd;;;;OAIG;IACH,IAAI,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;IACtE;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAED,+DAA+D;AAC/D,MAAM,MAAM,KAAK,CAAC,MAAM,IACvB,MAAM,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@nxgt/mongo-kit",
3
+ "version": "0.1.0",
4
+ "description": "An application's MongoDB wiring in one object: a validated config, the clients it opens from it, and every collection typed on the database it lives in",
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
+ "typescript",
26
+ "collections",
27
+ "configuration",
28
+ "transactions"
29
+ ],
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/softistx/nxgt-data.git",
33
+ "directory": "packages/mongo-kit"
34
+ },
35
+ "publishConfig": {
36
+ "registry": "https://registry.npmjs.org",
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "build": "bun run ../../build.ts",
41
+ "test": "bun test src",
42
+ "typecheck": "tsc --noEmit"
43
+ },
44
+ "nxgt": {
45
+ "entrypoints": [
46
+ "src/index.ts"
47
+ ]
48
+ },
49
+ "devDependencies": {
50
+ "@nxgt/mongo": "^0.11.0",
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
+ "@nxgt/mongo": "^0.11.0",
58
+ "mongodb": ">=7.0.0 <8",
59
+ "typescript": "^6.0.3"
60
+ }
61
+ }