monsqlize 2.0.6 → 3.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.
- package/CHANGELOG.md +12 -5
- package/MIGRATION.md +127 -0
- package/README.md +194 -202
- package/SECURITY.md +21 -0
- package/changelogs/README.md +16 -4
- package/changelogs/v2.0.7.md +63 -0
- package/changelogs/v3.0.0.md +99 -0
- package/changelogs/v3.1.0-rc.0.md +23 -0
- package/changelogs/v3.1.0.md +24 -0
- package/dist/cjs/cli/data-task.cjs +380 -0
- package/dist/cjs/index.cjs +12261 -5497
- package/dist/cjs/transaction/Transaction.cjs +151 -19
- package/dist/cjs/transaction/TransactionManager.cjs +152 -20
- package/dist/esm/index.mjs +12265 -5500
- package/dist/types/collection.d.mts +92 -13
- package/dist/types/collection.d.ts +92 -13
- package/dist/types/data-tasks.d.mts +221 -0
- package/dist/types/data-tasks.d.ts +221 -0
- package/dist/types/index.d.mts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/lock.d.mts +7 -4
- package/dist/types/lock.d.ts +7 -4
- package/dist/types/model.d.mts +46 -14
- package/dist/types/model.d.ts +46 -14
- package/dist/types/monsqlize.d.mts +130 -16
- package/dist/types/monsqlize.d.ts +130 -16
- package/dist/types/runtime.d.mts +14 -1
- package/dist/types/runtime.d.ts +14 -1
- package/dist/types/saga.d.mts +11 -6
- package/dist/types/saga.d.ts +11 -6
- package/dist/types/sync.d.mts +47 -1
- package/dist/types/sync.d.ts +47 -1
- package/dist/types/transaction.d.mts +8 -0
- package/dist/types/transaction.d.ts +8 -0
- package/licenses/production-dependencies.json +11 -0
- package/package.json +45 -16
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type { MonSQLizeOptions } from './monsqlize';
|
|
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;
|
package/dist/types/index.d.mts
CHANGED
package/dist/types/index.d.ts
CHANGED
package/dist/types/lock.d.mts
CHANGED
|
@@ -50,14 +50,17 @@ export declare class Lock {
|
|
|
50
50
|
getHoldTime(): number;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Manages legacy monSQLize locks backed by the in-memory cache or a Redis store.
|
|
55
|
+
* @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking.
|
|
56
|
+
*/
|
|
54
57
|
export declare class LockManager {
|
|
55
58
|
constructor(options?: { logger?: unknown; lockKeyPrefix?: string; maxDuration?: number; });
|
|
56
|
-
/**
|
|
59
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
57
60
|
withLock<T>(key: string, callback: () => Promise<T>, options?: LockOptions): Promise<T>;
|
|
58
|
-
/**
|
|
61
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
59
62
|
acquireLock(key: string, options?: LockOptions): Promise<Lock>;
|
|
60
|
-
/**
|
|
63
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
61
64
|
tryAcquireLock(key: string, options?: Omit<LockOptions, 'retryTimes'>): Promise<Lock | null>;
|
|
62
65
|
/** Return `true` if `key` is currently locked. */
|
|
63
66
|
isLocked(key: string): boolean;
|
package/dist/types/lock.d.ts
CHANGED
|
@@ -50,14 +50,17 @@ export declare class Lock {
|
|
|
50
50
|
getHoldTime(): number;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Manages legacy monSQLize locks backed by the in-memory cache or a Redis store.
|
|
55
|
+
* @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking.
|
|
56
|
+
*/
|
|
54
57
|
export declare class LockManager {
|
|
55
58
|
constructor(options?: { logger?: unknown; lockKeyPrefix?: string; maxDuration?: number; });
|
|
56
|
-
/**
|
|
59
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
57
60
|
withLock<T>(key: string, callback: () => Promise<T>, options?: LockOptions): Promise<T>;
|
|
58
|
-
/**
|
|
61
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
59
62
|
acquireLock(key: string, options?: LockOptions): Promise<Lock>;
|
|
60
|
-
/**
|
|
63
|
+
/** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
|
|
61
64
|
tryAcquireLock(key: string, options?: Omit<LockOptions, 'retryTimes'>): Promise<Lock | null>;
|
|
62
65
|
/** Return `true` if `key` is currently locked. */
|
|
63
66
|
isLocked(key: string): boolean;
|
package/dist/types/model.d.mts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import type { BookmarkClearResult, BookmarkListResult, BookmarkPrewarmResult, DeleteBatchResult, DeleteResult, IncrementOneResult, IndexCreateResult, InsertBatchResult, InsertManyResult, InsertOneResult, UpdateBatchResult, UpdateResult } from './collection.mjs';
|
|
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 = (
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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.
|
package/dist/types/model.d.ts
CHANGED
|
@@ -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 = (
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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?:
|
|
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.
|