Skip to content

Commit 68ff2cb

Browse files
committed
fix:修复链接
1 parent 0ce20b8 commit 68ff2cb

8 files changed

Lines changed: 124 additions & 62 deletions

examples/aggregate.examples.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
const MonSQLize = require('../lib');
7+
const { stopMemoryServer } = require('../lib/mongodb/connect');
78

89
// ============================================================================
910
// 常量配置
@@ -1044,6 +1045,9 @@ async function runAllExamples() {
10441045
} catch (error) {
10451046
console.error('\n❌ 运行示例时出错:', error);
10461047
process.exit(1);
1048+
} finally {
1049+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
1050+
await stopMemoryServer();
10471051
}
10481052
}
10491053

examples/bookmarks.examples.js

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
// bookmarks.examples.js - Bookmark 维护 APIs 示例
22
// 用途:管理 findPage 的 bookmark 缓存,用于运维调试、性能优化
33

4-
const MonSQLize = require('..');
4+
const MonSQLize = require('../lib');
5+
const { stopMemoryServer } = require('../lib/mongodb/connect');
6+
7+
// MongoDB 连接配置 - 使用内存数据库方便独立运行
8+
const DB_CONFIG = {
9+
type: 'mongodb',
10+
databaseName: 'example_bookmarks',
11+
config: { useMemoryServer: true },
12+
cache: { enabled: true, maxSize: 1000 },
13+
defaults: { limit: 10, bookmarkTTL: 3600000 } // bookmark 缓存 1 小时
14+
};
515

