@classytic/mongokit 3.18.0 → 3.19.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.
- package/README.md +66 -0
- package/dist/PaginationEngine-4SjzfBQE.d.mts +103 -0
- package/dist/actions/index.d.mts +1 -1
- package/dist/adapter/index.mjs +1 -1
- package/dist/ai/index.d.mts +1 -1
- package/dist/better-auth/index.mjs +1 -1
- package/dist/create-repository-D5YX0wGs.mjs +29 -0
- package/dist/create-repository-K0yft2ht.d.mts +1663 -0
- package/dist/{index-fi91ycQc.d.mts → index-BgSBwlWu.d.mts} +1 -1
- package/dist/index.d.mts +11 -1665
- package/dist/index.mjs +5 -29
- package/dist/{mongooseToJsonSchema-BdQJU39w.d.mts → mongooseToJsonSchema-Bz47aMfH.d.mts} +1 -1
- package/dist/multi-tenant.plugin-D6lw3BsI.d.mts +487 -0
- package/dist/pagination/PaginationEngine.d.mts +1 -102
- package/dist/plugins/index.d.mts +3 -2
- package/dist/plugins/index.mjs +2 -1
- package/dist/query/primitives/coercion.d.mts +1 -1
- package/dist/query/primitives/indexes.d.mts +1 -1
- package/dist/testkit/index.d.mts +129 -0
- package/dist/testkit/index.mjs +139 -0
- package/dist/{validation-chain.plugin-DJtC0KJ-.mjs → timestamp.plugin-B1A9U-P7.mjs} +1 -1052
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.mjs +2 -2
- package/dist/{validation-chain.plugin-BpMfy18N.d.mts → validation-chain.plugin-CYOPfFGF.d.mts} +2 -486
- package/dist/validation-chain.plugin-DHZbpb4t.mjs +1056 -0
- package/package.json +9 -1
- /package/dist/{adapter-DYEMkLwK.mjs → adapter-BSxhT0Ip.mjs} +0 -0
- /package/dist/{coercion-TRMmR49h.d.mts → coercion-BxxHyLjP.d.mts} +0 -0
- /package/dist/{field-selection-CQW3WBiK.mjs → field-selection-d1qdRiMr.mjs} +0 -0
- /package/dist/{indexes-CgOF7Wjd.d.mts → indexes-3eHBWNZ2.d.mts} +0 -0
- /package/dist/{mongooseToJsonSchema-CBEQmeEa.mjs → mongooseToJsonSchema-ChtXSrLT.mjs} +0 -0
- /package/dist/{types-CU3SEDbD.d.mts → types-BA8oXjFe.d.mts} +0 -0
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as coerceHeuristic, i as coerceFieldValue, n as SchemaPathsLike, o as coerceToType, r as buildFieldTypeMap, s as normalizeMongooseType, t as FieldType } from "../../coercion-
|
|
1
|
+
import { a as coerceHeuristic, i as coerceFieldValue, n as SchemaPathsLike, o as coerceToType, r as buildFieldTypeMap, s as normalizeMongooseType, t as FieldType } from "../../coercion-BxxHyLjP.mjs";
|
|
2
2
|
export { FieldType, SchemaPathsLike, buildFieldTypeMap, coerceFieldValue, coerceHeuristic, coerceToType, normalizeMongooseType };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as SchemaIndexes, r as extractSchemaIndexes, t as IndexableSchema } from "../../indexes-
|
|
1
|
+
import { n as SchemaIndexes, r as extractSchemaIndexes, t as IndexableSchema } from "../../indexes-3eHBWNZ2.mjs";
|
|
2
2
|
export { IndexableSchema, SchemaIndexes, extractSchemaIndexes };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { a as Repository, n as CreateRepositoryConfig } from "../create-repository-K0yft2ht.mjs";
|
|
2
|
+
import { Connection, Model, Schema } from "mongoose";
|
|
3
|
+
import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server";
|
|
4
|
+
|
|
5
|
+
//#region src/testkit/index.d.ts
|
|
6
|
+
interface MongoMemoryOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Boot a single-node replica set instead of a standalone mongod. Required for
|
|
9
|
+
* any code that opens a `session.startTransaction()` — a standalone server
|
|
10
|
+
* rejects it with error 263. Slightly slower to start (~2-3s). Default false.
|
|
11
|
+
*/
|
|
12
|
+
replset?: boolean;
|
|
13
|
+
/** Database name baked into the generated URI. Default `"mongokit-test"`. */
|
|
14
|
+
dbName?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Use this URI instead of spinning an in-memory server (real MongoDB, CI
|
|
17
|
+
* replica set). Falls back to `process.env.MONGODB_URI`. When either is set,
|
|
18
|
+
* `stop()` is a no-op — the caller owns the server's lifecycle.
|
|
19
|
+
*/
|
|
20
|
+
uri?: string;
|
|
21
|
+
}
|
|
22
|
+
interface MongoMemoryHandle {
|
|
23
|
+
/** Connection string for the running (or external) server. */
|
|
24
|
+
uri: string;
|
|
25
|
+
/** The in-memory server; `undefined` when an external `uri` is used. */
|
|
26
|
+
server?: MongoMemoryServer | MongoMemoryReplSet;
|
|
27
|
+
/** Stop the in-memory server. No-op for an external URI. Idempotent. */
|
|
28
|
+
stop(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Start an ephemeral MongoDB (or resolve an external one) and return its URI +
|
|
32
|
+
* a `stop()` teardown. Prefer `createTestConnection` / `createTestRepository`
|
|
33
|
+
* unless you need the server without a mongoose connection.
|
|
34
|
+
*/
|
|
35
|
+
declare function createMongoMemory(options?: MongoMemoryOptions): Promise<MongoMemoryHandle>;
|
|
36
|
+
interface TestConnection {
|
|
37
|
+
/** An isolated mongoose connection (never the global `mongoose.connection`). */
|
|
38
|
+
connection: Connection;
|
|
39
|
+
uri: string;
|
|
40
|
+
/** Empty every collection on this connection — call in `beforeEach`. */
|
|
41
|
+
clear(): Promise<void>;
|
|
42
|
+
/** Close the connection and stop the server. Idempotent. */
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/** Drop every document in every collection on a connection (models preserved). */
|
|
46
|
+
declare function clearCollections(connection: Connection): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Start (or resolve) a server AND open an isolated mongoose connection over it.
|
|
49
|
+
* Each call gets its own connection — model registries are per-connection, so
|
|
50
|
+
* repeated boots in one process never collide.
|
|
51
|
+
*/
|
|
52
|
+
declare function createTestConnection(options?: MongoMemoryOptions): Promise<TestConnection>;
|
|
53
|
+
/**
|
|
54
|
+
* Scoped harness: start a connection, run `fn`, and tear down in a `finally`
|
|
55
|
+
* even if `fn` throws.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* await withMongoMemory(async ({ connection }) => {
|
|
59
|
+
* const repo = createRepository(connection.model("User", userSchema));
|
|
60
|
+
* // ... assertions ...
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare function withMongoMemory<T>(fn: (ctx: TestConnection) => Promise<T> | T, options?: MongoMemoryOptions): Promise<T>;
|
|
65
|
+
interface CreateTestRepositoryOptions<TDoc> extends MongoMemoryOptions {
|
|
66
|
+
/** Model / collection name (unique per test to avoid cross-file collisions). */
|
|
67
|
+
name: string;
|
|
68
|
+
/** Mongoose schema for the model. */
|
|
69
|
+
schema: Schema<TDoc>;
|
|
70
|
+
/** mongokit `createRepository` config — tenant, softDelete, plugins, cache… */
|
|
71
|
+
config?: CreateRepositoryConfig;
|
|
72
|
+
}
|
|
73
|
+
interface TestRepository<TDoc> {
|
|
74
|
+
/** A live mongokit repository over an in-memory collection. */
|
|
75
|
+
repository: Repository<TDoc>;
|
|
76
|
+
/** The underlying mongoose model (bound to the isolated connection). */
|
|
77
|
+
model: Model<TDoc>;
|
|
78
|
+
connection: Connection;
|
|
79
|
+
uri: string;
|
|
80
|
+
/** Empty this repository's collection. */
|
|
81
|
+
clear(): Promise<void>;
|
|
82
|
+
/** Close the connection and stop the server. Idempotent. */
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The fast path for testing a mongokit repository: spins an in-memory server,
|
|
87
|
+
* opens an isolated connection, registers the model on it, builds indexes, and
|
|
88
|
+
* returns a ready `Repository` (plus `clear()` / `close()`).
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* const t = await createTestRepository({ name: "Order", schema: orderSchema,
|
|
92
|
+
* config: { softDelete: true } });
|
|
93
|
+
* await t.repository.create({ total: 10 });
|
|
94
|
+
* await t.close();
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare function createTestRepository<TDoc>(options: CreateTestRepositoryOptions<TDoc>): Promise<TestRepository<TDoc>>;
|
|
98
|
+
/** The context a module receives from the Mongo backend (live connection + URI). */
|
|
99
|
+
interface MongoTestContext {
|
|
100
|
+
connection: Connection;
|
|
101
|
+
uri: string;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A backend usable by `@classytic/arc-testkit`'s `bootModuleApp`. Structurally
|
|
105
|
+
* a `TestBackend<MongoTestContext>` — no import of arc-testkit, so mongokit
|
|
106
|
+
* never depends on it (one-way: arc-testkit consumes this by shape).
|
|
107
|
+
*/
|
|
108
|
+
interface MongoMemoryBackend {
|
|
109
|
+
setup(): Promise<{
|
|
110
|
+
ctx: MongoTestContext;
|
|
111
|
+
teardown: () => Promise<void>;
|
|
112
|
+
}>;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A real in-memory Mongo backend for arc-testkit:
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* import { bootModuleApp } from "@classytic/arc-testkit";
|
|
119
|
+
* import { mongoMemoryBackend } from "@classytic/mongokit/testkit";
|
|
120
|
+
*
|
|
121
|
+
* const t = await bootModuleApp(
|
|
122
|
+
* ({ connection }) => [createAccountingModule({ connection, permissions })],
|
|
123
|
+
* { backend: mongoMemoryBackend() },
|
|
124
|
+
* );
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
declare function mongoMemoryBackend(options?: MongoMemoryOptions): MongoMemoryBackend;
|
|
128
|
+
//#endregion
|
|
129
|
+
export { CreateTestRepositoryOptions, MongoMemoryBackend, MongoMemoryHandle, MongoMemoryOptions, MongoTestContext, TestConnection, TestRepository, clearCollections, createMongoMemory, createTestConnection, createTestRepository, mongoMemoryBackend, withMongoMemory };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { t as createRepository } from "../create-repository-D5YX0wGs.mjs";
|
|
2
|
+
import { createConnection } from "mongoose";
|
|
3
|
+
//#region src/testkit/index.ts
|
|
4
|
+
function idempotentStop(server) {
|
|
5
|
+
let stopped = false;
|
|
6
|
+
return async () => {
|
|
7
|
+
if (stopped) return;
|
|
8
|
+
stopped = true;
|
|
9
|
+
await server.stop().catch(() => void 0);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Start an ephemeral MongoDB (or resolve an external one) and return its URI +
|
|
14
|
+
* a `stop()` teardown. Prefer `createTestConnection` / `createTestRepository`
|
|
15
|
+
* unless you need the server without a mongoose connection.
|
|
16
|
+
*/
|
|
17
|
+
async function createMongoMemory(options = {}) {
|
|
18
|
+
const externalUri = options.uri ?? process.env.MONGODB_URI;
|
|
19
|
+
if (externalUri) return {
|
|
20
|
+
uri: externalUri,
|
|
21
|
+
stop: async () => void 0
|
|
22
|
+
};
|
|
23
|
+
const { MongoMemoryServer, MongoMemoryReplSet } = await import("mongodb-memory-server");
|
|
24
|
+
const dbName = options.dbName ?? "mongokit-test";
|
|
25
|
+
if (options.replset) {
|
|
26
|
+
const rs = await MongoMemoryReplSet.create({ replSet: { count: 1 } });
|
|
27
|
+
return {
|
|
28
|
+
uri: rs.getUri(dbName),
|
|
29
|
+
server: rs,
|
|
30
|
+
stop: idempotentStop(rs)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const server = await MongoMemoryServer.create();
|
|
34
|
+
return {
|
|
35
|
+
uri: server.getUri(dbName),
|
|
36
|
+
server,
|
|
37
|
+
stop: idempotentStop(server)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Drop every document in every collection on a connection (models preserved). */
|
|
41
|
+
async function clearCollections(connection) {
|
|
42
|
+
const collections = await connection.db?.collections();
|
|
43
|
+
if (!collections) return;
|
|
44
|
+
await Promise.all(collections.map((c) => c.deleteMany({})));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Start (or resolve) a server AND open an isolated mongoose connection over it.
|
|
48
|
+
* Each call gets its own connection — model registries are per-connection, so
|
|
49
|
+
* repeated boots in one process never collide.
|
|
50
|
+
*/
|
|
51
|
+
async function createTestConnection(options = {}) {
|
|
52
|
+
const handle = await createMongoMemory(options);
|
|
53
|
+
const connection = await createConnection(handle.uri).asPromise();
|
|
54
|
+
let closed = false;
|
|
55
|
+
return {
|
|
56
|
+
connection,
|
|
57
|
+
uri: handle.uri,
|
|
58
|
+
clear: () => clearCollections(connection),
|
|
59
|
+
close: async () => {
|
|
60
|
+
if (closed) return;
|
|
61
|
+
closed = true;
|
|
62
|
+
await connection.close().catch(() => void 0);
|
|
63
|
+
await handle.stop();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Scoped harness: start a connection, run `fn`, and tear down in a `finally`
|
|
69
|
+
* even if `fn` throws.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* await withMongoMemory(async ({ connection }) => {
|
|
73
|
+
* const repo = createRepository(connection.model("User", userSchema));
|
|
74
|
+
* // ... assertions ...
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
async function withMongoMemory(fn, options = {}) {
|
|
79
|
+
const ctx = await createTestConnection(options);
|
|
80
|
+
try {
|
|
81
|
+
return await fn(ctx);
|
|
82
|
+
} finally {
|
|
83
|
+
await ctx.close();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The fast path for testing a mongokit repository: spins an in-memory server,
|
|
88
|
+
* opens an isolated connection, registers the model on it, builds indexes, and
|
|
89
|
+
* returns a ready `Repository` (plus `clear()` / `close()`).
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* const t = await createTestRepository({ name: "Order", schema: orderSchema,
|
|
93
|
+
* config: { softDelete: true } });
|
|
94
|
+
* await t.repository.create({ total: 10 });
|
|
95
|
+
* await t.close();
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
async function createTestRepository(options) {
|
|
99
|
+
const tc = await createTestConnection(options);
|
|
100
|
+
const model = tc.connection.model(options.name, options.schema);
|
|
101
|
+
await model.init();
|
|
102
|
+
return {
|
|
103
|
+
repository: createRepository(model, options.config),
|
|
104
|
+
model,
|
|
105
|
+
connection: tc.connection,
|
|
106
|
+
uri: tc.uri,
|
|
107
|
+
clear: async () => {
|
|
108
|
+
await model.deleteMany({});
|
|
109
|
+
},
|
|
110
|
+
close: tc.close
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A real in-memory Mongo backend for arc-testkit:
|
|
115
|
+
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { bootModuleApp } from "@classytic/arc-testkit";
|
|
118
|
+
* import { mongoMemoryBackend } from "@classytic/mongokit/testkit";
|
|
119
|
+
*
|
|
120
|
+
* const t = await bootModuleApp(
|
|
121
|
+
* ({ connection }) => [createAccountingModule({ connection, permissions })],
|
|
122
|
+
* { backend: mongoMemoryBackend() },
|
|
123
|
+
* );
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
function mongoMemoryBackend(options = {}) {
|
|
127
|
+
return { async setup() {
|
|
128
|
+
const tc = await createTestConnection(options);
|
|
129
|
+
return {
|
|
130
|
+
ctx: {
|
|
131
|
+
connection: tc.connection,
|
|
132
|
+
uri: tc.uri
|
|
133
|
+
},
|
|
134
|
+
teardown: tc.close
|
|
135
|
+
};
|
|
136
|
+
} };
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { clearCollections, createMongoMemory, createTestConnection, createTestRepository, mongoMemoryBackend, withMongoMemory };
|