monsqlize 2.0.5 → 3.0.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +14 -5
  2. package/MIGRATION.md +105 -0
  3. package/README.md +183 -189
  4. package/SECURITY.md +21 -0
  5. package/changelogs/README.md +16 -4
  6. package/changelogs/v2.0.6.md +16 -0
  7. package/changelogs/v2.0.7.md +63 -0
  8. package/changelogs/v3.0.0.md +97 -0
  9. package/dist/cjs/cli/data-task.cjs +17768 -0
  10. package/dist/cjs/index.cjs +12166 -5505
  11. package/dist/cjs/transaction/Transaction.cjs +151 -19
  12. package/dist/cjs/transaction/TransactionManager.cjs +152 -20
  13. package/dist/esm/index.mjs +12171 -5509
  14. package/dist/types/base.d.mts +81 -0
  15. package/dist/types/collection.d.mts +1156 -0
  16. package/dist/types/collection.d.ts +138 -15
  17. package/dist/types/data-tasks.d.mts +221 -0
  18. package/dist/types/data-tasks.d.ts +221 -0
  19. package/dist/types/expression.d.mts +115 -0
  20. package/dist/types/index.d.mts +24 -0
  21. package/dist/types/index.d.ts +1 -0
  22. package/dist/types/lock.d.mts +77 -0
  23. package/dist/types/lock.d.ts +7 -4
  24. package/dist/types/model.d.mts +666 -0
  25. package/dist/types/model.d.ts +46 -14
  26. package/dist/types/mongodb.d.mts +56 -0
  27. package/dist/types/monsqlize.d.mts +638 -0
  28. package/dist/types/monsqlize.d.ts +130 -16
  29. package/dist/types/pool.d.mts +84 -0
  30. package/dist/types/runtime.d.mts +400 -0
  31. package/dist/types/runtime.d.ts +18 -5
  32. package/dist/types/saga.d.mts +148 -0
  33. package/dist/types/saga.d.ts +11 -6
  34. package/dist/types/slow-query-log.d.mts +126 -0
  35. package/dist/types/sync.d.mts +149 -0
  36. package/dist/types/sync.d.ts +47 -1
  37. package/dist/types/transaction.d.mts +152 -0
  38. package/dist/types/transaction.d.ts +8 -0
  39. package/package.json +32 -14
@@ -1,11 +1,16 @@
1
1
  import type { BookmarkClearResult, BookmarkListResult, BookmarkPrewarmResult, DeleteBatchResult, DeleteResult, IncrementOneResult, IndexCreateResult, InsertBatchResult, InsertManyResult, InsertOneResult, UpdateBatchResult, UpdateResult } from './collection';
2
+ import type { SchemaDslRuntime } from 'schema-dsl/runtime';
3
+
4
+ /**
5
+ * Runtime-scoped schema-dsl namespace passed to Model schema callbacks.
6
+ */
7
+ export type ModelSchemaDsl = SchemaDslRuntime['s'];
2
8
 
3
9
  /**
4
10
  * Schema DSL transformer function.
5
- * Receives a raw DSL descriptor and returns a transformed schema object.
6
11
  * @since v1.0.0
7
12
  */
8
- export type SchemaDSL = (dsl: unknown) => unknown;
13
+ export type SchemaDSL = (s: ModelSchemaDsl) => unknown;
9
14
 
10
15
  /**
11
16
  * Default value for a model field — either a static value or a factory function.
@@ -54,6 +59,8 @@ export interface PopulateConfig {
54
59
  sort?: Record<string, 1 | -1>;
55
60
  limit?: number;
56
61
  skip?: number;
62
+ /** Maximum nested populate depth for this branch. Defaults to 5. */
63
+ maxDepth?: number;
57
64
  populate?: string | PopulateConfig | Array<string | PopulateConfig>;
58
65
  }
59
66
 