616
/**
717
* 示例 1: 预热常用页面的 bookmark 缓存
@@ -10,17 +20,11 @@ const MonSQLize = require('..');
1020
async function example1_PrewarmBookmarks() {
1121
console.log('\n========== 示例 1: 预热 bookmark 缓存 ==========');
1222

13-
const client = new MonSQLize({
14-
type: 'mongodb',
15-
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
16-
databaseName: 'example_bookmarks',
17-
cache: { enabled: true, maxSize: 1000 },
18-
defaults: { limit: 10, bookmarkTTL: 3600000 } // bookmark 缓存 1 小时
19-
});
23+
const client = new MonSQLize(DB_CONFIG);
2024

2125
try {
22-
await client.connect();
23-
const products = client.db().collection('products');
26+
const { collection } = await client.connect();
27+
const products = collection('products');
2428

2529
// 预热前 3 页的 bookmark(常用的热点页面)
2630
const keyDims = {
@@ -51,16 +55,11 @@ async function example1_PrewarmBookmarks() {
5155
async function example2_ListBookmarks() {
5256
console.log('\n========== 示例 2: 列出 bookmark 缓存 ==========');
5357

54-
const client = new MonSQLize({
55-
type: 'mongodb',
56-
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
57-
databaseName: 'example_bookmarks',
58-
cache: { enabled: true, maxSize: 1000 }
59-
});
58+
const client = new MonSQLize(DB_CONFIG);
6059

6160
try {
62-
await client.connect();
63-
const products = client.db().collection('products');
61+
const { collection } = await client.connect();
62+
const products = collection('products');
6463

6564
// 先预热一些页面
6665
const keyDims = {
@@ -97,16 +96,11 @@ async function example2_ListBookmarks() {
9796
async function example3_ClearBookmarks() {
9897
console.log('\n========== 示例 3: 清除 bookmark 缓存 ==========');
9998

100-
const client = new MonSQLize({
101-
type: 'mongodb',
102-
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
103-
databaseName: 'example_bookmarks',
104-
cache: { enabled: true, maxSize: 1000 }
105-
});
99+
const client = new MonSQLize(DB_CONFIG);
106100

107101
try {
108-
await client.connect();
109-
const products = client.db().collection('products');
102+
const { collection } = await client.connect();
103+
const products = collection('products');
110104

111105
// 预热两个不同的查询
112106
const keyDims1 = { query: { category: 'books' }, sort: { title: 1 }, limit: 10 };
@@ -145,16 +139,11 @@ async function example3_ClearBookmarks() {
145139
async function example4_ClearAllBookmarks() {
146140
console.log('\n========== 示例 4: 清除所有 bookmark 缓存 ==========');
147141

148-
const client = new MonSQLize({
149-
type: 'mongodb',
150-
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
151-
databaseName: 'example_bookmarks',
152-
cache: { enabled: true, maxSize: 1000 }
153-
});
142+
const client = new MonSQLize(DB_CONFIG);
154143

155144
try {
156-
await client.connect();
157-
const products = client.db().collection('products');
145+
const { collection } = await client.connect();
146+
const products = collection('products');
158147

159148
// 预热多个查询
160149
await products.prewarmBookmarks({ sort: { _id: 1 }, limit: 10 }, [1, 2]);
@@ -186,16 +175,11 @@ async function example4_ClearAllBookmarks() {
186175
async function example5_CompleteWorkflow() {
187176
console.log('\n========== 示例 5: 完整工作流 ==========');
188177

189-
const client = new MonSQLize({
190-
type: 'mongodb',
191-
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
192-
databaseName: 'example_bookmarks',
193-
cache: { enabled: true, maxSize: 1000 }
194-
});
178+
const client = new MonSQLize(DB_CONFIG);
195179

196180
try {
197-
await client.connect();
198-
const orders = client.db().collection('orders');
181+
const { collection } = await client.connect();
182+
const orders = collection('orders');
199183

200184
const keyDims = {
201185
query: { status: 'pending' },
@@ -245,8 +229,7 @@ async function runAll() {
245229
console.log('========================================');
246230
console.log('MonSQLize Bookmark 维护 APIs 示例');
247231
console.log('========================================');
248-
console.log('\n注意:这些示例需要 MongoDB 服务运行在 localhost:27017');
249-
console.log('或通过环境变量 MONGODB_URI 指定连接串\n');
232+
console.log('\n使用内存数据库运行示例,无需外部 MongoDB 服务\n');
250233

251234
try {
252235
await example1_PrewarmBookmarks();
@@ -260,11 +243,11 @@ async function runAll() {
260243
console.log('========================================\n');
261244
} catch (error) {
262245
console.error('\n❌ 示例运行失败:', error.message);
263-
console.error('\n请确保:');
264-
console.error(' 1. MongoDB 服务正在运行');
265-
console.error(' 2. 连接字符串正确(默认: mongodb://localhost:27017)');
266-
console.error(' 3. 数据库可访问\n');
246+
console.error('错误堆栈:', error.stack);
267247
process.exit(1);
248+
} finally {
249+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
250+
await stopMemoryServer();
268251
}
269252
}
270253

examples/count.examples.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
const MonSQLize = require('../lib');
7+
const { stopMemoryServer } = require('../lib/mongodb/connect');
78

89
// ============================================================================
910
// 常量配置
@@ -152,6 +153,8 @@ async function example1_BasicCount() {
152153

153154
} catch (error) {
154155
console.error('示例 1 出错:', error.message);
156+
} finally {
157+
await monSQLize.close();
155158
}
156159
}
157160

@@ -199,11 +202,13 @@ async function example2_ConditionalCount() {
199202

200203
} catch (error) {
201204
console.error('示例 2 出错:', error.message);
205+
} finally {
206+
await monSQLize.close();
202207
}
203208
}
204209

205210
/**
206-
* 示例 3: 多集合统计 - 业务报表数据
211+
* 示例 3: 多集合统计 - 业务报表
207212
*/
208213
async function example3_MultiCollectionStats() {
209214
console.log('\n=== 示例 3: 多集合统计 ===');
@@ -253,11 +258,13 @@ async function example3_MultiCollectionStats() {
253258

254259
} catch (error) {
255260
console.error('示例 3 出错:', error.message);
261+
} finally {
262+
await monSQLize.close();
256263
}
257264
}
258265

259266
/**
260-
* 示例 4: 日期范围统计 - 时间段分析
267+
* 示例 4: 日期范围统计
261268
*/
262269
async function example4_DateRangeCount() {
263270
console.log('\n=== 示例 4: 日期范围统计 ===');
@@ -304,11 +311,13 @@ async function example4_DateRangeCount() {
304311

305312
} catch (error) {
306313
console.error('示例 4 出错:', error.message);
314+
} finally {
315+
await monSQLize.close();
307316
}
308317
}
309318

310319
/**
311-
* 示例 5: 数组字段统计 - 标签和分类
320+
* 示例 5: 数组字段统计
312321
*/
313322
async function example5_ArrayFieldCount() {
314323
console.log('\n=== 示例 5: 数组字段统计 ===');
@@ -343,11 +352,13 @@ async function example5_ArrayFieldCount() {
343352

344353
} catch (error) {
345354
console.error('示例 5 出错:', error.message);
355+
} finally {
356+
await monSQLize.close();
346357
}
347358
}
348359

