monsqlize 2.0.5 → 2.0.6

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.
@@ -0,0 +1,126 @@
1
+ import type { LoggerLike } from './base.mjs';
2
+
3
+ export interface SlowQueryLogEntry {
4
+ database: string;
5
+ collection: string;
6
+ operation: string;
7
+ durationMs: number;
8
+ query?: unknown;
9
+ timestamp?: Date;
10
+ queryHash?: string;
11
+ metadata?: Record<string, unknown>;
12
+ }
13
+
14
+ export interface SlowQueryLogRecord {
15
+ queryHash: string;
16
+ database: string;
17
+ collection: string;
18
+ operation: string;
19
+ count: number;
20
+ totalTimeMs: number;
21
+ avgTimeMs: number;
22
+ maxTimeMs: number;
23
+ minTimeMs: number;
24
+ firstSeen: Date;
25
+ lastSeen: Date;
26
+ sampleQuery?: unknown;
27
+ metadata?: Record<string, unknown>;
28
+ }
29
+
30
+ export interface SlowQueryLogFilter {
31
+ database?: string;
32
+ collection?: string;
33
+ operation?: string;
34
+ queryHash?: string;
35
+ }
36
+
37
+ export interface SlowQueryLogQueryOptions {
38
+ sort?: Record<string, 1 | -1>;
39
+ limit?: number;
40
+ skip?: number;
41
+ }
42
+
43
+ export interface SlowQueryLogStorageConfig {
44
+ type?: 'memory' | 'mongodb';
45
+ useBusinessConnection?: boolean;
46
+ uri?: string | null;
47
+ database?: string;
48
+ collection?: string;
49
+ ttl?: number;
50
+ options?: Record<string, unknown>;
51
+ }
52
+
53
+ export interface SlowQueryLogConfig {
54
+ enabled: boolean;
55
+ storage: SlowQueryLogStorageConfig;
56
+ batch: {
57
+ enabled: boolean;
58
+ size: number;
59
+ interval: number;
60
+ maxBufferSize: number;
61
+ };
62
+ filter: {
63
+ excludeDatabases: string[];
64
+ excludeCollections: string[];
65
+ excludeOperations: string[];
66
+ minExecutionTimeMs: number;
67
+ };
68
+ advanced: {
69
+ errorHandling: 'log' | 'throw' | 'silent';
70
+ autoCreateIndexes: boolean;
71
+ };
72
+ }
73
+
74
+ export type SlowQueryLogConfigInput = boolean | Partial<SlowQueryLogConfig>;
75
+
76
+ export interface SlowQueryLogStorage {
77
+ initialize(): Promise<void>;
78
+ save(log: SlowQueryLogEntry): Promise<void>;
79
+ saveBatch(logs: SlowQueryLogEntry[]): Promise<void>;
80
+ query(filter?: SlowQueryLogFilter, options?: SlowQueryLogQueryOptions): Promise<SlowQueryLogRecord[]>;
81
+ close(): Promise<void>;
82
+ }
83
+
84
+ export declare const DEFAULT_SLOW_QUERY_LOG_CONFIG: SlowQueryLogConfig;
85
+ export declare function generateQueryHash(input: unknown): string;
86
+
87
+ export declare class BatchQueue {
88
+ constructor(storage: Pick<SlowQueryLogStorage, 'saveBatch'>, options?: Partial<SlowQueryLogConfig['batch']>, logger?: LoggerLike | null);
89
+ add(log: SlowQueryLogEntry): Promise<void>;
90
+ flush(): Promise<void>;
91
+ close(): Promise<void>;
92
+ }
93
+
94
+ export declare class SlowQueryLogMemoryStorage implements SlowQueryLogStorage {
95
+ initialize(): Promise<void>;
96
+ save(log: SlowQueryLogEntry): Promise<void>;
97
+ saveBatch(logs: SlowQueryLogEntry[]): Promise<void>;
98
+ query(filter?: SlowQueryLogFilter, options?: SlowQueryLogQueryOptions): Promise<SlowQueryLogRecord[]>;
99
+ close(): Promise<void>;
100
+ }
101
+
102
+ export declare class MongoDBSlowQueryLogStorage implements SlowQueryLogStorage {
103
+ constructor(config?: SlowQueryLogStorageConfig, businessClient?: unknown, logger?: LoggerLike | null);
104
+ initialize(): Promise<void>;
105
+ save(log: SlowQueryLogEntry): Promise<void>;
106
+ saveBatch(logs: SlowQueryLogEntry[]): Promise<void>;
107
+ query(filter?: SlowQueryLogFilter, options?: SlowQueryLogQueryOptions): Promise<SlowQueryLogRecord[]>;
108
+ close(): Promise<void>;
109
+ }
110
+
111
+ export declare class SlowQueryLogConfigManager {
112
+ static mergeConfig(userConfig?: SlowQueryLogConfigInput, businessType?: string): SlowQueryLogConfig;
113
+ static validate(config: SlowQueryLogConfig, businessType?: string): boolean;
114
+ }
115
+
116
+ export declare class SlowQueryLogManager {
117
+ readonly config: SlowQueryLogConfig;
118
+ readonly storage: SlowQueryLogStorage;
119
+ readonly queue: BatchQueue | null;
120
+ constructor(userConfig?: SlowQueryLogConfigInput, businessClient?: unknown, businessType?: string, logger?: LoggerLike | null);
121
+ initialize(): Promise<void>;
122
+ save(log: SlowQueryLogEntry): Promise<void>;
123
+ query(filter?: SlowQueryLogFilter, options?: SlowQueryLogQueryOptions): Promise<SlowQueryLogRecord[]>;
124
+ close(): Promise<void>;
125
+ }
126
+
@@ -0,0 +1,103 @@
1
+ import type { ChangeStreamDocument, Document, MongoClientOptions } from 'mongodb';
2
+
3
+ import type { LoggerLike } from './base.mjs';
4
+
5
+ export type SyncChangeEvent<TDocument extends Document = Document> = ChangeStreamDocument<TDocument> & {
6
+ _id?: unknown;
7
+ operationType: 'insert' | 'update' | 'replace' | 'delete';
8
+ ns: {
9
+ db: string;
10
+ coll: string;
11
+ };
12
+ documentKey?: Record<string, unknown>;
13
+ fullDocument?: TDocument;
14
+ };
15
+
16
+ export interface SyncTargetHealthCheckConfig {
17
+ /** Whether to enable health checks; defaults to true. */
18
+ enabled?: boolean;
19
+ /** Health check interval in milliseconds; defaults to 30000. */
20
+ interval?: number;
21
+ /** Timeout for a single health check in milliseconds; defaults to 5000. */
22
+ timeout?: number;
23
+ /** Number of retries after failure; defaults to 3. */
24
+ retries?: number;
25
+ }
26
+
27
+ export interface SyncTargetConfig {
28
+ name: string;
29
+ uri?: string;
30
+ pool?: string;
31
+ databaseName?: string;
32
+ collections?: string[];
33
+ options?: MongoClientOptions;
34
+ apply?: (event: SyncChangeEvent, document: Record<string, unknown> | undefined) => Promise<void>;
35
+ /** Target node health check configuration. @since v1.0.8 */
36
+ healthCheck?: SyncTargetHealthCheckConfig;
37
+ }
38
+
39
+ export interface ResumeTokenRedisLike {
40
+ get(key: string): Promise<string | null> | string | null;
41
+ set(key: string, value: string): Promise<unknown> | unknown;
42
+ del?(key: string): Promise<unknown> | unknown;
43
+ }
44
+
45
+ export interface ResumeTokenConfig {
46
+ storage?: 'file' | 'redis';
47
+ path?: string;
48
+ redis?: ResumeTokenRedisLike;
49
+ key?: string;
50
+ }
51
+
52
+ export interface SyncConfig {
53
+ enabled: boolean;
54
+ targets: SyncTargetConfig[];
55
+ collections?: string[];
56
+ resumeToken?: ResumeTokenConfig;
57
+ filter?: (event: SyncChangeEvent) => boolean;
58
+ /**
59
+ * Transform a change-stream document before forwarding to sync targets.
60
+ * v1 form took a single argument (`doc => ...`); v2 added a second `event` argument.
61
+ * Signature is permissive (`any`) to accept both forms — v1 was never typed.
62
+ */
63
+ transform?: (document: any, event?: SyncChangeEvent) => any;
64
+ }
65
+
66
+ export interface SyncStats {
67
+ isRunning: boolean;
68
+ eventCount: number;
69
+ syncedCount: number;
70
+ errorCount: number;
71
+ startTime: Date | null;
72
+ lastEventTime: Date | null;
73
+ targets: Array<{
74
+ name: string;
75
+ syncCount: number;
76
+ errorCount: number;
77
+ lastSyncTime: Date | null;
78
+ lastError: Error | null;
79
+ successRate: string;
80
+ }>;
81
+ }
82
+
83
+ export declare function validateSyncConfig(config: SyncConfig): void;
84
+
85
+ export declare class ResumeTokenStore {
86
+ constructor(options?: ResumeTokenConfig & { logger?: LoggerLike | null; });
87
+ load(): Promise<unknown | null>;
88
+ save(token: unknown): Promise<void>;
89
+ clear(): Promise<void>;
90
+ }
91
+
92
+ export declare class ChangeStreamSyncManager {
93
+ constructor(options: {
94
+ db: unknown;
95
+ poolManager?: unknown;
96
+ config: SyncConfig;
97
+ logger?: LoggerLike | null;
98
+ });
99
+ start(): Promise<void>;
100
+ stop(): Promise<void>;
101
+ getStats(): SyncStats;
102
+ }
103
+
@@ -0,0 +1,144 @@
1
+ import type { CacheLike } from './runtime.mjs';
2
+ import type { LoggerLike } from './base.mjs';
3
+
4
+ /** Minimal MongoDB session contract used by the transaction layer. */
5
+ export interface MongoSession {
6
+ id: unknown;
7
+ inTransaction(): boolean;
8
+ startTransaction(options?: Record<string, unknown>): void;
9
+ commitTransaction(): Promise<void>;
10
+ abortTransaction(): Promise<void>;
11
+ endSession(): Promise<void>;
12
+ /** v1 compat — v1 callers read `session.transaction?.state`; populated by the underlying driver. */
13
+ transaction?: { state?: string;[key: string]: unknown };
14
+ }
15
+
16
+ /** Options forwarded to the MongoDB driver when starting a transaction session. */
17
+ export interface TransactionOptions {
18
+ readConcern?: {
19
+ level: 'local' | 'majority' | 'snapshot' | 'linearizable' | 'available';
20
+ };
21
+ readPreference?: 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
22
+ causalConsistency?: boolean;
23
+ /** Maximum transaction duration in milliseconds before an automatic abort. */
24
+ maxDuration?: number;
25
+ /** @alias maxDuration — v1 compat field */
26
+ timeout?: number;
27
+ enableRetry?: boolean;
28
+ maxRetries?: number;
29
+ retryDelay?: number;
30
+ retryBackoff?: number;
31
+ enableCacheLock?: boolean;
32
+ lockCleanupInterval?: number;
33
+ writeConcern?: Record<string, unknown>;
34
+ }
35
+
36
+ /** Snapshot of a transaction's current state. */
37
+ export interface TransactionInfo {
38
+ id: string;
39
+ status: 'pending' | 'started' | 'committed' | 'aborted';
40
+ duration: number;
41
+ sessionId: string;
42
+ }
43
+
44
+ /** Aggregate statistics for all transactions managed by a `TransactionManager`. */
45
+ export interface TransactionStats {
46
+ totalTransactions: number;
47
+ successfulTransactions: number;
48
+ failedTransactions: number;
49
+ readOnlyTransactions: number;
50
+ writeTransactions: number;
51
+ activeTransactions: number;
52
+ averageDuration: number;
53
+ p95Duration: number;
54
+ p99Duration: number;
55
+ successRate: string;
56
+ readOnlyRatio: string;
57
+ sampleCount: number;
58
+ }
59
+
60
+ /**
61
+ * In-memory lock tracker used to serialise cache invalidation across transaction boundaries.
62
+ * Attached to `TransactionManager`; locks are auto-released when a transaction commits or aborts.
63
+ */
64
+ export declare class CacheLockManager {
65
+ constructor(options?: { logger?: LoggerLike | null; maxDuration?: number; cleanupInterval?: number; });
66
+ /** Register a lock for `key` owned by `owner`. */
67
+ addLock(key: string, owner: { id?: unknown; } | string): void;
68
+ /** Return `true` if `key` is currently locked. */
69
+ isLocked(key: string): boolean;
70
+ /** Release all locks held by `owner`. */
71
+ releaseLocks(owner: { id?: unknown; } | string): void;
72
+ /** Return lock usage statistics. */
73
+ getStats(): { totalLocks: number; activeLocks: number; maxDuration: number; };
74
+ /** Release all locks immediately. */
75
+ clear(): void;
76
+ /** Stop the background cleanup timer. */
77
+ stop(): void;
78
+ }
79
+
80
+ /** Represents a single MongoDB transaction session with optional cache-lock integration. */
81
+ export declare class Transaction {
82
+ readonly id: string;
83
+ readonly session: MongoSession;
84
+ readonly state: 'pending' | 'active' | 'committed' | 'aborted';
85
+ constructor(session: MongoSession, options?: {
86
+ cache?: CacheLike | null;
87
+ logger?: LoggerLike | null;
88
+ lockManager?: CacheLockManager | null;
89
+ timeout?: number;
90
+ transactionOptions?: Record<string, unknown>;
91
+ });
92
+ /** Begin the transaction (starts the MongoDB session transaction). */
93
+ start(): Promise<void>;
94
+ /** Commit the transaction; replays recorded cache invalidations on success. */
95
+ commit(): Promise<void>;
96
+ /** Abort the transaction and discard pending cache invalidations. */
97
+ abort(): Promise<void>;
98
+ /** Alias for `commit()` — v1 compat shorthand. */
99
+ end(): Promise<void>;
100
+ /** Record a cache-invalidation pattern to be replayed on commit. */
101
+ recordInvalidation(pattern: string): Promise<void>;
102
+ /** Return the elapsed duration in milliseconds since the transaction started. */
103
+ getDuration(): number;
104
+ /** Return a snapshot of the transaction's current state and metadata. */
105
+ getInfo(): TransactionInfo;
106
+ /** Return v1-compatible per-transaction statistics. */
107
+ getStats(): {
108
+ id: string;
109
+ state: 'pending' | 'active' | 'committed' | 'aborted';
110
+ duration: number;
111
+ hasWriteOperation: boolean;
112
+ operationCount: number;
113
+ lockedKeysCount: number;
114
+ };
115
+ }
116
+
117
+ /** Manages the lifecycle of MongoDB transactions, including retry and session pooling. */
118
+ export declare class TransactionManager {
119
+ constructor(options: {
120
+ client: unknown;
121
+ cache?: CacheLike | null;
122
+ logger?: LoggerLike | null;
123
+ lockManager?: CacheLockManager | null;
124
+ maxDuration?: number;
125
+ enableRetry?: boolean;
126
+ maxRetries?: number;
127
+ retryDelay?: number;
128
+ retryBackoff?: number;
129
+ defaultReadConcern?: TransactionOptions['readConcern'];
130
+ defaultWriteConcern?: TransactionOptions['writeConcern'];
131
+ defaultReadPreference?: TransactionOptions['readPreference'];
132
+ maxStatsSamples?: number;
133
+ });
134
+ /** Open a new transaction session. */
135
+ startSession(options?: TransactionOptions): Promise<Transaction>;
136
+ /** Execute `callback` inside a transaction; commits on success, aborts on failure. */
137
+ withTransaction<T>(callback: (transaction: Transaction) => Promise<T>, options?: TransactionOptions): Promise<T>;
138
+ /** Return all currently active (uncommitted) transactions. */
139
+ getActiveTransactions(): Transaction[];
140
+ /** Abort all active transactions immediately. */
141
+ abortAll(): Promise<void>;
142
+ /** Return aggregate transaction statistics. */
143
+ getStats(): TransactionStats;
144
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monsqlize",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "TypeScript-native MongoDB ODM with multi-level caching (cache-hub), distributed locks, Saga orchestration, unified expression system (122 operators), connection pool management, ChangeStream sync, slow-query logging, and full v1 API compatibility",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/cjs/index.cjs",
@@ -8,7 +8,10 @@
8
8
  "types": "./dist/types/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./dist/types/index.d.ts",
11
+ "types": {
12
+ "import": "./dist/types/index.d.mts",
13
+ "require": "./dist/types/index.d.ts"
14
+ },
12
15
  "require": "./dist/cjs/index.cjs",
13
16
  "import": "./dist/esm/index.mjs"
14
17
  },
@@ -18,6 +21,8 @@
18
21
  "dist/**/*.cjs",
19
22
  "dist/**/*.mjs",
20
23
  "dist/**/*.d.ts",
24
+ "dist/**/*.d.mts",
25
+ "changelogs/v2.0.6.md",
21
26
  "changelogs/v2.0.5.md",
22
27
  "changelogs/v2.0.4.md",
23
28
  "changelogs/v2.0.3.md",
@@ -117,7 +122,7 @@
117
122
  "cache-hub": "2.2.4",
118
123
  "ioredis": "5.8.2",
119
124
  "mongodb": "6.21.0",
120
- "schema-dsl": "2.0.10",
125
+ "schema-dsl": "2.0.11",
121
126
  "ssh2": "1.17.0"
122
127
  }
123
128
  }