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
@@ -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;
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Unified expression operator type definitions.
3
+ * @module types/expression
4
+ * @since v1.0.9
5
+ */
6
+
7
+ /**
8
+ * All 122 expression operators supported by the MonSQLize expression system
9
+ * (100% MongoDB coverage).
10
+ *
11
+ * @since v1.0.9
12
+ */
13
+ export namespace UnifiedExpressionOperators {
14
+ /** Ternary conditional operator. */
15
+ export type TernaryOperator = '? :';
16
+
17
+ /** Nullish coalescing operator. */
18
+ export type NullishCoalescing = '??';
19
+
20
+ /** Comparison operators. */
21
+ export type ComparisonOperators = '>' | '>=' | '<' | '<=' | '===' | '!==';
22
+
23
+ /** Logical operators. */
24
+ export type LogicalOperators = '&&' | '||' | 'NOT';
25
+
26
+ /** Arithmetic operators. */
27
+ export type ArithmeticOperators = '+' | '-' | '*' | '/' | '%';
28
+
29
+ /** Math functions (8). */
30
+ export type MathFunctions = 'ABS' | 'CEIL' | 'FLOOR' | 'ROUND' | 'SQRT' | 'POW' | 'LOG' | 'LOG10';
31
+
32
+ /** Basic string functions (12). */
33
+ export type StringBasicFunctions =
34
+ | 'CONCAT' | 'UPPER' | 'LOWER' | 'TRIM' | 'SUBSTR' | 'LENGTH'
35
+ | 'SPLIT' | 'REPLACE' | 'INDEX_OF_STR' | 'LTRIM' | 'RTRIM' | 'SUBSTR_CP';
36
+
37
+ /** Extended string functions (3). @since v1.1.0 */
38
+ export type StringExtendedFunctions = 'STR_LEN_BYTES' | 'STR_LEN_CP' | 'SUBSTR_BYTES';
39
+
40
+ /** Basic array functions (10). */
41
+ export type ArrayBasicFunctions =
42
+ | 'SIZE' | 'ARRAY_ELEM_AT' | 'IN' | 'SLICE' | 'FIRST' | 'LAST'
43
+ | 'FILTER' | 'MAP' | 'INDEX_OF' | 'CONCAT_ARRAYS';
44
+
45
+ /** Extended array functions (4). @since v1.1.0 */
46
+ export type ArrayExtendedFunctions = 'REDUCE' | 'ZIP' | 'REVERSE_ARRAY' | 'RANGE';
47
+
48
+ /** Basic date functions (6). */
49
+ export type DateBasicFunctions = 'YEAR' | 'MONTH' | 'DAY_OF_MONTH' | 'HOUR' | 'MINUTE' | 'SECOND';
50
+
51
+ /** Advanced date functions (5). @since v1.1.0 */
52
+ export type DateAdvancedFunctions =
53
+ | 'DATE_ADD' | 'DATE_SUBTRACT' | 'DATE_DIFF' | 'DATE_TO_STRING' | 'DATE_FROM_STRING';
54
+
55
+ /** Extended date functions (8). @since v1.1.0 */
56
+ export type DateExtendedFunctions =
57
+ | 'DATE_FROM_PARTS' | 'DATE_TO_PARTS' | 'ISO_WEEK' | 'ISO_WEEK_YEAR'
58
+ | 'ISO_DAY_OF_WEEK' | 'DAY_OF_WEEK' | 'DAY_OF_YEAR' | 'WEEK';
59
+
60
+ /** Type inspection functions (5). */
61
+ export type TypeCheckFunctions = 'TYPE' | 'IS_NUMBER' | 'IS_ARRAY' | 'EXISTS' | 'NOT';
62
+
63
+ /** Basic type conversion functions. */
64
+ export type TypeConversionBasicFunctions = 'TO_INT' | 'TO_STRING' | 'OBJECT_TO_ARRAY' | 'ARRAY_TO_OBJECT';
65
+
66
+ /** Extended type conversion functions (7). @since v1.1.0 */
67
+ export type TypeConversionExtendedFunctions =
68
+ | 'TO_BOOL' | 'TO_DATE' | 'TO_DOUBLE' | 'TO_DECIMAL'
69
+ | 'TO_LONG' | 'TO_OBJECT_ID' | 'CONVERT';
70
+
71
+ /** Logical extended functions (2). @since v1.1.0 */
72
+ export type LogicalExtendedFunctions = 'ALL_ELEMENTS_TRUE' | 'ANY_ELEMENT_TRUE';
73
+
74
+ /** Conditional functions (3). */
75
+ export type ConditionalFunctions = 'SWITCH' | 'COND' | 'IF_NULL';
76
+
77
+ /** Object manipulation functions (5). */
78
+ export type ObjectOperationFunctions =
79
+ | 'MERGE_OBJECTS' | 'SET_FIELD' | 'UNSET_FIELD' | 'GET_FIELD' | 'OBJECT_TO_ARRAY';
80
+
81
+ /** Set operation functions (5). */
82
+ export type SetOperationFunctions =
83
+ | 'SET_UNION' | 'SET_DIFFERENCE' | 'SET_EQUALS' | 'SET_INTERSECTION' | 'SET_IS_SUBSET';
84
+
85
+ /** Advanced operation functions (5). @since v1.1.0 */
86
+ export type AdvancedOperationFunctions = 'LET' | 'LITERAL' | 'RAND' | 'SAMPLE_RATE' | 'REGEX';
87
+
88
+ /** Aggregate accumulator functions (7). */
89
+ export type AggregateFunctions = 'SUM' | 'AVG' | 'MAX' | 'MIN' | 'COUNT' | 'PUSH' | 'ADD_TO_SET';
90
+
91
+ /** Union of all supported operator strings. */
92
+ export type AllOperators =
93
+ | TernaryOperator
94
+ | NullishCoalescing
95
+ | ComparisonOperators
96
+ | LogicalOperators
97
+ | ArithmeticOperators
98
+ | MathFunctions
99
+ | StringBasicFunctions
100
+ | StringExtendedFunctions
101
+ | ArrayBasicFunctions
102
+ | ArrayExtendedFunctions
103
+ | DateBasicFunctions
104
+ | DateAdvancedFunctions
105
+ | DateExtendedFunctions
106
+ | TypeCheckFunctions
107
+ | TypeConversionBasicFunctions
108
+ | TypeConversionExtendedFunctions
109
+ | LogicalExtendedFunctions
110
+ | ConditionalFunctions
111
+ | ObjectOperationFunctions
112
+ | SetOperationFunctions
113
+ | AdvancedOperationFunctions
114
+ | AggregateFunctions;
115
+ }
@@ -0,0 +1,24 @@
1
+ export * from './base.mjs';
2
+ export * from './collection.mjs';
3
+ export * from './data-tasks.mjs';
4
+ export * from './expression.mjs';
5
+ export * from './lock.mjs';
6
+ export * from './model.mjs';
7
+ export * from './mongodb.mjs';
8
+ export * from './pool.mjs';
9
+ export * from './runtime.mjs';
10
+ export * from './saga.mjs';
11
+ export * from './slow-query-log.mjs';
12
+ export * from './sync.mjs';
13
+ export * from './transaction.mjs';
14
+ export * from './monsqlize.mjs';
15
+
16
+ export { default, MonSQLize } from './monsqlize.mjs';
17
+ export { Model, generateQueryHash, validateSyncConfig } from './runtime.mjs';
18
+
19
+ export type { Lock as LockContract } from './lock.mjs';
20
+ export type { ModelInstance as ModelAccessor, ModelInstance } from './model.mjs';
21
+ export type { SyncTargetConfig as SyncTarget } from './sync.mjs';
22
+ export type { Transaction as TransactionContract } from './transaction.mjs';
23
+ export type { MonSQLizeOptions as BaseOptions } from './monsqlize.mjs';
24
+
@@ -1,5 +1,6 @@
1
1
  export * from './base';