349360
/**
350-
* 示例 6: 缓存统计 - 性能优化
361+
* 示例 6: 缓存统计
351362
*/
352363
async function example6_CachedCount() {
353364
console.log('\n=== 示例 6: 缓存统计 ===');
@@ -393,11 +404,13 @@ async function example6_CachedCount() {
393404

394405
} catch (error) {
395406
console.error('示例 6 出错:', error.message);
407+
} finally {
408+
await monSQLize.close();
396409
}
397410
}
398411

399412
/**
400-
* 示例 7: 查询执行计划 - 性能分析
413+
* 示例 7: 查询执行计划
401414
*/
402415
async function example7_ExplainCount() {
403416
console.log('\n=== 示例 7: 查询执行计划 ===');
@@ -429,6 +442,8 @@ async function example7_ExplainCount() {
429442

430443
} catch (error) {
431444
console.error('示例 7 出错:', error.message);
445+
} finally {
446+
await monSQLize.close();
432447
}
433448
}
434449

@@ -463,6 +478,8 @@ async function example8_HintCount() {
463478

464479
} catch (error) {
465480
console.error('示例 8 出错:', error.message);
481+
} finally {
482+
await monSQLize.close();
466483
}
467484
}
468485

@@ -507,11 +524,13 @@ async function example9_ErrorHandling() {
507524

508525
} catch (error) {
509526
console.error('示例 9 出错:', error.message);
527+
} finally {
528+
await monSQLize.close();
510529
}
511530
}
512531

513532
/**
514-
* 示例 10: 最佳实践 - 综合示例
533+
* 示例 10: 最佳实践
515534
*/
516535
async function example10_BestPractices() {
517536
console.log('\n=== 示例 10: 最佳实践 ===');
@@ -579,6 +598,8 @@ async function example10_BestPractices() {
579598

580599
} catch (error) {
581600
console.error('示例 10 出错:', error.message);
601+
} finally {
602+
await monSQLize.close();
582603
}
583604
}
584605

@@ -608,12 +629,22 @@ async function runAllExamples() {
608629

609630
} catch (error) {
610631
console.error('\n❌ 示例运行失败:', error.message);
632+
console.error('错误堆栈:', error.stack);
633+
throw error; // 重新抛出错误,让外层处理
634+
} finally {
635+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
636+
await stopMemoryServer();
611637
}
612638
}
613639

614640
// 如果直接运行此文件,则执行所有示例
615641
if (require.main === module) {
616-
runAllExamples().catch(console.error);
642+
runAllExamples()
643+
.then(() => process.exit(0))
644+
.catch(error => {
645+
console.error('Fatal error:', error);
646+
process.exit(1);
647+
});
617648
}
618649

619650
module.exports = {

examples/distinct.examples.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
const MonSQLize = require('../lib');
7+
const { stopMemoryServer } = require('../lib/mongodb/connect');
78

89
// ============================================================================
910
// 常量配置
@@ -919,7 +920,13 @@ async function runAllExamples() {
919920
// ============================================================================
920921

921922
if (require.main === module) {
922-
runAllExamples().catch(console.error);
923+
runAllExamples()
924+
.catch(console.error)
925+
.finally(async () => {
926+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
927+
const { stopMemoryServer } = require('../lib/mongodb/connect');
928+
await stopMemoryServer();
929+
});
923930
}
924931

925932
module.exports = {

examples/explain.examples.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
const MonSQLize = require('../lib');
16+
const { stopMemoryServer } = require('../lib/mongodb/connect');
1617

1718
// MongoDB 连接配置 - 使用内存数据库方便独立运行
1819
const DB_CONFIG = {
@@ -353,11 +354,13 @@ async function runAllExamples() {
353354
console.log("- 查询优化最佳实践: 见项目文档");
354355
console.log("\n");
355356

356-
process.exit(0);
357357
} catch (error) {
358358
console.error("\n❌ 示例运行失败:", error.message);
359359
console.error(error.stack);
360360
process.exit(1);
361+
} finally {
362+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
363+
await stopMemoryServer();
361364
}
362365
}
363366

examples/find.examples.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
const MonSQLize = require('../lib');
7+
const { stopMemoryServer } = require('../lib/mongodb/connect');
78

89
// ============================================================================
910
// 常量配置
@@ -932,6 +933,9 @@ async function runAllExamples() {
932933
await msqCleanup.connect();
933934
await cleanupExampleData(msqCleanup, needCleanup);
934935
await msqCleanup.close();
936+
937+
// 显式停止 Memory Server,否则 Node.js 进程会卡住
938+
await stopMemoryServer();
935939
}
936940
}
937941

0 commit comments

Comments
 (0)