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,4 +1,4 @@
1
- import type { ChangeStream, Document, FindOptions, Sort } from 'mongodb';
1
+ import type { ChangeStream, Document, FindOptions as MongoFindOptions, Sort } from 'mongodb';
2
2
 
3
3
  /** Meta options for controlling timing/cache info in query results. */
4
4
  export interface MetaOptions {
@@ -8,6 +8,105 @@ export interface MetaOptions {
8
8
  includeCache?: boolean;
9
9
  }
10
10
 
11
+ export type CacheInvalidationOperation =
12
+ | 'find'
13
+ | 'findOne'
14
+ | 'count'
15
+ | 'findPage'
16
+ | 'aggregate'
17
+ | 'distinct'
18
+ | 'findOneById'
19
+ | 'findByIds'
20
+ | 'all';
21
+
22
+ export type CacheInvalidationEntry =
23
+ | string
24
+ | {
25
+ /** Exact cache key to delete. */
26
+ cacheKey?: string;
27
+ /** Exact cache keys to delete. */
28
+ cacheKeys?: string[];
29
+ /** Cache pattern to delete with delPattern(). */
30
+ pattern?: string;
31
+ /** Cache patterns to delete with delPattern(). */
32
+ patterns?: string[];
33
+ /** Read operation to invalidate. Add query/options for exact key invalidation. */
34
+ operation?: CacheInvalidationOperation | string;
35
+ /** Alias for operation. */
36
+ op?: CacheInvalidationOperation | string;
37
+ query?: unknown;
38
+ options?: unknown;
39
+ pipeline?: unknown[];
40
+ /** Field name for distinct invalidation descriptors. */
41
+ field?: string;
42
+ /** ID for findOneById invalidation descriptors. */
43
+ id?: unknown;
44
+ /** IDs for findByIds invalidation descriptors. */
45
+ ids?: unknown[];
46
+ };
47
+
48
+ export type CacheInvalidationRequest = CacheInvalidationEntry | CacheInvalidationEntry[];
49
+
50
+ export interface WriteCacheControlOptions {
51
+ cache?: {
52
+ /**
53
+ * Explicit invalidation for this write. `false` or `[]` means no invalidation for
54
+ * this write, even when instance-level cache.autoInvalidate is enabled.
55
+ */
56
+ invalidate?: false | true | CacheInvalidationRequest;
57
+ /** Per-write broad invalidation shortcut. Prefer invalidate for precise entries. */
58
+ autoInvalidate?: boolean;
59
+ };
60
+ /** Per-write broad invalidation shortcut kept for compatibility. */
61
+ autoInvalidate?: boolean;
62
+ }
63
+
64
+ /** v1-compatible find options exported from the root package. */
65
+ export interface FindOptions {
66
+ projection?: Record<string, any> | string[];
67
+ /** Alias for `projection`; `projection` wins when both are provided. */
68
+ project?: Record<string, any> | string[];
69
+ sort?: Record<string, 1 | -1>;
70
+ limit?: number;
71
+ skip?: number;
72
+ cache?: number;
73
+ maxTimeMS?: number;
74
+ hint?: any;
75
+ collation?: any;
76
+ comment?: string;
77
+ meta?: boolean | MetaOptions;
78
+ }
79
+
80
+ /** v1-compatible count options exported from the root package. */
81
+ export interface CountOptions {
82
+ cache?: number;
83
+ maxTimeMS?: number;
84
+ hint?: any;
85
+ collation?: any;
86
+ comment?: string;
87
+ meta?: boolean | MetaOptions;
88
+ }
89
+
90
+ /** v1-compatible aggregate options exported from the root package. */
91
+ export interface AggregateOptions {
92
+ cache?: number;
93
+ maxTimeMS?: number;
94
+ allowDiskUse?: boolean;
95
+ collation?: any;
96
+ hint?: string | object;
97
+ comment?: string;
98
+ meta?: boolean | MetaOptions;
99
+ }
100
+
101
+ /** v1-compatible distinct options exported from the root package. */
102
+ export interface DistinctOptions {
103
+ cache?: number;
104
+ maxTimeMS?: number;
105
+ collation?: any;
106
+ hint?: string | object;
107
+ meta?: boolean | MetaOptions;
108
+ }
109
+
11
110
  /** Query options that request the v1 `{ data, meta }` wrapper. */
12
111
  export interface QueryMetaOption extends Record<string, unknown> {
13
112
  /** Include operation timing metadata in the resolved query result. */
@@ -113,6 +212,8 @@ export interface TotalsOptions {
113
212
  /** Stream query options for collection.stream(). */
114
213
  export interface StreamOptions {
115
214
  projection?: Record<string, unknown> | string[];
215
+ /** Alias for `projection`; `projection` wins when both are provided. */
216
+ project?: Record<string, unknown> | string[];
116
217
  sort?: Record<string, 1 | -1>;
117
218
  limit?: number;
118
219
  skip?: number;
@@ -127,6 +228,8 @@ export interface StreamOptions {
127
228
  /** Explain/query plan options for collection.explain(). */
128
229
  export interface ExplainOptions {
129
230
  projection?: Record<string, unknown>;
231
+ /** Alias for `projection`; `projection` wins when both are provided. */
232
+ project?: Record<string, unknown>;
130
233
  sort?: Record<string, 1 | -1>;
131
234
  limit?: number;
132
235
  skip?: number;
@@ -169,9 +272,11 @@ export interface HealthView {
169
272
  status: 'up' | 'down';
170
273
  connected: boolean;
171
274
  /** @since v1 — driver connection state alias */
172
- driver?: { connected: boolean };
275
+ driver?: { connected: boolean; error?: string };
173
276
  defaults?: Record<string, unknown>;
174
277
  cache?: Record<string, unknown>;
278
+ checks?: Record<string, Record<string, unknown> & { status: 'up' | 'down' | 'unknown' }>;
279
+ capabilities?: Record<string, unknown>;
175
280
  }
176
281
 
177
282
  export interface InsertOneResult {
@@ -198,6 +303,10 @@ export interface DeleteResult {
198
303
  deletedCount: number;
199
304
  }
200
305
 
306
+ export type CursorValueType = 'date' | 'objectId' | 'string' | 'number' | 'boolean' | 'raw';
307
+
308
+ export type CursorValueNormalizer = (field: string, value: unknown) => unknown;
309
+
201
310
  export interface FindPageOptions<TSchema = any> {
202
311
  query?: Document;
203
312
  page?: number;
@@ -205,7 +314,9 @@ export interface FindPageOptions<TSchema = any> {
205
314
  after?: string;
206
315
  before?: string;
207
316
  sort?: Sort;
208
- projection?: Document;
317
+ projection?: Document | string[];
318
+ /** Alias for `projection`; `projection` wins when both are provided. */
319
+ project?: Document | string[];
209
320
  pipeline?: Document[];
210
321
  /** Totals/count configuration */
211
322
  totals?: TotalsOptions;
@@ -213,7 +324,11 @@ export interface FindPageOptions<TSchema = any> {
213
324
  explain?: boolean | string;
214
325
  /** Query comment for server-side profiling. v1 compat — top-level shortcut for `options.comment`. */
215
326
  comment?: string;
216
- options?: FindOptions;
327
+ /** Cursor value type hints used when decoding after/before tokens. Keeps ISO-like string fields as strings when set to `string` or `raw`. */
328
+ cursorTypes?: Record<string, CursorValueType>;
329
+ /** Custom cursor value normalizer. Receives the sort field name and decoded cursor value. */
330
+ cursorValueNormalizer?: CursorValueNormalizer;
331
+ options?: MongoFindOptions;
217
332
  /** Cursor-walking bookmark jump configuration */
218
333
  jump?: JumpOptions;
219
334
  /** Offset-based fallback for small page ranges */
@@ -388,12 +503,16 @@ export interface UpdateBatchResult {
388
503
  totalCount: number | null;
389
504
  matchedCount: number;
390
505
  modifiedCount: number;
391
- /** Number of documents upserted across all batches. */
506
+ /** Always 0; updateBatch rejects `upsert: true` before executing batch writes. */
392
507
  upsertedCount: number;
393
508
  batchCount: number;
394
509
  errors: BatchErrorRecord[];
395
510
  /** Per-batch retry records. */
396
511
  retries: BatchRetryRecord[];
512
+ /** Number of documents skipped by strict model version conflict handling. */
513
+ conflictCount?: number;
514
+ /** Document ids skipped by strict model version conflict handling. */
515
+ conflictedIds?: unknown[];
397
516
  }
398
517
 
399
518
  export interface DeleteBatchResult {
@@ -406,7 +525,7 @@ export interface DeleteBatchResult {
406
525
  retries: BatchRetryRecord[];
407
526
  }
408
527
 
409
- export interface BatchWriteOptions {
528
+ export interface BatchWriteOptions extends WriteCacheControlOptions {
410
529
  batchSize?: number;
411
530
  ordered?: boolean;
412
531
  concurrency?: number;
@@ -418,7 +537,7 @@ export interface BatchWriteOptions {
418
537
  }
419
538
 
420
539
  /** Options for updateBatch operations. */
421
- export interface UpdateBatchOptions {
540
+ export interface UpdateBatchOptions extends WriteCacheControlOptions {
422
541
  /** Number of documents per batch (default: 1000). */
423
542
  batchSize?: number;
424
543
  /** Estimate total for progress reporting. */
@@ -435,7 +554,11 @@ export interface UpdateBatchOptions {
435
554
  onRetry?: (retryInfo: RetryInfo) => void;
436
555
  /** MongoDB write concern. */
437
556
  writeConcern?: WriteConcern;
438
- /** Upsert documents that do not match the filter. */
557
+ /**
558
+ * @deprecated `updateBatch` does not support `upsert: true`; use `upsertOne` for
559
+ * single-document upserts, or `updateMany(..., { upsert: true })` only when you
560
+ * want MongoDB's native single-insert-on-no-match semantics. Passing `true` throws.
561
+ */
439
562
  upsert?: boolean;
440
563
  /** Array filters for nested array updates. */
441
564
  arrayFilters?: unknown[];
@@ -445,7 +568,7 @@ export interface UpdateBatchOptions {
445
568
  sort?: Record<string, 1 | -1>;
446
569
  }
447
570
 
448
- export interface IncrementOneOptions extends Record<string, unknown> {
571
+ export interface IncrementOneOptions extends WriteCacheControlOptions, Record<string, unknown> {
449
572
  returnDocument?: 'before' | 'after';
450
573
  projection?: Record<string, unknown>;
451
574
  $set?: Record<string, unknown>;
@@ -569,7 +692,7 @@ export interface AdminAccessor {
569
692
 
570
693
  export interface Collection<TSchema = any> {
571
694
  /** Returns the namespace descriptor (iid, db, collection) for this collection. */
572
- getNamespace(): { iid: string; type: 'mongodb'; db: string; collection: string; };
695
+ getNamespace(): { iid: string; type: 'mongodb'; db: string; collection: string; pool?: string; };
573
696
  /** Returns the underlying native MongoDB Collection object. */
574
697
  raw(): unknown;
575
698
  /**
@@ -739,7 +862,7 @@ export interface Collection<TSchema = any> {
739
862
  * Updates matched documents in configurable batches, walking via a cursor.
740
863
  * @param filter - MongoDB filter query.
741
864
  * @param update - Update operators.
742
- * @param options - UpdateBatchOptions (batchSize, upsert, onProgress, ).
865
+ * @param options - UpdateBatchOptions (batchSize, onProgress, retry settings; `upsert: true` is rejected).
743
866
  * @returns Aggregated update result across all batches.
744
867
  * @since v1.2.0
745
868
  */
@@ -947,12 +1070,12 @@ export type ClearBookmarksResult = BookmarkClearResult;
947
1070
  // ---------------------------------------------------------------------------
948
1071
 
949
1072
  /** Simplified insertOne options (used for the shorthand call form). */
950
- export interface InsertOneSimplifiedOptions {
1073
+ export interface InsertOneSimplifiedOptions extends WriteCacheControlOptions {
951
1074
  writeConcern?: WriteConcern;
952
1075
  bypassDocumentValidation?: boolean;
953
1076
  comment?: string;
954
1077
  session?: unknown;
955
- /** Invalidate query cache on success. @since v1.1.5 */
1078
+ /** Broadly invalidate query cache on success. Prefer `cache.invalidate` for precise entries. */
956
1079
  autoInvalidate?: boolean;
957
1080
  }
958
1081
 
@@ -965,13 +1088,13 @@ export interface InsertOneOptions {
965
1088
  }
966
1089
 
967
1090
  /** Simplified insertMany options (used for the shorthand call form). */
968
- export interface InsertManySimplifiedOptions {
1091
+ export interface InsertManySimplifiedOptions extends WriteCacheControlOptions {
969
1092
  ordered?: boolean;
970
1093
  writeConcern?: WriteConcern;
971
1094
  bypassDocumentValidation?: boolean;
972
1095
  comment?: string;
973
1096
  session?: unknown;
974
- /** Invalidate query cache on success. @since v1.1.5 */
1097
+ /** Broadly invalidate query cache on success. Prefer `cache.invalidate` for precise entries. */
975
1098
  autoInvalidate?: boolean;
976
1099
  }
977
1100
 
@@ -0,0 +1,221 @@
1
+ import type { MonSQLizeOptions } from './monsqlize.mjs';
2
+
3
+ export type DataTaskEnvironment = 'development' | 'test' | 'staging' | 'production' | 'prod' | 'live';
4
+
5
+ export interface DataTaskIndexDefinition {
6
+ key: Record<string, unknown>;
7
+ options?: Record<string, unknown>;
8
+ name?: string;
9
+ }
10
+
11
+ export type DataTaskJobErrorCode =
12
+ | 'INVALID_JOB'
13
+ | 'IDENTITY_CONFLICT'
14
+ | 'INDEX_CONFLICT'
15
+ | 'APPROVAL_STALE'
16
+ | 'BACKUP_FAILED'
17
+ | 'LOCK_NOT_ACQUIRED'
18
+ | 'LOCK_LOST'
19
+ | 'APPLY_PARTIAL'
20
+ | 'RESTORE_DRIFT'
21
+ | 'RESTORE_FAILED';
22
+
23
+ export declare class DataTaskJobError extends Error {
24
+ readonly code: DataTaskJobErrorCode;
25
+ readonly phase: string;
26
+ readonly collection?: string;
27
+ constructor(code: DataTaskJobErrorCode, message: string, phase?: string, collection?: string);
28
+ }
29
+
30
+ export interface DataTaskConnectionRuntime {
31
+ readonly options?: MonSQLizeOptions;
32
+ collection(name: string): unknown;
33
+ connect?(): Promise<unknown>;
34
+ close?(): Promise<void>;
35
+ }
36
+
37
+ export type DataTaskConnection = DataTaskConnectionRuntime | MonSQLizeOptions;
38
+
39
+ export type DataTaskIdentity =
40
+ | { mode: 'fields'; fields: string[] }
41
+ | { mode: 'source-id'; conflictBy?: string[] };
42
+
43
+ export interface DataTaskDataRule {
44
+ filter?: Record<string, unknown>;
45
+ all?: boolean;
46
+ identity: DataTaskIdentity;
47
+ strategy?: 'upsert' | 'insert';
48
+ projection?: Record<string, unknown>;
49
+ /** Rename only the listed source fields before synchronizing them. */
50
+ rename?: Record<string, string>;
51
+ /** Set only the listed literal values before synchronizing the document. */
52
+ set?: Record<string, unknown>;
53
+ /** Remove only the listed field paths from the synchronized target document. */
54
+ unset?: string[];
55
+ /** Maximum source documents this collection may plan. Defaults to 10000. */
56
+ maxDocuments?: number;
57
+ /** Write-ahead checkpoint batch size. Writes remain ordered within each batch. */
58
+ batchSize?: number;
59
+ }
60
+
61
+ export interface DataTaskVerifyRule {
62
+ mode?: 'sample' | 'full';
63
+ fields?: string[];
64
+ sampleSize?: number;
65
+ }
66
+
67
+ export interface DataTaskCollectionJob {
68
+ name: string;
69
+ targetName?: string;
70
+ indexes?: DataTaskIndexDefinition[];
71
+ data?: DataTaskDataRule;
72
+ verify?: DataTaskVerifyRule;
73
+ }
74
+
75
+ export interface DataTaskBackupOptions {
76
+ dir?: string;
77
+ format?: 'extended-jsonl';
78
+ compression?: 'gzip' | 'none';
79
+ retentionDays?: number;
80
+ /** Maximum uncompressed affected-scope backup size in bytes. Defaults to 256 MiB. */
81
+ maxBytes?: number;
82
+ }
83
+
84
+ export interface DataTaskLeaseLockOptions {
85
+ ttlMs?: number;
86
+ waitTimeoutMs?: number;
87
+ }
88
+
89
+ export interface DataTaskJob {
90
+ name: string;
91
+ description?: string;
92
+ source: DataTaskConnection;
93
+ target: DataTaskConnection;
94
+ targetEnvironment: DataTaskEnvironment;
95
+ collections: DataTaskCollectionJob[];
96
+ backup?: DataTaskBackupOptions;
97
+ lock?: boolean | DataTaskLeaseLockOptions;
98
+ }
99
+
100
+ export interface DataTaskApproval {
101
+ kind: 'apply' | 'restore';
102
+ token: string;
103
+ jobHash: string;
104
+ sourceHash: string;
105
+ targetHash: string;
106
+ indexHash: string;
107
+ issuedAt: string;
108
+ expiresAt: string;
109
+ }
110
+
111
+ export interface DataTaskIndexPlan {
112
+ name?: string;
113
+ key: Record<string, unknown>;
114
+ options: Record<string, unknown>;
115
+ status: 'existing' | 'missing' | 'conflict';
116
+ reason?: string;
117
+ }
118
+
119
+ export interface DataTaskChangeSample {
120
+ identity: Record<string, unknown>;
121
+ operation: 'insert' | 'update';
122
+ before: Record<string, unknown> | null;
123
+ after: Record<string, unknown>;
124
+ }
125
+
126
+ export interface DataTaskCollectionPreview {
127
+ source: string;
128
+ target: string;
129
+ data: {
130
+ source: number;
131
+ insert: number;
132
+ update: number;
133
+ unchanged: number;
134
+ conflict: number;
135
+ };
136
+ indexes: DataTaskIndexPlan[];
137
+ samples: DataTaskChangeSample[];
138
+ backupDocuments: number;
139
+ backupBytes: number;
140
+ }
141
+
142
+ export interface DataTaskPreviewResult {
143
+ mode: 'preview';
144
+ jobName: string;
145
+ passed: boolean;
146
+ collections: DataTaskCollectionPreview[];
147
+ warnings: string[];
148
+ errors: string[];
149
+ approval?: DataTaskApproval;
150
+ }
151
+
152
+ export interface DataTaskPreviewOptions {
153
+ approvalTtlMs?: number;
154
+ sampleSize?: number;
155
+ }
156
+
157
+ export interface DataTaskBackupRef {
158
+ runId: string;
159
+ manifestPath: string;
160
+ checksum: string;
161
+ }
162
+
163
+ export interface DataTaskApplyOptions {
164
+ approval: DataTaskApproval;
165
+ }
166
+
167
+ export interface DataTaskApplyResult {
168
+ mode: 'apply';
169
+ runId: string;
170
+ jobName: string;
171
+ passed: boolean;
172
+ status: 'passed' | 'partial' | 'failed';
173
+ collections: DataTaskCollectionPreview[];
174
+ backup: DataTaskBackupRef;
175
+ warnings: string[];
176
+ errors: string[];
177
+ }
178
+
179
+ export interface DataTaskRestoreTargetOptions {
180
+ target?: DataTaskConnection;
181
+ }
182
+
183
+ export interface DataTaskRestorePreviewResult {
184
+ mode: 'preview-restore';
185
+ runId: string;
186
+ passed: boolean;
187
+ restoreDocuments: number;
188
+ deleteDocuments: number;
189
+ dropIndexes: number;
190
+ createIndexes: number;
191
+ warnings: string[];
192
+ errors: string[];
193
+ approval?: DataTaskApproval;
194
+ }
195
+
196
+ export interface DataTaskRestoreOptions extends DataTaskRestoreTargetOptions {
197
+ approval: DataTaskApproval;
198
+ }
199
+
200
+ export interface DataTaskRestoreResult {
201
+ mode: 'restore';
202
+ runId: string;
203
+ passed: boolean;
204
+ status: 'passed' | 'partial' | 'failed';
205
+ safetyBackup: DataTaskBackupRef;
206
+ restoredDocuments: number;
207
+ deletedDocuments: number;
208
+ droppedIndexes: number;
209
+ createdIndexes: number;
210
+ warnings: string[];
211
+ errors: string[];
212
+ }
213
+
214
+ export interface DataTaskService {
215
+ preview(job: DataTaskJob, options?: DataTaskPreviewOptions): Promise<DataTaskPreviewResult>;
216
+ apply(job: DataTaskJob, options: DataTaskApplyOptions): Promise<DataTaskApplyResult>;
217
+ previewRestore(backup: DataTaskBackupRef, options?: DataTaskRestoreTargetOptions): Promise<DataTaskRestorePreviewResult>;
218
+ restore(backup: DataTaskBackupRef, options: DataTaskRestoreOptions): Promise<DataTaskRestoreResult>;
219
+ }
220
+
221
+ export declare const dataTasks: DataTaskService;