@@ -65,10 +72,38 @@ export interface VirtualConfig {
65
72
  export type ModelAutoIndexOptions = boolean | {
66
73
  /** Enable automatic model index creation. Defaults to true for backward compatibility. */
67
74
  enabled?: boolean;
68
- /** Emit `model-index-error` when automatic index creation fails. Defaults to true. */
75
+ /** Emit `model-index-error` when automatic index creation fails or conflicts are detected. Defaults to true. */
69
76
  emitEvents?: boolean;
70
77
  };
71
78
 
79
+ export type ModelVersionUpdateManyMode = 'counter' | 'strict' | 'off';
80
+
81
+ export interface ModelVersionOptions {
82
+ enabled?: boolean;
83
+ field?: string;
84
+ /**
85
+ * Version handling for updateMany on versioned models.
86
+ * - counter: native batch update plus version increment; not optimistic locking.
87
+ * - strict: pre-read IDs/versions and conditionally update each document.
88
+ * - off: skip version handling for this batch update.
89
+ */
90
+ updateMany?: ModelVersionUpdateManyMode;
91
+ }
92
+
93
+ export type ModelWriteOptions = Record<string, unknown> & {
94
+ expectedVersion?: number;
95
+ version?: number;
96
+ };
97
+
98
+ export type ModelUpdateManyOptions = ModelWriteOptions & {
99
+ versionMode?: ModelVersionUpdateManyMode;
100
+ };
101
+
102
+ export type ModelStrictUpdateManyResult = UpdateResult & {
103
+ conflictCount: number;
104
+ conflictedIds: unknown[];
105
+ };
106
+
72
107
  export type ModelIndexSource = 'definition' | 'softDelete';
73
108
 
74
109
  export interface ModelDeclaredIndex {
@@ -200,10 +235,7 @@ export interface ModelDefinitionOptions {
200
235
  type?: string;
201
236
  ttl?: number | null;
202
237
  };
203
- version?: boolean | {
204
- enabled?: boolean;
205
- field?: string;
206
- };
238
+ version?: boolean | ModelVersionOptions;
207
239
  }
208
240
 
209
241
  export interface ModelDefinition<TDocument = Record<string, unknown>> {
@@ -212,7 +244,7 @@ export interface ModelDefinition<TDocument = Record<string, unknown>> {
212
244
  /** Compatibility collection name used by model auto-loading files; `collection` has higher priority. */
213
245
  name?: string;
214
246
  enums?: Record<string, string>;
215
- schema?: ((dsl: unknown) => unknown) | Record<string, unknown>;
247
+ schema?: SchemaDSL | Record<string, unknown>;
216
248
  defaults?: Record<string, unknown | ((context?: unknown, doc?: TDocument) => unknown)>;
217
249
  hooks?:
218
250
  | {
@@ -284,7 +316,7 @@ export interface ModelInstance<TDocument = any> {
284
316
  readonly poolName?: string;
285
317
  readonly definition: ModelDefinition<TDocument>;
286
318
  /** Returns namespace metadata for the current model, including instance ID, type, database, and collection. */
287
- getNamespace(): { iid: string; type: 'mongodb'; db: string; collection: string; };
319
+ getNamespace(): { iid: string; type: 'mongodb'; db: string; collection: string; pool?: string; };
288
320
  /** Returns the relation config map declared by the current model. */
289
321
  getRelations(): Record<string, RelationConfig>;
290
322
  /** Returns the enum value map declared by the current model. */
@@ -379,21 +411,21 @@ export interface ModelInstance<TDocument = any> {
379
411
  * @param update Update operator document, such as `$set` or `$inc`.
380
412
  * @param options Optional update options.
381
413
  */
382
- updateOne(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateResult>;
414
+ updateOne(filter?: unknown, update?: unknown, options?: ModelWriteOptions): Promise<UpdateResult>;
383
415
  /**
384
416
  * Updates all documents matching the filter.
385
417
  * @param filter Filter.
386
418
  * @param update Update operator document.
387
419
  * @param options Optional update options.
388
420
  */
389
- updateMany(filter?: unknown, update?: unknown, options?: unknown): Promise<UpdateResult>;
421
+ updateMany(filter?: unknown, update?: unknown, options?: ModelUpdateManyOptions): Promise<UpdateResult>;
390
422
  /**
391
423
  * Replaces the first document matching the filter with a full replacement document.
392
424
  * @param filter Filter.
393
425
  * @param replacement Full replacement document.
394
426
  * @param options Optional replacement options.
395
427
  */
396
- replaceOne(filter?: unknown, replacement?: unknown, options?: unknown): Promise<UpdateResult>;
428
+ replaceOne(filter?: unknown, replacement?: unknown, options?: ModelWriteOptions): Promise<UpdateResult>;
397
429
  /**
398
430
  * Atomically finds and updates one document.
399
431
  * @param filter Filter.
@@ -401,7 +433,7 @@ export interface ModelInstance<TDocument = any> {
401
433
  * @param options Optional options such as `returnDocument: 'after'`.
402
434
  * @returns Updated document, or `null` when none is found.
403
435
  */
404
- findOneAndUpdate(filter?: unknown, update?: unknown, options?: unknown): Promise<TDocument | null>;
436
+ findOneAndUpdate(filter?: unknown, update?: unknown, options?: ModelWriteOptions): Promise<TDocument | null>;
405
437
  /**
406
438
  * Atomically finds and replaces one document.
407
439
  * @param filter Filter.
@@ -409,7 +441,7 @@ export interface ModelInstance<TDocument = any> {
409
441
  * @param options Optional options.
410
442
  * @returns Replaced document, or `null` when none is found.
411
443
  */
412
- findOneAndReplace(filter?: unknown, replacement?: unknown, options?: unknown): Promise<TDocument | null>;
444
+ findOneAndReplace(filter?: unknown, replacement?: unknown, options?: ModelWriteOptions): Promise<TDocument | null>;
413
445
  /**
414
446
  * Atomically finds and deletes one document.
415
447
  * @param filter Filter.
@@ -0,0 +1,56 @@
1
+ import type { Db, MongoClient, MongoClientOptions } from 'mongodb';
2
+ import type { SSHConfig } from './monsqlize.mjs';
3
+
4
+ export interface MongoConnectConfig {
5
+ uri?: string;
6
+ options?: MongoClientOptions;
7
+ /** v1 compat: read preference shortcut merged into MongoClient options. */
8
+ readPreference?: MongoClientOptions['readPreference'];
9
+ /**
10
+ * v1 compat: when true, automatically starts mongodb-memory-server without requiring a uri.
11
+ * For testing only.
12
+ */
13
+ useMemoryServer?: boolean;
14
+ /** Instance/binary configuration options for mongodb-memory-server. */
15
+ memoryServerOptions?: {
16
+ instance?: {
17
+ port?: number;
18
+ dbName?: string;
19
+ storageEngine?: string;
20
+ replSet?: string;
21
+ dbPath?: string;
22
+ launchTimeout?: number;
23
+ };
24
+ binary?: { version?: string };
25
+ [key: string]: unknown;
26
+ };
27
+ /**
28
+ * SSH tunnel configuration. When provided, monSQLize establishes an SSH port-forward
29
+ * through the specified bastion host before connecting to MongoDB.
30
+ * The remote host/port are auto-parsed from `uri` unless overridden by `remoteHost`/`remotePort`.
31
+ * @since v1.3.0
32
+ */
33
+ ssh?: SSHConfig;
34
+ /**
35
+ * Explicit remote MongoDB host visible from the SSH server.
36
+ * Overrides the host auto-parsed from `uri` when `ssh` is set.
37
+ * @since v1.3.0
38
+ */
39
+ remoteHost?: string;
40
+ /**
41
+ * Explicit remote MongoDB port visible from the SSH server.
42
+ * Overrides the port auto-parsed from `uri` when `ssh` is set.
43
+ * @since v1.3.0
44
+ */
45
+ remotePort?: number;
46
+ /** @alias remoteHost — v1 SSH config field */
47
+ mongoHost?: string;
48
+ /** @alias remotePort — v1 SSH config field */
49
+ mongoPort?: number;
50
+ }
51
+
52
+ export interface MongoConnectionState {
53
+ client: MongoClient;
54
+ db: Db;
55
+ }
56
+