2
2
  export * from './collection';
3
+ export * from './data-tasks';
3
4
  export * from './expression';
4
5
  export * from './lock';
5
6
  export * from './model';
@@ -0,0 +1,77 @@
1
+ /** Options for acquiring or configuring a distributed lock. */
2
+ export interface LockOptions {
3
+ /** Lock TTL in milliseconds; the lock auto-releases after this duration. */
4
+ ttl?: number;
5
+ /** Number of retry attempts on acquire failure. */
6
+ retryTimes?: number;
7
+ /** Base delay between retries in milliseconds. */
8
+ retryDelay?: number;
9
+ /** Backoff multiplier applied to `retryDelay` on each successive retry. */
10
+ retryBackoff?: number;
11
+ /** When `true`, the callback is executed without a lock if acquisition fails (no-lock fallback). */
12
+ fallbackToNoLock?: boolean;
13
+ }
14
+
15
+ /** Aggregate statistics for a `LockManager` instance. */
16
+ export interface LockStats {
17
+ locksAcquired: number;
18
+ locksReleased: number;
19
+ lockChecks: number;
20
+ errors: number;
21
+ lockKeyPrefix: string;
22
+ maxDuration: number;
23
+ activeLocks: number;
24
+ }
25
+
26
+ /** Thrown when a lock cannot be acquired after all retry attempts are exhausted. */
27
+ export declare class LockAcquireError extends Error {
28
+ readonly code: 'LOCK_ACQUIRE_FAILED';
29
+ }
30
+
31
+ /** Thrown when a lock acquisition attempt times out. */
32
+ export declare class LockTimeoutError extends Error {
33
+ readonly code: 'LOCK_TIMEOUT';
34
+ }
35
+
36
+ /** Represents an acquired distributed lock; call `release()` to free it. */
37
+ export declare class Lock {
38
+ readonly key: string;
39
+ readonly lockId: string;
40
+ readonly ttl: number;
41
+ /** `true` after the lock has been released. v1 compat — exposed as readonly to prevent external mutation. */
42
+ readonly released: boolean;
43
+ /** Release the lock; returns `true` on success. */
44
+ release(): Promise<boolean>;
45
+ /** Extend the lock's TTL; returns `true` on success. */
46
+ renew(ttl?: number): Promise<boolean>;
47
+ /** Return `true` if the lock is still held (not yet released or expired). */
48
+ isHeld(): boolean;
49
+ /** Return the elapsed hold time in milliseconds since the lock was acquired. */
50
+ getHoldTime(): number;
51
+ }
52
+
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
+ */
57
+ export declare class LockManager {
58
+ constructor(options?: { logger?: unknown; lockKeyPrefix?: string; maxDuration?: number; });
59
+ /** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
60
+ withLock<T>(key: string, callback: () => Promise<T>, options?: LockOptions): Promise<T>;
61
+ /** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
62
+ acquireLock(key: string, options?: LockOptions): Promise<Lock>;
63
+ /** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
64
+ tryAcquireLock(key: string, options?: Omit<LockOptions, 'retryTimes'>): Promise<Lock | null>;
65
+ /** Return `true` if `key` is currently locked. */
66
+ isLocked(key: string): boolean;
67
+ /** Release the lock identified by `key` + `lockId`. */
68
+ releaseLock(key: string, lockId: string): Promise<boolean>;
69
+ /** Extend the TTL for the lock identified by `key` + `lockId`. */
70
+ renewLock(key: string, lockId: string, ttl: number): Promise<boolean>;
71
+ /** Return aggregate lock statistics. */
72
+ getStats(): LockStats;
73
+ /** Clear all active locks immediately. */
74
+ clear(): void;
75
+ /** Shut down the lock manager and release internal resources. */
76
+ close(): void;
77
+ }
@@ -50,14 +50,17 @@ export declare class Lock {
50
50
  getHoldTime(): number;
51
51
  }
52
52
 
53
- /** Manages distributed locks backed by the in-memory cache or a Redis store. */
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
- /** Execute `callback` while holding the lock for `key`; the lock is released automatically on completion. */
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
- /** Acquire the lock for `key`, blocking until available or timeout/retries are exhausted. */
61
+ /** @deprecated Business locks are retained for compatibility. Prefer application/framework-level locking. */
59
62
  acquireLock(key: string, options?: LockOptions): Promise<Lock>;
60
- /** Try to acquire the lock for `key` without blocking; returns `null` if already held. */
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;