monsqlize 1.3.1 → 2.0.1

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 (114) hide show
  1. package/CHANGELOG.md +506 -235
  2. package/LICENSE +201 -21
  3. package/README.md +542 -928
  4. package/changelogs/README.md +163 -0
  5. package/changelogs/v2.0.0.md +222 -0
  6. package/changelogs/v2.0.1.md +39 -0
  7. package/dist/cjs/index.cjs +10788 -0
  8. package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
  9. package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
  10. package/dist/cjs/transaction/Transaction.cjs +158 -0
  11. package/dist/cjs/transaction/TransactionManager.cjs +298 -0
  12. package/dist/esm/index.mjs +10838 -0
  13. package/dist/types/base.d.ts +81 -0
  14. package/dist/types/collection.d.ts +1031 -0
  15. package/dist/types/expression.d.ts +115 -0
  16. package/dist/types/index.d.ts +23 -0
  17. package/dist/types/lock.d.ts +74 -0
  18. package/dist/types/model.d.ts +530 -0
  19. package/dist/types/mongodb.d.ts +49 -0
  20. package/dist/types/monsqlize.d.ts +491 -0
  21. package/dist/types/pool.d.ts +84 -0
  22. package/dist/types/runtime.d.ts +362 -0
  23. package/dist/types/saga.d.ts +143 -0
  24. package/dist/types/slow-query-log.d.ts +126 -0
  25. package/dist/types/sync.d.ts +103 -0
  26. package/dist/types/transaction.d.ts +132 -0
  27. package/package.json +120 -117
  28. package/index.d.ts +0 -1289
  29. package/lib/cache.js +0 -491
  30. package/lib/common/cursor.js +0 -58
  31. package/lib/common/docs-urls.js +0 -72
  32. package/lib/common/index-options.js +0 -222
  33. package/lib/common/log.js +0 -60
  34. package/lib/common/namespace.js +0 -21
  35. package/lib/common/normalize.js +0 -33
  36. package/lib/common/page-result.js +0 -42
  37. package/lib/common/runner.js +0 -56
  38. package/lib/common/server-features.js +0 -231
  39. package/lib/common/shape-builders.js +0 -26
  40. package/lib/common/validation.js +0 -112
  41. package/lib/connect.js +0 -76
  42. package/lib/constants.js +0 -54
  43. package/lib/count-queue.js +0 -187
  44. package/lib/distributed-cache-invalidator.js +0 -259
  45. package/lib/errors.js +0 -167
  46. package/lib/index.js +0 -461
  47. package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -211
  48. package/lib/infrastructure/ssh-tunnel.js +0 -40
  49. package/lib/infrastructure/uri-parser.js +0 -35
  50. package/lib/lock/Lock.js +0 -66
  51. package/lib/lock/errors.js +0 -27
  52. package/lib/lock/index.js +0 -12
  53. package/lib/logger.js +0 -224
  54. package/lib/model/examples/test.js +0 -114
  55. package/lib/mongodb/common/accessor-helpers.js +0 -58
  56. package/lib/mongodb/common/agg-pipeline.js +0 -32
  57. package/lib/mongodb/common/iid.js +0 -27
  58. package/lib/mongodb/common/lexicographic-expr.js +0 -52
  59. package/lib/mongodb/common/shape.js +0 -31
  60. package/lib/mongodb/common/sort.js +0 -38
  61. package/lib/mongodb/common/transaction-aware.js +0 -24
  62. package/lib/mongodb/connect.js +0 -233
  63. package/lib/mongodb/index.js +0 -591
  64. package/lib/mongodb/management/admin-ops.js +0 -199
  65. package/lib/mongodb/management/bookmark-ops.js +0 -166
  66. package/lib/mongodb/management/cache-ops.js +0 -49
  67. package/lib/mongodb/management/collection-ops.js +0 -386
  68. package/lib/mongodb/management/database-ops.js +0 -201
  69. package/lib/mongodb/management/index-ops.js +0 -474
  70. package/lib/mongodb/management/index.js +0 -16
  71. package/lib/mongodb/management/namespace.js +0 -30
  72. package/lib/mongodb/management/validation-ops.js +0 -267
  73. package/lib/mongodb/queries/aggregate.js +0 -142
  74. package/lib/mongodb/queries/chain.js +0 -630
  75. package/lib/mongodb/queries/count.js +0 -98
  76. package/lib/mongodb/queries/distinct.js +0 -77
  77. package/lib/mongodb/queries/find-and-count.js +0 -192
  78. package/lib/mongodb/queries/find-by-ids.js +0 -235
  79. package/lib/mongodb/queries/find-one-by-id.js +0 -170
  80. package/lib/mongodb/queries/find-one.js +0 -70
  81. package/lib/mongodb/queries/find-page.js +0 -577
  82. package/lib/mongodb/queries/find.js +0 -170
  83. package/lib/mongodb/queries/index.js +0 -50
  84. package/lib/mongodb/queries/watch.js +0 -537
  85. package/lib/mongodb/writes/delete-many.js +0 -190
  86. package/lib/mongodb/writes/delete-one.js +0 -182
  87. package/lib/mongodb/writes/find-one-and-delete.js +0 -202
  88. package/lib/mongodb/writes/find-one-and-replace.js +0 -238
  89. package/lib/mongodb/writes/find-one-and-update.js +0 -239
  90. package/lib/mongodb/writes/increment-one.js +0 -252
  91. package/lib/mongodb/writes/index.js +0 -41
  92. package/lib/mongodb/writes/insert-batch.js +0 -507
  93. package/lib/mongodb/writes/insert-many.js +0 -227
  94. package/lib/mongodb/writes/insert-one.js +0 -180
  95. package/lib/mongodb/writes/replace-one.js +0 -215
  96. package/lib/mongodb/writes/result-handler.js +0 -236
  97. package/lib/mongodb/writes/update-many.js +0 -221
  98. package/lib/mongodb/writes/update-one.js +0 -223
  99. package/lib/mongodb/writes/upsert-one.js +0 -206
  100. package/lib/multi-level-cache.js +0 -189
  101. package/lib/operators.js +0 -330
  102. package/lib/redis-cache-adapter.js +0 -237
  103. package/lib/slow-query-log/base-storage.js +0 -69
  104. package/lib/slow-query-log/batch-queue.js +0 -96
  105. package/lib/slow-query-log/config-manager.js +0 -195
  106. package/lib/slow-query-log/index.js +0 -237
  107. package/lib/slow-query-log/mongodb-storage.js +0 -323
  108. package/lib/slow-query-log/query-hash.js +0 -38
  109. package/lib/transaction/CacheLockManager.js +0 -161
  110. package/lib/transaction/DistributedCacheLockManager.js +0 -474
  111. package/lib/transaction/Transaction.js +0 -314
  112. package/lib/transaction/TransactionManager.js +0 -266
  113. package/lib/transaction/index.js +0 -10
  114. package/lib/utils/objectid-converter.js +0 -566
@@ -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;