monsqlize 1.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.
- package/CHANGELOG.md +2474 -0
- package/LICENSE +21 -0
- package/README.md +1368 -0
- package/index.d.ts +1052 -0
- package/lib/cache.js +491 -0
- package/lib/common/cursor.js +58 -0
- package/lib/common/docs-urls.js +72 -0
- package/lib/common/index-options.js +222 -0
- package/lib/common/log.js +60 -0
- package/lib/common/namespace.js +21 -0
- package/lib/common/normalize.js +33 -0
- package/lib/common/page-result.js +42 -0
- package/lib/common/runner.js +56 -0
- package/lib/common/server-features.js +231 -0
- package/lib/common/shape-builders.js +26 -0
- package/lib/common/validation.js +49 -0
- package/lib/connect.js +76 -0
- package/lib/constants.js +54 -0
- package/lib/count-queue.js +187 -0
- package/lib/distributed-cache-invalidator.js +259 -0
- package/lib/errors.js +157 -0
- package/lib/index.js +352 -0
- package/lib/logger.js +224 -0
- package/lib/model/examples/test.js +114 -0
- package/lib/mongodb/common/accessor-helpers.js +44 -0
- package/lib/mongodb/common/agg-pipeline.js +32 -0
- package/lib/mongodb/common/iid.js +27 -0
- package/lib/mongodb/common/lexicographic-expr.js +52 -0
- package/lib/mongodb/common/shape.js +31 -0
- package/lib/mongodb/common/sort.js +38 -0
- package/lib/mongodb/common/transaction-aware.js +24 -0
- package/lib/mongodb/connect.js +178 -0
- package/lib/mongodb/index.js +458 -0
- package/lib/mongodb/management/admin-ops.js +199 -0
- package/lib/mongodb/management/bookmark-ops.js +166 -0
- package/lib/mongodb/management/cache-ops.js +49 -0
- package/lib/mongodb/management/collection-ops.js +386 -0
- package/lib/mongodb/management/database-ops.js +201 -0
- package/lib/mongodb/management/index-ops.js +474 -0
- package/lib/mongodb/management/index.js +16 -0
- package/lib/mongodb/management/namespace.js +30 -0
- package/lib/mongodb/management/validation-ops.js +267 -0
- package/lib/mongodb/queries/aggregate.js +133 -0
- package/lib/mongodb/queries/chain.js +623 -0
- package/lib/mongodb/queries/count.js +88 -0
- package/lib/mongodb/queries/distinct.js +68 -0
- package/lib/mongodb/queries/find-and-count.js +183 -0
- package/lib/mongodb/queries/find-by-ids.js +235 -0
- package/lib/mongodb/queries/find-one-by-id.js +170 -0
- package/lib/mongodb/queries/find-one.js +61 -0
- package/lib/mongodb/queries/find-page.js +565 -0
- package/lib/mongodb/queries/find.js +161 -0
- package/lib/mongodb/queries/index.js +49 -0
- package/lib/mongodb/writes/delete-many.js +181 -0
- package/lib/mongodb/writes/delete-one.js +173 -0
- package/lib/mongodb/writes/find-one-and-delete.js +193 -0
- package/lib/mongodb/writes/find-one-and-replace.js +222 -0
- package/lib/mongodb/writes/find-one-and-update.js +223 -0
- package/lib/mongodb/writes/increment-one.js +243 -0
- package/lib/mongodb/writes/index.js +41 -0
- package/lib/mongodb/writes/insert-batch.js +498 -0
- package/lib/mongodb/writes/insert-many.js +218 -0
- package/lib/mongodb/writes/insert-one.js +171 -0
- package/lib/mongodb/writes/replace-one.js +199 -0
- package/lib/mongodb/writes/result-handler.js +236 -0
- package/lib/mongodb/writes/update-many.js +205 -0
- package/lib/mongodb/writes/update-one.js +207 -0
- package/lib/mongodb/writes/upsert-one.js +190 -0
- package/lib/multi-level-cache.js +189 -0
- package/lib/operators.js +330 -0
- package/lib/redis-cache-adapter.js +237 -0
- package/lib/transaction/CacheLockManager.js +161 -0
- package/lib/transaction/DistributedCacheLockManager.js +239 -0
- package/lib/transaction/Transaction.js +314 -0
- package/lib/transaction/TransactionManager.js +266 -0
- package/lib/transaction/index.js +10 -0
- package/package.json +111 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事务管理器
|
|
3
|
+
* 负责管理 MongoDB 事务的生命周期
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const Transaction = require('./Transaction');
|
|
7
|
+
const CacheLockManager = require('./CacheLockManager');
|
|
8
|
+
|
|
9
|
+
class TransactionManager {
|
|
10
|
+
constructor(adapter, cache, logger, options = {}) {
|
|
11
|
+
this.adapter = adapter;
|
|
12
|
+
this.cache = cache;
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
this.options = {
|
|
15
|
+
maxRetries: options.maxRetries || 3,
|
|
16
|
+
retryDelay: options.retryDelay || 100,
|
|
17
|
+
retryBackoff: options.retryBackoff || 2,
|
|
18
|
+
defaultTimeout: options.defaultTimeout || 30000,
|
|
19
|
+
enableRetry: options.enableRetry !== false,
|
|
20
|
+
...options
|
|
21
|
+
};
|
|
22
|
+
this.activeTransactions = new Map();
|
|
23
|
+
|
|
24
|
+
// ✨ 统计信息
|
|
25
|
+
this.stats = {
|
|
26
|
+
totalTransactions: 0,
|
|
27
|
+
successfulTransactions: 0,
|
|
28
|
+
failedTransactions: 0,
|
|
29
|
+
readOnlyTransactions: 0,
|
|
30
|
+
writeTransactions: 0,
|
|
31
|
+
durations: []
|
|
32
|
+
};
|
|
33
|
+
this.maxStatsSamples = options.maxStatsSamples || 1000;
|
|
34
|
+
|
|
35
|
+
// 使用传入的 lockManager 或创建新的
|
|
36
|
+
if (options.lockManager) {
|
|
37
|
+
this.lockManager = options.lockManager;
|
|
38
|
+
} else {
|
|
39
|
+
// 初始化缓存锁管理器
|
|
40
|
+
this.lockManager = new CacheLockManager({
|
|
41
|
+
logger: this.logger,
|
|
42
|
+
maxDuration: options.lockMaxDuration || 300000,
|
|
43
|
+
cleanupInterval: options.lockCleanupInterval || 10000
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 将锁管理器设置到缓存实例
|
|
47
|
+
if (cache && typeof cache.setLockManager === 'function') {
|
|
48
|
+
cache.setLockManager(this.lockManager);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 创建一个新的事务会话
|
|
55
|
+
* @param {Object} options - 会话选项
|
|
56
|
+
* @returns {Promise<Transaction>}
|
|
57
|
+
*/
|
|
58
|
+
async startSession(options = {}) {
|
|
59
|
+
const session = this.adapter.client.startSession({
|
|
60
|
+
causalConsistency: options.causalConsistency !== false,
|
|
61
|
+
defaultTransactionOptions: {
|
|
62
|
+
readConcern: options.readConcern,
|
|
63
|
+
writeConcern: options.writeConcern,
|
|
64
|
+
readPreference: options.readPreference
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const transaction = new Transaction(session, {
|
|
69
|
+
cache: this.cache,
|
|
70
|
+
logger: this.logger,
|
|
71
|
+
lockManager: this.lockManager,
|
|
72
|
+
timeout: options.timeout || this.options.defaultTimeout,
|
|
73
|
+
...options
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this.activeTransactions.set(transaction.id, transaction);
|
|
77
|
+
|
|
78
|
+
return transaction;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 自动管理事务(推荐用法)
|
|
83
|
+
* @param {Function} callback - 事务回调函数
|
|
84
|
+
* @param {Object} options - 事务选项
|
|
85
|
+
* @returns {Promise<any>}
|
|
86
|
+
*/
|
|
87
|
+
async withTransaction(callback, options = {}) {
|
|
88
|
+
const maxRetries = options.maxRetries !== undefined
|
|
89
|
+
? options.maxRetries
|
|
90
|
+
: this.options.maxRetries;
|
|
91
|
+
|
|
92
|
+
const enableRetry = options.enableRetry !== undefined
|
|
93
|
+
? options.enableRetry
|
|
94
|
+
: this.options.enableRetry;
|
|
95
|
+
|
|
96
|
+
const retryDelay = options.retryDelay || this.options.retryDelay;
|
|
97
|
+
const retryBackoff = options.retryBackoff || this.options.retryBackoff;
|
|
98
|
+
|
|
99
|
+
let attempt = 0;
|
|
100
|
+
let lastError = null;
|
|
101
|
+
|
|
102
|
+
while (attempt <= maxRetries) {
|
|
103
|
+
const transaction = await this.startSession(options);
|
|
104
|
+
const startTime = Date.now();
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await transaction.start();
|
|
108
|
+
const result = await callback(transaction);
|
|
109
|
+
await transaction.commit();
|
|
110
|
+
|
|
111
|
+
// ✨ 记录统计信息(成功)
|
|
112
|
+
const duration = Date.now() - startTime;
|
|
113
|
+
this._recordStats(transaction, duration, true);
|
|
114
|
+
|
|
115
|
+
return result;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
lastError = error;
|
|
118
|
+
|
|
119
|
+
// 回滚事务
|
|
120
|
+
try {
|
|
121
|
+
await transaction.abort();
|
|
122
|
+
} catch (abortError) {
|
|
123
|
+
// 忽略回滚错误
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ✨ 记录统计信息(失败)
|
|
127
|
+
const duration = Date.now() - startTime;
|
|
128
|
+
this._recordStats(transaction, duration, false);
|
|
129
|
+
|
|
130
|
+
// 检查是否需要重试
|
|
131
|
+
if (enableRetry &&
|
|
132
|
+
attempt < maxRetries &&
|
|
133
|
+
this._isTransientError(error)) {
|
|
134
|
+
attempt++;
|
|
135
|
+
|
|
136
|
+
// 计算延迟时间(指数退避)
|
|
137
|
+
const delay = retryDelay * Math.pow(retryBackoff, attempt - 1);
|
|
138
|
+
|
|
139
|
+
this.logger?.info(`[TransactionManager] Retrying transaction (attempt ${attempt}/${maxRetries}) after ${delay}ms due to transient error: ${error.message}`);
|
|
140
|
+
|
|
141
|
+
// 等待后重试
|
|
142
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
143
|
+
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 不重试,抛出错误
|
|
148
|
+
throw error;
|
|
149
|
+
} finally {
|
|
150
|
+
await transaction.end();
|
|
151
|
+
this.activeTransactions.delete(transaction.id);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 达到最大重试次数
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Transaction failed after ${maxRetries} retries: ${lastError.message}`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 判断是否为瞬态错误(可重试)
|
|
163
|
+
* @private
|
|
164
|
+
*/
|
|
165
|
+
_isTransientError(error) {
|
|
166
|
+
if (!error) return false;
|
|
167
|
+
|
|
168
|
+
// MongoDB TransientTransactionError
|
|
169
|
+
if (error.hasErrorLabel && error.hasErrorLabel('TransientTransactionError')) {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 写冲突错误码
|
|
174
|
+
const transientErrorCodes = [
|
|
175
|
+
112, // WriteConflict
|
|
176
|
+
117, // ConflictingOperationInProgress
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
return transientErrorCodes.includes(error.code);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 获取所有活跃的事务
|
|
184
|
+
*/
|
|
185
|
+
getActiveTransactions() {
|
|
186
|
+
return Array.from(this.activeTransactions.values());
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 中止所有活跃的事务
|
|
191
|
+
*/
|
|
192
|
+
async abortAll() {
|
|
193
|
+
const transactions = this.getActiveTransactions();
|
|
194
|
+
await Promise.all(transactions.map(tx => tx.abort().catch(() => {})));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* ✨ 记录统计信息
|
|
199
|
+
* @param {Transaction} transaction - 事务实例
|
|
200
|
+
* @param {number} duration - 持续时间(毫秒)
|
|
201
|
+
* @param {boolean} success - 是否成功
|
|
202
|
+
* @private
|
|
203
|
+
*/
|
|
204
|
+
_recordStats(transaction, duration, success) {
|
|
205
|
+
this.stats.totalTransactions++;
|
|
206
|
+
|
|
207
|
+
if (success) {
|
|
208
|
+
this.stats.successfulTransactions++;
|
|
209
|
+
} else {
|
|
210
|
+
this.stats.failedTransactions++;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 记录只读/写入事务
|
|
214
|
+
if (transaction.hasWriteOperation) {
|
|
215
|
+
this.stats.writeTransactions++;
|
|
216
|
+
} else {
|
|
217
|
+
this.stats.readOnlyTransactions++;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 记录持续时间(环形缓冲)
|
|
221
|
+
this.stats.durations.push(duration);
|
|
222
|
+
if (this.stats.durations.length > this.maxStatsSamples) {
|
|
223
|
+
this.stats.durations.shift();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* ✨ 获取统计信息
|
|
229
|
+
* @returns {Object} 统计信息
|
|
230
|
+
*/
|
|
231
|
+
getStats() {
|
|
232
|
+
const { durations } = this.stats;
|
|
233
|
+
|
|
234
|
+
// 计算统计值
|
|
235
|
+
let avgDuration = 0;
|
|
236
|
+
let p95Duration = 0;
|
|
237
|
+
let p99Duration = 0;
|
|
238
|
+
|
|
239
|
+
if (durations.length > 0) {
|
|
240
|
+
avgDuration = durations.reduce((a, b) => a + b, 0) / durations.length;
|
|
241
|
+
|
|
242
|
+
const sorted = [...durations].sort((a, b) => a - b);
|
|
243
|
+
const p95Index = Math.floor(sorted.length * 0.95);
|
|
244
|
+
const p99Index = Math.floor(sorted.length * 0.99);
|
|
245
|
+
p95Duration = sorted[p95Index] || 0;
|
|
246
|
+
p99Duration = sorted[p99Index] || 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
...this.stats,
|
|
251
|
+
averageDuration: avgDuration,
|
|
252
|
+
p95Duration,
|
|
253
|
+
p99Duration,
|
|
254
|
+
successRate: this.stats.totalTransactions > 0
|
|
255
|
+
? (this.stats.successfulTransactions / this.stats.totalTransactions * 100).toFixed(2) + '%'
|
|
256
|
+
: '0%',
|
|
257
|
+
readOnlyRatio: this.stats.totalTransactions > 0
|
|
258
|
+
? (this.stats.readOnlyTransactions / this.stats.totalTransactions * 100).toFixed(2) + '%'
|
|
259
|
+
: '0%',
|
|
260
|
+
sampleCount: durations.length
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
module.exports = TransactionManager;
|
|
266
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "monsqlize",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight MongoDB ORM with multi-level caching, transaction support, and distributed features",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./lib/index.js",
|
|
11
|
+
"import": "./index.mjs",
|
|
12
|
+
"types": "./index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"types": "./index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"lib/",
|
|
19
|
+
"index.d.ts",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"CHANGELOG.md"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"mongodb",
|
|
26
|
+
"database",
|
|
27
|
+
"orm",
|
|
28
|
+
"cache",
|
|
29
|
+
"transaction",
|
|
30
|
+
"distributed",
|
|
31
|
+
"pagination",
|
|
32
|
+
"performance",
|
|
33
|
+
"mongodb-driver",
|
|
34
|
+
"nosql",
|
|
35
|
+
"query-builder",
|
|
36
|
+
"api"
|
|
37
|
+
],
|
|
38
|
+
"author": "vext.js Team",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/vextjs/monSQLize.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/vextjs/monSQLize/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/vextjs/monSQLize#readme",
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=14.0.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"test": "node test/run-tests.js",
|
|
53
|
+
"test:unit": "node test/run-tests.js",
|
|
54
|
+
"test:performance": "node test/performance/transaction-benchmark.js",
|
|
55
|
+
"test:coverage": "nyc --reporter=text --reporter=lcov node test/run-tests.js",
|
|
56
|
+
"coverage": "nyc --reporter=text --reporter=lcov --reporter=html node test/run-tests.js",
|
|
57
|
+
"benchmark": "node test/benchmark/run-benchmarks.js",
|
|
58
|
+
"test:compatibility": "node test/compatibility/run-node-test.js",
|
|
59
|
+
"test:compatibility:node": "node scripts/test-node-versions.js",
|
|
60
|
+
"test:compatibility:driver": "node scripts/test-driver-versions-simple.js",
|
|
61
|
+
"test:compatibility:driver:full": "node scripts/test-driver-versions-simple.js --drivers=4.17.2,5.9.2,6.17.0,7.0.0",
|
|
62
|
+
"test:compatibility:driver:quick": "node test/compatibility/run-driver-test.js",
|
|
63
|
+
"test:compatibility:server": "node scripts/test-server-versions.js --use-memory-server",
|
|
64
|
+
"test:compatibility:server:quick": "node test/compatibility/run-server-test.js",
|
|
65
|
+
"test:compatibility:server:docker": "node scripts/test-server-versions.js",
|
|
66
|
+
"lint": "eslint lib/ test/ --ext .js",
|
|
67
|
+
"lint:fix": "eslint lib/ test/ --ext .js --fix",
|
|
68
|
+
"postpublish": "echo '✅ 发布成功!请创建 GitHub Release: https://github.com/vextjs/monSQLize/releases/new?tag=v'$npm_package_version"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"ioredis": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"optionalDependencies": {
|
|
76
|
+
"ioredis": "^5.8.2"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"benchmark": "^2.1.4",
|
|
80
|
+
"chai": "^6.2.1",
|
|
81
|
+
"eslint": "^8.57.0",
|
|
82
|
+
"mocha": "^11.7.5",
|
|
83
|
+
"mongodb-memory-server": "^10.1.2",
|
|
84
|
+
"nyc": "^15.1.0",
|
|
85
|
+
"sinon": "^21.0.0"
|
|
86
|
+
},
|
|
87
|
+
"nyc": {
|
|
88
|
+
"include": [
|
|
89
|
+
"lib/**/*.js"
|
|
90
|
+
],
|
|
91
|
+
"exclude": [
|
|
92
|
+
"test/**",
|
|
93
|
+
"examples/**",
|
|
94
|
+
"scripts/**",
|
|
95
|
+
"reports/**"
|
|
96
|
+
],
|
|
97
|
+
"reporter": [
|
|
98
|
+
"text",
|
|
99
|
+
"lcov",
|
|
100
|
+
"html"
|
|
101
|
+
],
|
|
102
|
+
"check-coverage": true,
|
|
103
|
+
"lines": 70,
|
|
104
|
+
"statements": 70,
|
|
105
|
+
"functions": 70,
|
|
106
|
+
"branches": 65
|
|
107
|
+
},
|
|
108
|
+
"dependencies": {
|
|
109
|
+
"mongodb": "^6.17.0"
|
|
110
|
+
}
|
|
111
|
+
}
|