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('..');
1020async 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() {
5155async 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() {
9796async 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() {
145139async 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() {
186175async 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
0 commit comments