monsqlize 1.3.0 → 2.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 (172) hide show
  1. package/CHANGELOG.md +56 -60
  2. package/LICENSE +201 -21
  3. package/README.md +537 -1828
  4. package/changelogs/README.md +160 -0
  5. package/changelogs/v2.0.0.md +222 -0
  6. package/dist/cjs/index.cjs +10600 -0
  7. package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
  8. package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
  9. package/dist/cjs/transaction/Transaction.cjs +158 -0
  10. package/dist/cjs/transaction/TransactionManager.cjs +298 -0
  11. package/dist/esm/index.mjs +10650 -0
  12. package/dist/types/base.d.ts +81 -0
  13. package/dist/types/collection.d.ts +1031 -0
  14. package/dist/types/expression.d.ts +115 -0
  15. package/dist/types/index.d.ts +23 -0
  16. package/dist/types/lock.d.ts +74 -0
  17. package/dist/types/model.d.ts +526 -0
  18. package/dist/types/mongodb.d.ts +49 -0
  19. package/dist/types/monsqlize.d.ts +491 -0
  20. package/dist/types/pool.d.ts +84 -0
  21. package/dist/types/runtime.d.ts +362 -0
  22. package/dist/types/saga.d.ts +143 -0
  23. package/dist/types/slow-query-log.d.ts +126 -0
  24. package/dist/types/sync.d.ts +103 -0
  25. package/dist/types/transaction.d.ts +132 -0
  26. package/package.json +67 -69
  27. package/index.d.ts +0 -206
  28. package/index.mjs +0 -52
  29. package/lib/cache-invalidation.js +0 -279
  30. package/lib/cache.js +0 -530
  31. package/lib/common/cursor.js +0 -59
  32. package/lib/common/docs-urls.js +0 -73
  33. package/lib/common/index-options.js +0 -223
  34. package/lib/common/log.js +0 -61
  35. package/lib/common/namespace.js +0 -22
  36. package/lib/common/normalize.js +0 -34
  37. package/lib/common/page-result.js +0 -43
  38. package/lib/common/runner.js +0 -57
  39. package/lib/common/server-features.js +0 -232
  40. package/lib/common/shape-builders.js +0 -27
  41. package/lib/common/validation.js +0 -113
  42. package/lib/connect.js +0 -99
  43. package/lib/constants.js +0 -55
  44. package/lib/count-queue.js +0 -188
  45. package/lib/distributed-cache-invalidator.js +0 -260
  46. package/lib/errors.js +0 -168
  47. package/lib/expression/cache/ExpressionCache.js +0 -114
  48. package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
  49. package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
  50. package/lib/expression/detector.js +0 -84
  51. package/lib/expression/factory.js +0 -29
  52. package/lib/expression/index.js +0 -19
  53. package/lib/function-cache.js +0 -533
  54. package/lib/index.js +0 -1251
  55. package/lib/infrastructure/ConnectionPoolManager.js +0 -464
  56. package/lib/infrastructure/HealthChecker.js +0 -281
  57. package/lib/infrastructure/PoolConfig.js +0 -199
  58. package/lib/infrastructure/PoolSelector.js +0 -225
  59. package/lib/infrastructure/PoolStats.js +0 -244
  60. package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
  61. package/lib/infrastructure/ssh-tunnel.js +0 -41
  62. package/lib/infrastructure/uri-parser.js +0 -36
  63. package/lib/lock/Lock.js +0 -67
  64. package/lib/lock/errors.js +0 -28
  65. package/lib/lock/index.js +0 -13
  66. package/lib/logger.js +0 -225
  67. package/lib/model/examples/test.js +0 -311
  68. package/lib/model/features/defaults.js +0 -161
  69. package/lib/model/features/populate.js +0 -568
  70. package/lib/model/features/relations.js +0 -120
  71. package/lib/model/features/soft-delete.js +0 -349
  72. package/lib/model/features/version.js +0 -157
  73. package/lib/model/features/virtuals.js +0 -219
  74. package/lib/model/index.js +0 -1265
  75. package/lib/mongodb/common/accessor-helpers.js +0 -59
  76. package/lib/mongodb/common/agg-pipeline.js +0 -36
  77. package/lib/mongodb/common/aggregation-validator.js +0 -127
  78. package/lib/mongodb/common/iid.js +0 -28
  79. package/lib/mongodb/common/lexicographic-expr.js +0 -53
  80. package/lib/mongodb/common/shape.js +0 -32
  81. package/lib/mongodb/common/sort.js +0 -39
  82. package/lib/mongodb/common/transaction-aware.js +0 -25
  83. package/lib/mongodb/connect.js +0 -234
  84. package/lib/mongodb/index.js +0 -639
  85. package/lib/mongodb/management/admin-ops.js +0 -200
  86. package/lib/mongodb/management/bookmark-ops.js +0 -167
  87. package/lib/mongodb/management/cache-ops.js +0 -50
  88. package/lib/mongodb/management/collection-ops.js +0 -387
  89. package/lib/mongodb/management/database-ops.js +0 -202
  90. package/lib/mongodb/management/index-ops.js +0 -475
  91. package/lib/mongodb/management/index.js +0 -17
  92. package/lib/mongodb/management/namespace.js +0 -31
  93. package/lib/mongodb/management/validation-ops.js +0 -268
  94. package/lib/mongodb/queries/aggregate.js +0 -172
  95. package/lib/mongodb/queries/chain.js +0 -631
  96. package/lib/mongodb/queries/count.js +0 -99
  97. package/lib/mongodb/queries/distinct.js +0 -78
  98. package/lib/mongodb/queries/find-and-count.js +0 -193
  99. package/lib/mongodb/queries/find-by-ids.js +0 -236
  100. package/lib/mongodb/queries/find-one-by-id.js +0 -171
  101. package/lib/mongodb/queries/find-one.js +0 -71
  102. package/lib/mongodb/queries/find-page.js +0 -618
  103. package/lib/mongodb/queries/find.js +0 -171
  104. package/lib/mongodb/queries/index.js +0 -51
  105. package/lib/mongodb/queries/watch.js +0 -538
  106. package/lib/mongodb/writes/common/batch-retry.js +0 -65
  107. package/lib/mongodb/writes/delete-batch.js +0 -323
  108. package/lib/mongodb/writes/delete-many.js +0 -181
  109. package/lib/mongodb/writes/delete-one.js +0 -173
  110. package/lib/mongodb/writes/find-one-and-delete.js +0 -203
  111. package/lib/mongodb/writes/find-one-and-replace.js +0 -239
  112. package/lib/mongodb/writes/find-one-and-update.js +0 -240
  113. package/lib/mongodb/writes/increment-one.js +0 -259
  114. package/lib/mongodb/writes/index.js +0 -46
  115. package/lib/mongodb/writes/insert-batch.js +0 -508
  116. package/lib/mongodb/writes/insert-many.js +0 -223
  117. package/lib/mongodb/writes/insert-one.js +0 -169
  118. package/lib/mongodb/writes/replace-one.js +0 -226
  119. package/lib/mongodb/writes/result-handler.js +0 -237
  120. package/lib/mongodb/writes/update-batch.js +0 -416
  121. package/lib/mongodb/writes/update-many.js +0 -275
  122. package/lib/mongodb/writes/update-one.js +0 -273
  123. package/lib/mongodb/writes/upsert-one.js +0 -203
  124. package/lib/multi-level-cache.js +0 -244
  125. package/lib/operators.js +0 -330
  126. package/lib/redis-cache-adapter.js +0 -267
  127. package/lib/saga/SagaContext.js +0 -67
  128. package/lib/saga/SagaDefinition.js +0 -32
  129. package/lib/saga/SagaExecutor.js +0 -201
  130. package/lib/saga/SagaOrchestrator.js +0 -186
  131. package/lib/saga/index.js +0 -11
  132. package/lib/slow-query-log/base-storage.js +0 -70
  133. package/lib/slow-query-log/batch-queue.js +0 -97
  134. package/lib/slow-query-log/config-manager.js +0 -196
  135. package/lib/slow-query-log/index.js +0 -238
  136. package/lib/slow-query-log/mongodb-storage.js +0 -324
  137. package/lib/slow-query-log/query-hash.js +0 -39
  138. package/lib/sync/ChangeStreamSyncManager.js +0 -405
  139. package/lib/sync/ResumeTokenStore.js +0 -192
  140. package/lib/sync/SyncConfig.js +0 -127
  141. package/lib/sync/SyncTarget.js +0 -240
  142. package/lib/sync/index.js +0 -20
  143. package/lib/transaction/CacheLockManager.js +0 -162
  144. package/lib/transaction/DistributedCacheLockManager.js +0 -475
  145. package/lib/transaction/Transaction.js +0 -315
  146. package/lib/transaction/TransactionManager.js +0 -267
  147. package/lib/transaction/index.js +0 -11
  148. package/lib/utils/objectid-converter.js +0 -632
  149. package/types/README.md +0 -122
  150. package/types/base.ts +0 -94
  151. package/types/batch.ts +0 -187
  152. package/types/cache.ts +0 -71
  153. package/types/chain.ts +0 -254
  154. package/types/collection.ts +0 -357
  155. package/types/expression.ts +0 -109
  156. package/types/function-cache.d.ts +0 -135
  157. package/types/lock.ts +0 -95
  158. package/types/model/definition.ts +0 -152
  159. package/types/model/index.ts +0 -10
  160. package/types/model/instance.ts +0 -121
  161. package/types/model/relations.ts +0 -121
  162. package/types/model/virtuals.ts +0 -32
  163. package/types/monsqlize.ts +0 -245
  164. package/types/options.ts +0 -192
  165. package/types/pagination.ts +0 -154
  166. package/types/pool.ts +0 -125
  167. package/types/query.ts +0 -71
  168. package/types/saga.ts +0 -125
  169. package/types/stream.ts +0 -64
  170. package/types/sync.ts +0 -79
  171. package/types/transaction.ts +0 -79
  172. package/types/write.ts +0 -77
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ // src/entry/compat/mongodb/common/transaction-aware.ts
4
+ function isInTransaction(value) {
5
+ return !!value?.session?.inTransaction?.();
6
+ }
7
+ function getTransactionFromSession(session) {
8
+ return session?.__monSQLizeTransaction ?? null;
9
+ }
10
+ module.exports = { isInTransaction, getTransactionFromSession };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ // src/capabilities/transaction/index.ts
4
+ var CacheLockManager = class {
5
+ constructor(options = {}) {
6
+ this.locks = /* @__PURE__ */ new Map();
7
+ this._totalLocksAdded = 0;
8
+ this.logger = options.logger ?? null;
9
+ this.maxDuration = options.maxDuration ?? 3e5;
10
+ this.cleanupInterval = options.cleanupInterval ?? 1e4;
11
+ this.cleanupTimer = setInterval(() => {
12
+ this.cleanupExpiredLocks();
13
+ }, this.cleanupInterval);
14
+ this.cleanupTimer.unref?.();
15
+ }
16
+ /**
17
+ * Add a cache lock.
18
+ * @since v1.4.0
19
+ */
20
+ addLock(key, owner) {
21
+ const ownerId = typeof owner === "string" ? owner : String(owner.id ?? "unknown");
22
+ this.locks.set(key, {
23
+ ownerId,
24
+ expiresAt: Date.now() + this.maxDuration
25
+ });
26
+ this._totalLocksAdded += 1;
27
+ }
28
+ /**
29
+ * Check whether a cache key is locked.
30
+ * @since v1.4.0
31
+ */
32
+ isLocked(key) {
33
+ this.cleanupExpiredLocks();
34
+ if (this.locks.has(key)) {
35
+ return true;
36
+ }
37
+ for (const pattern of this.locks.keys()) {
38
+ if (!pattern.includes("*")) {
39
+ continue;
40
+ }
41
+ const regex = new RegExp(`^${escapeRegExp(pattern).replace(/\\\*/g, ".*")}$`);
42
+ if (regex.test(key)) {
43
+ return true;
44
+ }
45
+ }
46
+ return false;
47
+ }
48
+ /**
49
+ * Release all cache locks held by the given owner.
50
+ * @since v1.4.0
51
+ */
52
+ releaseLocks(owner) {
53
+ const ownerId = typeof owner === "string" ? owner : String(owner.id ?? "unknown");
54
+ for (const [key, record] of this.locks.entries()) {
55
+ if (record.ownerId === ownerId) {
56
+ this.locks.delete(key);
57
+ }
58
+ }
59
+ }
60
+ /**
61
+ * Get cache lock statistics.
62
+ * @since v1.4.0
63
+ */
64
+ getStats() {
65
+ this.cleanupExpiredLocks();
66
+ return {
67
+ totalLocks: this._totalLocksAdded,
68
+ activeLocks: this.locks.size,
69
+ maxDuration: this.maxDuration
70
+ };
71
+ }
72
+ /**
73
+ * Clear all cache locks.
74
+ * @since v1.4.0
75
+ */
76
+ clear() {
77
+ this.locks.clear();
78
+ }
79
+ /**
80
+ * Stop the cache lock manager.
81
+ * @since v1.4.0
82
+ */
83
+ stop() {
84
+ clearInterval(this.cleanupTimer);
85
+ }
86
+ cleanupExpiredLocks() {
87
+ const now = Date.now();
88
+ for (const [key, value] of this.locks.entries()) {
89
+ if (value.expiresAt <= now) {
90
+ this.locks.delete(key);
91
+ }
92
+ }
93
+ }
94
+ };
95
+ function escapeRegExp(value) {
96
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
97
+ }
98
+
99
+ // src/entry/compat/transaction/CacheLockManager.ts
100
+ module.exports = CacheLockManager;
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+
3
+ // src/capabilities/transaction/index.ts
4
+ var import_node_crypto = require("node:crypto");
5
+ function toPublicTransactionStatus(state) {
6
+ return state === "active" ? "started" : state;
7
+ }
8
+ var Transaction = class {
9
+ constructor(session, options = {}) {
10
+ this.session = session;
11
+ this.options = options;
12
+ this.id = `tx_${(0, import_node_crypto.randomBytes)(8).toString("hex")}`;
13
+ this.state = "pending";
14
+ this.startedAt = null;
15
+ this.timeoutTimer = null;
16
+ this.pendingInvalidations = /* @__PURE__ */ new Set();
17
+ this.session.__monSQLizeTransaction = this;
18
+ }
19
+ /**
20
+ * Start the transaction.
21
+ * @since v1.4.0
22
+ */
23
+ async start() {
24
+ if (this.state !== "pending") {
25
+ throw new Error(`Cannot start transaction in state: ${this.state}`);
26
+ }
27
+ this.session.startTransaction();
28
+ this.state = "active";
29
+ this.startedAt = Date.now();
30
+ const timeout = this.options.timeout ?? 3e4;
31
+ if (timeout > 0) {
32
+ this.timeoutTimer = setTimeout(() => {
33
+ if (this.state === "active") {
34
+ this.options.logger?.warn?.(`[Transaction] auto-abort on timeout: ${this.id}`);
35
+ void this.abort();
36
+ }
37
+ }, timeout);
38
+ this.timeoutTimer.unref?.();
39
+ }
40
+ }
41
+ /**
42
+ * Commit the transaction.
43
+ * @since v1.4.0
44
+ */
45
+ async commit() {
46
+ if (this.state !== "active") {
47
+ throw new Error(`Cannot commit transaction in state: ${this.state}`);
48
+ }
49
+ if (typeof this.session.commitTransaction === "function") {
50
+ await this.session.commitTransaction();
51
+ }
52
+ this.state = "committed";
53
+ this.options.lockManager?.releaseLocks(this.id);
54
+ this.pendingInvalidations.clear();
55
+ this.clearTimeout();
56
+ }
57
+ /**
58
+ * Roll back the transaction.
59
+ * @since v1.4.0
60
+ */
61
+ async abort() {
62
+ if (this.state !== "pending" && this.state !== "active") {
63
+ return;
64
+ }
65
+ if (this.state === "active") {
66
+ if (typeof this.session.abortTransaction === "function") {
67
+ await this.session.abortTransaction();
68
+ }
69
+ }
70
+ this.state = "aborted";
71
+ this.options.lockManager?.releaseLocks(this.id);
72
+ this.pendingInvalidations.clear();
73
+ this.clearTimeout();
74
+ }
75
+ /**
76
+ * End the transaction session.
77
+ * @since v1.4.0
78
+ */
79
+ async end() {
80
+ this.clearTimeout();
81
+ this.options.lockManager?.releaseLocks(this.id);
82
+ await this.session.endSession();
83
+ }
84
+ /**
85
+ * Record a cache invalidation intent.
86
+ * @since v1.4.0
87
+ */
88
+ async recordInvalidation(pattern) {
89
+ this.pendingInvalidations.add(pattern);
90
+ this.options.lockManager?.addLock(pattern, this.id);
91
+ if (this.options.cache?.delPattern) {
92
+ await this.options.cache.delPattern(pattern);
93
+ }
94
+ }
95
+ /**
96
+ * Get the transaction duration.
97
+ * @since v1.4.0
98
+ */
99
+ getDuration() {
100
+ if (!this.startedAt) {
101
+ return 0;
102
+ }
103
+ return Date.now() - this.startedAt;
104
+ }
105
+ /**
106
+ * Get transaction info.
107
+ * @since v1.4.0
108
+ */
109
+ getInfo() {
110
+ return {
111
+ id: this.id,
112
+ status: toPublicTransactionStatus(this.state),
113
+ duration: this.getDuration(),
114
+ sessionId: stringifySessionId(this.session.id)
115
+ };
116
+ }
117
+ /**
118
+ * Get v1-compatible transaction statistics for this transaction instance.
119
+ * @since v1.4.0
120
+ */
121
+ getStats() {
122
+ return {
123
+ id: this.id,
124
+ state: this.state,
125
+ duration: this.getDuration(),
126
+ hasWriteOperation: this.pendingInvalidations.size > 0,
127
+ operationCount: this.pendingInvalidations.size,
128
+ lockedKeysCount: this.pendingInvalidations.size
129
+ };
130
+ }
131
+ clearTimeout() {
132
+ if (this.timeoutTimer) {
133
+ clearTimeout(this.timeoutTimer);
134
+ this.timeoutTimer = null;
135
+ }
136
+ }
137
+ };
138
+ function stringifySessionId(id) {
139
+ if (typeof id === "string") {
140
+ return id;
141
+ }
142
+ if (typeof id === "object" && id !== null) {
143
+ const candidate = id;
144
+ if (typeof candidate.toHexString === "function") {
145
+ return candidate.toHexString();
146
+ }
147
+ if (candidate.id?.buffer) {
148
+ return Buffer.from(candidate.id.buffer).toString("hex");
149
+ }
150
+ if (typeof candidate.toString === "function") {
151
+ return candidate.toString();
152
+ }
153
+ }
154
+ return String(id);
155
+ }
156
+
157
+ // src/entry/compat/transaction/Transaction.ts
158
+ module.exports = Transaction;
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+
3
+ // src/capabilities/transaction/index.ts
4
+ var import_node_crypto = require("node:crypto");
5
+ function toPublicTransactionStatus(state) {
6
+ return state === "active" ? "started" : state;
7
+ }
8
+ var Transaction = class {
9
+ constructor(session, options = {}) {
10
+ this.session = session;
11
+ this.options = options;
12
+ this.id = `tx_${(0, import_node_crypto.randomBytes)(8).toString("hex")}`;
13
+ this.state = "pending";
14
+ this.startedAt = null;
15
+ this.timeoutTimer = null;
16
+ this.pendingInvalidations = /* @__PURE__ */ new Set();
17
+ this.session.__monSQLizeTransaction = this;
18
+ }
19
+ /**
20
+ * Start the transaction.
21
+ * @since v1.4.0
22
+ */
23
+ async start() {
24
+ if (this.state !== "pending") {
25
+ throw new Error(`Cannot start transaction in state: ${this.state}`);
26
+ }
27
+ this.session.startTransaction();
28
+ this.state = "active";
29
+ this.startedAt = Date.now();
30
+ const timeout = this.options.timeout ?? 3e4;
31
+ if (timeout > 0) {
32
+ this.timeoutTimer = setTimeout(() => {
33
+ if (this.state === "active") {
34
+ this.options.logger?.warn?.(`[Transaction] auto-abort on timeout: ${this.id}`);
35
+ void this.abort();
36
+ }
37
+ }, timeout);
38
+ this.timeoutTimer.unref?.();
39
+ }
40
+ }
41
+ /**
42
+ * Commit the transaction.
43
+ * @since v1.4.0
44
+ */
45
+ async commit() {
46
+ if (this.state !== "active") {
47
+ throw new Error(`Cannot commit transaction in state: ${this.state}`);
48
+ }
49
+ if (typeof this.session.commitTransaction === "function") {
50
+ await this.session.commitTransaction();
51
+ }
52
+ this.state = "committed";
53
+ this.options.lockManager?.releaseLocks(this.id);
54
+ this.pendingInvalidations.clear();
55
+ this.clearTimeout();
56
+ }
57
+ /**
58
+ * Roll back the transaction.
59
+ * @since v1.4.0
60
+ */
61
+ async abort() {
62
+ if (this.state !== "pending" && this.state !== "active") {
63
+ return;
64
+ }
65
+ if (this.state === "active") {
66
+ if (typeof this.session.abortTransaction === "function") {
67
+ await this.session.abortTransaction();
68
+ }
69
+ }
70
+ this.state = "aborted";
71
+ this.options.lockManager?.releaseLocks(this.id);
72
+ this.pendingInvalidations.clear();
73
+ this.clearTimeout();
74
+ }
75
+ /**
76
+ * End the transaction session.
77
+ * @since v1.4.0
78
+ */
79
+ async end() {
80
+ this.clearTimeout();
81
+ this.options.lockManager?.releaseLocks(this.id);
82
+ await this.session.endSession();
83
+ }
84
+ /**
85
+ * Record a cache invalidation intent.
86
+ * @since v1.4.0
87
+ */
88
+ async recordInvalidation(pattern) {
89
+ this.pendingInvalidations.add(pattern);
90
+ this.options.lockManager?.addLock(pattern, this.id);
91
+ if (this.options.cache?.delPattern) {
92
+ await this.options.cache.delPattern(pattern);
93
+ }
94
+ }
95
+ /**
96
+ * Get the transaction duration.
97
+ * @since v1.4.0
98
+ */
99
+ getDuration() {
100
+ if (!this.startedAt) {
101
+ return 0;
102
+ }
103
+ return Date.now() - this.startedAt;
104
+ }
105
+ /**
106
+ * Get transaction info.
107
+ * @since v1.4.0
108
+ */
109
+ getInfo() {
110
+ return {
111
+ id: this.id,
112
+ status: toPublicTransactionStatus(this.state),
113
+ duration: this.getDuration(),
114
+ sessionId: stringifySessionId(this.session.id)
115
+ };
116
+ }
117
+ /**
118
+ * Get v1-compatible transaction statistics for this transaction instance.
119
+ * @since v1.4.0
120
+ */
121
+ getStats() {
122
+ return {
123
+ id: this.id,
124
+ state: this.state,
125
+ duration: this.getDuration(),
126
+ hasWriteOperation: this.pendingInvalidations.size > 0,
127
+ operationCount: this.pendingInvalidations.size,
128
+ lockedKeysCount: this.pendingInvalidations.size
129
+ };
130
+ }
131
+ clearTimeout() {
132
+ if (this.timeoutTimer) {
133
+ clearTimeout(this.timeoutTimer);
134
+ this.timeoutTimer = null;
135
+ }
136
+ }
137
+ };
138
+ var TransactionManager = class {
139
+ constructor(input, legacyCache, legacyOptions = {}) {
140
+ this.activeTransactions = /* @__PURE__ */ new Map();
141
+ this.durations = [];
142
+ this.stats = {
143
+ totalTransactions: 0,
144
+ successfulTransactions: 0,
145
+ failedTransactions: 0
146
+ };
147
+ const options = "client" in input ? input : {
148
+ client: input,
149
+ cache: legacyCache,
150
+ ...legacyOptions
151
+ };
152
+ this.client = options.client;
153
+ this.cache = options.cache ?? null;
154
+ this.logger = options.logger ?? null;
155
+ this.lockManager = options.lockManager ?? null;
156
+ this.defaultOptions = {
157
+ maxDuration: options.maxDuration ?? 3e4,
158
+ enableRetry: options.enableRetry ?? true,
159
+ maxRetries: options.maxRetries ?? 3,
160
+ retryDelay: options.retryDelay ?? 100,
161
+ retryBackoff: options.retryBackoff ?? 2
162
+ };
163
+ }
164
+ /**
165
+ * Create a manual transaction session.
166
+ * @since v1.4.0
167
+ */
168
+ async startSession(options = {}) {
169
+ const session = this.client.startSession({
170
+ causalConsistency: options.causalConsistency !== false
171
+ });
172
+ const transaction = new Transaction(session, {
173
+ cache: this.cache,
174
+ logger: this.logger,
175
+ lockManager: options.enableCacheLock === false ? null : this.lockManager,
176
+ timeout: options.timeout ?? options.maxDuration ?? this.defaultOptions.maxDuration
177
+ });
178
+ const originalEnd = transaction.end.bind(transaction);
179
+ transaction.end = async () => {
180
+ await originalEnd();
181
+ this.activeTransactions.delete(transaction.id);
182
+ };
183
+ this.activeTransactions.set(transaction.id, transaction);
184
+ return transaction;
185
+ }
186
+ /**
187
+ * Automatically manage the transaction lifecycle.
188
+ * @since v1.4.0
189
+ */
190
+ async withTransaction(callback, options = {}) {
191
+ const maxRetries = options.maxRetries ?? this.defaultOptions.maxRetries;
192
+ const retryDelay = options.retryDelay ?? this.defaultOptions.retryDelay;
193
+ const retryBackoff = options.retryBackoff ?? this.defaultOptions.retryBackoff;
194
+ const enableRetry = options.enableRetry ?? this.defaultOptions.enableRetry;
195
+ let lastError;
196
+ for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
197
+ const transaction = await this.startSession(options);
198
+ const startedAt = Date.now();
199
+ try {
200
+ await transaction.start();
201
+ const result = await callback(transaction);
202
+ await transaction.commit();
203
+ this.recordStats(Date.now() - startedAt, true);
204
+ return result;
205
+ } catch (error) {
206
+ lastError = error;
207
+ await transaction.abort();
208
+ this.recordStats(Date.now() - startedAt, false);
209
+ if (!enableRetry || attempt === maxRetries || !isTransientTransactionError(error)) {
210
+ throw error;
211
+ }
212
+ await sleep(retryDelay * Math.pow(retryBackoff, attempt));
213
+ } finally {
214
+ await transaction.end();
215
+ }
216
+ }
217
+ throw lastError instanceof Error ? lastError : new Error("Transaction failed.");
218
+ }
219
+ /**
220
+ * Get all active transactions.
221
+ * @since v1.4.0
222
+ */
223
+ getActiveTransactions() {
224
+ return [...this.activeTransactions.values()];
225
+ }
226
+ /**
227
+ * Abort all active transactions.
228
+ * @since v1.4.0
229
+ */
230
+ async abortAll() {
231
+ const transactions = this.getActiveTransactions();
232
+ for (const transaction of transactions) {
233
+ await transaction.abort();
234
+ await transaction.end();
235
+ this.activeTransactions.delete(transaction.id);
236
+ }
237
+ }
238
+ /**
239
+ * Get transaction statistics.
240
+ * @since v1.4.0
241
+ */
242
+ getStats() {
243
+ const averageDuration = this.durations.length === 0 ? 0 : this.durations.reduce((sum, item) => sum + item, 0) / this.durations.length;
244
+ return {
245
+ totalTransactions: this.stats.totalTransactions,
246
+ successfulTransactions: this.stats.successfulTransactions,
247
+ failedTransactions: this.stats.failedTransactions,
248
+ activeTransactions: this.activeTransactions.size,
249
+ averageDuration
250
+ };
251
+ }
252
+ recordStats(duration, success) {
253
+ this.stats.totalTransactions += 1;
254
+ if (success) {
255
+ this.stats.successfulTransactions += 1;
256
+ } else {
257
+ this.stats.failedTransactions += 1;
258
+ }
259
+ this.durations.push(duration);
260
+ if (this.durations.length > 100) {
261
+ this.durations.shift();
262
+ }
263
+ }
264
+ };
265
+ function stringifySessionId(id) {
266
+ if (typeof id === "string") {
267
+ return id;
268
+ }
269
+ if (typeof id === "object" && id !== null) {
270
+ const candidate = id;
271
+ if (typeof candidate.toHexString === "function") {
272
+ return candidate.toHexString();
273
+ }
274
+ if (candidate.id?.buffer) {
275
+ return Buffer.from(candidate.id.buffer).toString("hex");
276
+ }
277
+ if (typeof candidate.toString === "function") {
278
+ return candidate.toString();
279
+ }
280
+ }
281
+ return String(id);
282
+ }
283
+ function isTransientTransactionError(error) {
284
+ if (!error || typeof error !== "object") {
285
+ return false;
286
+ }
287
+ const candidate = error;
288
+ if (typeof candidate.hasErrorLabel === "function" && candidate.hasErrorLabel("TransientTransactionError")) {
289
+ return true;
290
+ }
291
+ return candidate.code === 112 || candidate.code === 117;
292
+ }
293
+ async function sleep(ms) {
294
+ await new Promise((resolve) => setTimeout(resolve, ms));
295
+ }
296
+
297
+ // src/entry/compat/transaction/TransactionManager.ts
298
+ module.exports = TransactionManager;