mm_mongodb 1.3.8 → 1.4.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 (5) hide show
  1. package/cache.js +359 -0
  2. package/index.js +38 -100
  3. package/package.json +36 -36
  4. package/test.js +35 -14
  5. package/test2.js +57 -0
package/cache.js ADDED
@@ -0,0 +1,359 @@
1
+ /**
2
+ * @fileOverview mongodb_cache缓存帮助类函数
3
+ * @author <a href="http://qww.elins.cn">邱文武</a>
4
+ * @version 1.2
5
+ */
6
+ const {
7
+ MongoDB
8
+ } = require('./index.js');
9
+
10
+ /* 创建Mongodb_cache帮助类函数 */
11
+ /// 构造函数
12
+ class Mongodb_cache extends MongoDB {
13
+ /**
14
+ * @description 创建Mongodb_cache帮助类函数 (构造函数)
15
+ * @param {String} scope 作用域
16
+ * @param {String} dir 当前路径
17
+ * @constructor
18
+ */
19
+ constructor(scope, dir, config) {
20
+ super(scope, dir, config);
21
+
22
+ // 消息订阅器
23
+ this.conn_rss;
24
+
25
+ // 订阅回调函数
26
+ this.func_list = {};
27
+ }
28
+ }
29
+
30
+ /**
31
+ * 消息订阅
32
+ * @param {String} channel 订阅的频道
33
+ * @param {Function} func 操作回调函数,可判断成功与否
34
+ */
35
+ Mongodb_cache.prototype.subscribe = function(channel, func) {
36
+ this.conn_rss.subscribe(channel);
37
+ this.func_list[channel] = func;
38
+ };
39
+
40
+ /**
41
+ * 取消订阅
42
+ * @param {String} channel 订阅的频道
43
+ */
44
+ Mongodb_cache.prototype.unsubscribe = function(channel) {
45
+ this.conn_rss.unsubscribe(channel);
46
+ delete this.func_list[channel];
47
+ };
48
+
49
+ /**
50
+ * 发布消息
51
+ * @param {String} channel 频道
52
+ * @param {String} message 订阅的消息
53
+ * @param {Function} func 消息发布成功回调函数
54
+ */
55
+ Mongodb_cache.prototype.publish = function(channel, message, func) {
56
+ this.conn.publish(channel, message, func);
57
+ };
58
+
59
+ /**
60
+ * 订阅消息处理, 如果没有订阅函数,则其他的订阅消息会进入该函数
61
+ * @param {String} channel
62
+ * @param {String} message
63
+ */
64
+ Mongodb_cache.prototype.main = async function(channel, message) {};
65
+
66
+ /**
67
+ * 消息通知
68
+ * @param {String} channel 频道
69
+ * @param {String} message 消息
70
+ */
71
+ Mongodb_cache.prototype.message = function(channel, message) {
72
+ // console.log("我接收到信息了" + message);
73
+ var func = this.func_list[channel];
74
+ if (func) {
75
+ func(message);
76
+ } else {
77
+ this.main(channel, message);
78
+ }
79
+ };
80
+
81
+ /**z
82
+ * @description 增加整数值(负数为减)
83
+ * @param {String} key 键
84
+ * @param {Number} num 数值
85
+ * @return {Promise|Number} 计算后的结果
86
+ */
87
+ Mongodb_cache.prototype.addInt = function(key, num) {
88
+ if (num > 0) {
89
+ return this.conn.incrby(key, num);
90
+ } else {
91
+ return this.conn.decrby(key, -num);
92
+ }
93
+ };
94
+
95
+ /**
96
+ * @description 增加浮点数值(负数为减)
97
+ * @param {String} key 键
98
+ * @param {Number} num 数值
99
+ * @return {Promise|Number} 计算后的结果
100
+ */
101
+ Mongodb_cache.prototype.addFloat = function(key, num) {
102
+ if (num > 0) {
103
+ return this.conn.incrbyfloat(key, num);
104
+ } else {
105
+ return this.conn.decrby(key, -num);
106
+ };
107
+ };
108
+
109
+ /**
110
+ * @description 增加字符串值到指定缓存
111
+ * @param {String} key 键
112
+ * @param {String} str 添加的字符串
113
+ * @return {Promise|String} 添加后的字符串
114
+ */
115
+ Mongodb_cache.prototype.addStr = function(key, str) {
116
+ return this.conn.append(key, str);
117
+ };
118
+
119
+ /**
120
+ * @description 删除缓存
121
+ * @param {String} key 键
122
+ * @return {Promise|Boolean} 成功返回true,失败返回false
123
+ */
124
+ Mongodb_cache.prototype.del = function(key) {
125
+ return this.conn.del(key);
126
+ };
127
+
128
+ /**
129
+ * @description 修改缓存
130
+ * @param {String} key 键
131
+ * @param {Object} value 值
132
+ * @param {Number} seconds 秒
133
+ * @return {Object} 值
134
+ */
135
+ Mongodb_cache.prototype.set = async function(key, value, seconds) {
136
+ var ret;
137
+ ret = await this.conn.set(key, value);
138
+ if (seconds) {
139
+ ret = await this.conn.expire(key, seconds);
140
+ }
141
+ return ret
142
+ };
143
+
144
+ /**
145
+ * @description 添加缓存
146
+ * @param {String} key 键
147
+ * @param {Object} value 值
148
+ * @param {Number} seconds 秒
149
+ * @return {Promise|Object} 值
150
+ */
151
+ Mongodb_cache.prototype.add = async function(key, value, seconds) {
152
+ var ret;
153
+ var has = await this.conn.exists(key);
154
+ if (!has) {
155
+ ret = await this.conn.set(key, value);
156
+ }
157
+ if (seconds) {
158
+ ret = await this.conn.expire(key, seconds);
159
+ }
160
+ return ret
161
+ };
162
+
163
+ /**
164
+ * @description 查询缓存
165
+ * @param {String} key 键
166
+ * @return {Promise|Object} 查询值
167
+ */
168
+ Mongodb_cache.prototype.get = function(key) {
169
+ return this.conn.get(key);
170
+ };
171
+
172
+ /**
173
+ * @description 判断键是否存在
174
+ * @param {String} key 键
175
+ * @return {Promise|Boolean} 有返回true, 没有返回false
176
+ */
177
+ Mongodb_cache.prototype.has = function(key) {
178
+ return this.conn.exists(key);
179
+ };
180
+
181
+ /**
182
+ * @description 查询缓存的字符串中的一段字符串
183
+ * @param {String} key 键
184
+ * @param {Number} start 开始位置
185
+ * @param {Number} end 结束位置
186
+ * @return {Promise|String} 查询值
187
+ */
188
+ Mongodb_cache.prototype.getrange = function(key, start, end) {
189
+ return this.conn.getrange(key, start, end);
190
+ };
191
+
192
+ /**
193
+ * @description 在值的指定位置开始增加一段字符串
194
+ * @param {String} key 键
195
+ * @param {Number} index 开始位置
196
+ * @param {String} value 变更的值
197
+ * @return {Promise|Number} 字符串长度
198
+ */
199
+ Mongodb_cache.prototype.setrange = function(key, index, value) {
200
+ return this.conn.getrange(key, index, value);
201
+ };
202
+
203
+ /**
204
+ * @description 清空缓存
205
+ * @param {String} key 键, 为空则清空所有
206
+ * @return {Promise|Array} 执行结果
207
+ */
208
+ Mongodb_cache.prototype.clear = function(key) {
209
+ if (key) {
210
+ return this.conn.del(key);
211
+ } else {
212
+ return this.conn.flushdb();
213
+ }
214
+ };
215
+
216
+ /**
217
+ * @description 排序
218
+ * @param {String} key 键
219
+ * @param {String} way = [asc|desc]排序方式, 可以为空
220
+ * @param {String} obj_key 排序成员的键
221
+ * @return {Promise|Array} 排序后的数组
222
+ */
223
+ Mongodb_cache.prototype.sort = function(key) {
224
+ return this.conn.sort(key);
225
+ };
226
+
227
+ /**
228
+ * @description 获取所有键名
229
+ * @param {String} key 键 支持*号, 前面加*表示后面名称一致, 前后加*表示包含名称, 后面加*表示前面名称一致
230
+ * @return {Promise|Array} 键数组
231
+ */
232
+ Mongodb_cache.prototype.keys = function(key) {
233
+ if (!key) {
234
+ key = "*";
235
+ }
236
+ return this.conn.keys(key);
237
+ };
238
+
239
+ /**
240
+ * @description 修改数组缓存
241
+ * @param {String} key 键
242
+ * @param {Object} value 值
243
+ * @param {Number} seconds 秒
244
+ * @return {Promise|Array} 执行结果
245
+ */
246
+ Mongodb_cache.prototype.list_set = function(key, value, seconds) {
247
+ return this.conn.rpush(key, value);
248
+ };
249
+
250
+ /**
251
+ * @description 数组缓存追加对象
252
+ * @param {String} key 键
253
+ * @param {Object} value 值
254
+ * @return {Promise|String} 追加后的数组
255
+ */
256
+ Mongodb_cache.prototype.list_push = function(key, value) {
257
+ return this.conn.rpush(key, value);
258
+ };
259
+
260
+ /**
261
+ * @description 判断成员是否存在
262
+ * @param {String} key 键
263
+ * @param {Object} value 值
264
+ * @return {Promise|Boolean} 存在返回true, 否则返回false
265
+ */
266
+ Mongodb_cache.prototype.list_has = async function(key, value) {
267
+ if (!value) {
268
+ throw '值不能为空';
269
+ }
270
+ var arr = await this.list_get(key);
271
+ var has = false;
272
+ var len = arr.length;
273
+ for (var i = 0; i < len; i++) {
274
+ if (value == arr[i]) {
275
+ has = true;
276
+ break;
277
+ }
278
+ }
279
+ return has;
280
+ };
281
+
282
+ /**
283
+ * @description 查询数组缓存
284
+ * @param {String} key 键
285
+ * @param {Number} start 起始位置
286
+ * @param {Number} end 结束位置
287
+ * @return {Promise|Array} 查询到的数组
288
+ */
289
+ Mongodb_cache.prototype.list_get = function(key, start, end) {
290
+ if (start === undefined) {
291
+ start = 0;
292
+ }
293
+ if (end === undefined) {
294
+ end = -1;
295
+ }
296
+ return this.conn.lrange(key, start, end);
297
+ };
298
+
299
+ /**
300
+ * @description 清空数组缓存
301
+ * @param {String} key 键
302
+ * @param {Array} value 新成员, 没有则删除数组
303
+ * @return {Promise|Boolean} 成功返回true,是否返回false
304
+ */
305
+ Mongodb_cache.prototype.list_clear = async function(key, value) {
306
+ var ret;
307
+ if (key) {
308
+ if (value) {
309
+ // 如果有值则删除其他成员,保留新值
310
+ try {
311
+ var len = await this.list_push(key, value);
312
+ ret = this.conn.ltrim(key, len - value.length, len - 1);
313
+ } catch (e) {
314
+
315
+ }
316
+ } else {
317
+ // 直接删除键
318
+ return await this.conn.del(key);
319
+ }
320
+ } else {
321
+ throw '键不能为空';
322
+ }
323
+ return ret
324
+ };
325
+
326
+ /**
327
+ * @description 导出Mongodb_cache类
328
+ */
329
+ exports.Mongodb_cache = Mongodb_cache;
330
+
331
+ /**
332
+ * @description mongodb_cache连接池
333
+ */
334
+ if (!$.pool.mongodb_cache) {
335
+ $.pool.mongodb_cache = {};
336
+ }
337
+
338
+ /**
339
+ * @description 缓存管理器,用于创建缓存
340
+ * @param {String} scope 作用域
341
+ * @param {String} dir 当前路径
342
+ * @return {Object} 返回一个缓存类
343
+ */
344
+ function mongodb_cache_admin(scope, dir) {
345
+ if (!scope) {
346
+ scope = $.val.scope
347
+ }
348
+ var obj = $.pool.mongodb_cache[scope];
349
+ if (!obj) {
350
+ $.pool.mongodb_cache[scope] = new Mongodb_cache(scope, dir);
351
+ obj = $.pool.mongodb_cache[scope];
352
+ }
353
+ return obj;
354
+ }
355
+
356
+ /**
357
+ * @description 导出mongodb_cache管理函数
358
+ */
359
+ exports.mongodb_cache_admin = mongodb_cache_admin;
package/index.js CHANGED
@@ -16,7 +16,7 @@ class MongoDB {
16
16
  * @param {String} dir 当前路径
17
17
  * @constructor
18
18
  */
19
- constructor(scope, dir) {
19
+ constructor(scope, dir, config) {
20
20
  // 作用域
21
21
  this.scope;
22
22
  if (scope) {
@@ -29,9 +29,7 @@ class MongoDB {
29
29
  if (dir) {
30
30
  this.dir = dir;
31
31
  }
32
-
33
- // 配置参数
34
- MongoDB.prototype.config = {
32
+ this.config = Object.assign({
35
33
  // 服务器地址
36
34
  host: "localhost",
37
35
  // 端口号
@@ -47,13 +45,14 @@ class MongoDB {
47
45
  // 使用新的url分析器
48
46
  useNewUrlParser: true,
49
47
  // 自动重连接
50
- auto_reconnect: true,
48
+ // auto_reconnect: true,
51
49
  // 使用统一拓扑
52
50
  useUnifiedTopology: true,
51
+ // useCreateIndex: true,
53
52
  // 连接池数
54
- poolSize: 10
53
+ // poolSize: 10
55
54
  }
56
- };
55
+ }, config);
57
56
  // 连接地址
58
57
  this.url;
59
58
  // 数据库连接器
@@ -61,7 +60,7 @@ class MongoDB {
61
60
  // 操作的数据库
62
61
  this.db;
63
62
  // 表名
64
- this.table = "";
63
+ this.table = "cache";
65
64
  /**
66
65
  * 页码
67
66
  */
@@ -107,20 +106,20 @@ MongoDB.prototype.open = async function() {
107
106
  if (!this.url) {
108
107
  this.setUrl();
109
108
  }
110
- var _this = this;
111
- // 返回一个 Promise
112
- var p = new Promise((resolve, reject) => {
113
- MongoClient.connect(_this.url, _this.config.options, function(err, conn) {
114
- if (err) {
115
- reject(err);
116
- } else {
117
- resolve(conn);
118
- }
119
- });
120
- });
121
- _this.conn = await p;
122
- _this.db = _this.conn.db(_this.config.database);
123
- return _this.conn == true;
109
+ if (!this.conn) {
110
+ this.conn = await MongoClient.connect(this.url, this.config.options);
111
+ this.db = this.conn.db(this.config.database);
112
+ }
113
+ return this.conn == true;
114
+ };
115
+
116
+ /**
117
+ * 关闭数据库连接
118
+ */
119
+ MongoDB.prototype.close = function() {
120
+ if (this.conn) {
121
+ this.conn.close();
122
+ }
124
123
  };
125
124
 
126
125
  /**
@@ -129,21 +128,12 @@ MongoDB.prototype.open = async function() {
129
128
  * @return {Promise|Object} 执行结果
130
129
  */
131
130
  MongoDB.prototype.addTable = function(table) {
132
- var _this = this;
133
131
  if (table) {
134
132
  this.table = table;
135
133
  } else {
136
134
  table = this.table;
137
135
  }
138
- return new Promise((resolve, reject) => {
139
- _this.db.createCollection(table, function(err, ret) {
140
- if (err) {
141
- reject(err);
142
- } else {
143
- resolve(ret);
144
- }
145
- });
146
- });
136
+ return this.db.createCollection(table);
147
137
  };
148
138
 
149
139
  /**
@@ -152,16 +142,7 @@ MongoDB.prototype.addTable = function(table) {
152
142
  * @return {Promise|Object} 执行结果
153
143
  */
154
144
  MongoDB.prototype.addObj = function(obj) {
155
- var _this = this;
156
- return new Promise((resolve, reject) => {
157
- _this.db.collection(_this.table).insertOne(obj, function(err, ret) {
158
- if (err) {
159
- reject(err);
160
- } else {
161
- resolve(ret);
162
- }
163
- });
164
- });
145
+ return this.db.collection(this.table).insertOne(obj);
165
146
  };
166
147
 
167
148
  /**
@@ -170,83 +151,40 @@ MongoDB.prototype.addObj = function(obj) {
170
151
  * @return {Promise|Object} 执行结果
171
152
  */
172
153
  MongoDB.prototype.addList = function(list) {
173
- var _this = this;
174
- return new Promise((resolve, reject) => {
175
- _this.db.collection(_this.table).insertMany(list, function(err, ret) {
176
- if (err) {
177
- reject(err);
178
- } else {
179
- resolve(ret);
180
- }
181
- });
182
- });
154
+ return this.db.collection(this.table).insertMany(list);
183
155
  };
184
156
 
185
157
  /**
186
158
  * @description 删除缓存
187
- * @param {Object} query 对象
159
+ * @param {Object} query 查询条件对象
188
160
  * @return {Promise|Object} 执行结果
189
161
  */
190
162
  MongoDB.prototype.del = function(query) {
191
- var _this = this;
192
- return new Promise((resolve, reject) => {
193
- _this.db.collection(_this.table).remove(query, function(err, ret) {
194
- if (err) {
195
- reject(err);
196
- } else {
197
- resolve(ret);
198
- }
199
- });
200
- });
163
+ return this.db.collection(this.table).remove(query);
201
164
  };
202
165
 
203
166
  /**
204
167
  * @description 修改缓存
205
- * @param {Object} query 对象
206
- * @param {Object} obj 对象
168
+ * @param {Object} query 查询条件对象
169
+ * @param {Object} body 修改对象
207
170
  * @return {Promise|Object} 执行结果
208
171
  */
209
- MongoDB.prototype.set = function(query, obj) {
210
- var _this = this;
211
- return new Promise((resolve, reject) => {
212
- _this.db.collection(_this.table).update(query, obj, function(err, ret) {
213
- if (err) {
214
- reject(err);
215
- } else {
216
- resolve(ret);
217
- }
218
- });
219
- });
172
+ MongoDB.prototype.set = function(query, body) {
173
+ return this.db.collection(this.table).update(query, body);
220
174
  };
221
175
 
222
176
  /**
223
177
  * @description 查询缓存
224
- * @param {Object} query 对象
178
+ * @param {Object} query 查询条件对象
225
179
  * @return {Promise|Array} 执行结果
226
180
  */
227
181
  MongoDB.prototype.get = function(query) {
228
- var _this = this;
229
- return new Promise((resolve, reject) => {
230
- if (_this.page !== 0 && _this.size !== 0) {
231
- _this.db.collection(_this.table).find(query).limit(_this.size).skip((_this.page - 1) * _this.size).toArray(
232
- function(err,
233
- ret) {
234
- if (err) {
235
- reject(err);
236
- } else {
237
- resolve(ret);
238
- }
239
- });
240
- } else {
241
- _this.db.collection(_this.table).find(query).toArray(function(err, ret) {
242
- if (err) {
243
- reject(err);
244
- } else {
245
- resolve(ret);
246
- }
247
- });
248
- }
249
- });
182
+ if (this.page !== 0 && this.size !== 0) {
183
+ return this.db.collection(this.table).find(query).limit(this.size).skip((this.page - 1) * this
184
+ .size).toArray();
185
+ } else {
186
+ return this.db.collection(this.table).find(query).toArray();
187
+ }
250
188
  };
251
189
 
252
190
  /**
@@ -283,4 +221,4 @@ function mongoDB_admin(scope, dir) {
283
221
  /**
284
222
  * @module 导出MongoDB管理器
285
223
  */
286
- exports.mongoDB_admin = mongoDB_admin;
224
+ exports.mongoDB_admin = mongoDB_admin;
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "mm_mongodb",
3
- "version": "1.3.8",
4
- "description": "这是超级美眉mongdb帮助函数模块,用于便捷操作mongdb,使用await方式,可以避免嵌套函数",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/qiuwenwu/mm_mongodb.git"
12
- },
13
- "keywords": [
14
- "mysql",
15
- "async",
16
- "await",
17
- "promise",
18
- "add",
19
- "del",
20
- "set",
21
- "get",
22
- "query",
23
- "run",
24
- "exec"
25
- ],
26
- "author": "邱文武",
27
- "license": "ISC",
28
- "bugs": {
29
- "url": "https://github.com/qiuwenwu/mm_mongodb/issues"
30
- },
31
- "homepage": "https://github.com/qiuwenwu/mm_mongodb#readme",
32
- "dependencies": {
33
- "mm_expand": "^1.4.7",
34
- "mongodb": "^3.3.2"
35
- }
36
- }
1
+ {
2
+ "name": "mm_mongodb",
3
+ "version": "1.4.1",
4
+ "description": "这是超级美眉mongdb帮助函数模块,用于便捷操作mongdb,使用await方式,可以避免嵌套函数",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/qiuwenwu/mm_mongodb.git"
12
+ },
13
+ "keywords": [
14
+ "mysql",
15
+ "async",
16
+ "await",
17
+ "promise",
18
+ "add",
19
+ "del",
20
+ "set",
21
+ "get",
22
+ "query",
23
+ "run",
24
+ "exec"
25
+ ],
26
+ "author": "邱文武",
27
+ "license": "ISC",
28
+ "bugs": {
29
+ "url": "https://github.com/qiuwenwu/mm_mongodb/issues"
30
+ },
31
+ "homepage": "https://github.com/qiuwenwu/mm_mongodb#readme",
32
+ "dependencies": {
33
+ "mm_expand": "^1.6.9",
34
+ "mongodb": "^5.9.1"
35
+ }
36
+ }
package/test.js CHANGED
@@ -1,36 +1,54 @@
1
1
  const MongoDB = require('./index.js').MongoDB;
2
+ const mongodb_admin = require('./index.js').mongoDB_admin;
3
+
4
+ async function test2() {
5
+ var mb = mongodb_admin("sys");
6
+ mb.setConfig();
7
+ // mb.open();
8
+ console.log(mb);
9
+ }
10
+ test2();
2
11
 
3
12
  /* 调用示例 */
4
13
  async function test() {
14
+ console.log("测试开始");
5
15
  // 实例化构造函数,传入服务器地址和数据库名
6
- var mb = new MongoDB("localhost", "mm");
16
+ var mb = new MongoDB("localhost", "mm");
7
17
  mb.setConfig();
8
-
18
+
9
19
  await mb.open();
10
- // 创建数据表
11
- await mb.addTable('bs_table');
20
+ try {
21
+ // 创建数据表
22
+ await mb.addTable('bs_table');
23
+ console.log("创建表bs_table");
24
+ } catch (e) {
25
+ console.log("创建表错误提示:", e);
26
+ //TODO handle the exception
27
+ }
12
28
  mb.table = "mm_table";
13
-
29
+
14
30
  var ret = await mb.get({
15
31
  'name': '李四'
16
32
  });
17
-
33
+ console.log("查询李四",ret);
34
+
18
35
  ret = await mb.get({
19
36
  'name': '张三'
20
37
  });
21
- console.log(ret);
22
-
23
- // 选择要操作的表
38
+ console.log("查询张三",ret);
39
+
40
+ // 选择要操作的表
24
41
  mb.table = 'mm_table';
25
-
26
- // 添加一条数据
42
+
43
+ // 添加一条数据
27
44
  ret = await mb.addObj({
28
45
  name: "张三",
29
46
  age: 21,
30
47
  sex: true
31
48
  });
32
-
33
- // 添加多条数据
49
+ console.log("添加一条数据", ret);
50
+
51
+ // 添加多条数据
34
52
  ret = await mb.addList([{
35
53
  name: "李四",
36
54
  age: 18,
@@ -40,6 +58,9 @@ async function test() {
40
58
  age: 15,
41
59
  sex: true
42
60
  }]);
61
+
62
+ console.log("添加多条数据", ret);
63
+ console.log("测试完成");
43
64
  }
44
65
 
45
- test();
66
+ test();
package/test2.js ADDED
@@ -0,0 +1,57 @@
1
+ const { Mongodb_cache, mongodb_cache_admin } = require('./cache.js');
2
+
3
+ async function test2() {
4
+ var mb = mongodb_cache_admin("sys");
5
+ mb.setConfig();
6
+ // mb.open();
7
+ console.log(mb);
8
+ }
9
+ test2();
10
+
11
+ /* 调用示例 */
12
+ async function test() {
13
+ console.log("测试开始");
14
+ // 实例化构造函数,传入服务器地址和数据库名
15
+ var mb = new Mongodb_cache("localhost", "mm");
16
+ mb.setConfig();
17
+
18
+ await mb.open();
19
+ try {
20
+ // 创建数据表
21
+ await mb.addTable('bs_keyValue');
22
+ console.log("创建表bs_keyValue");
23
+ } catch (e) {
24
+ // console.log("创建表错误提示:", e);
25
+ //TODO handle the exception
26
+ }
27
+ mb.table = "mm_table";
28
+
29
+ console.log("查询", mb.conn);
30
+ var ret = await mb.get('李四');
31
+ console.log("查询李四",ret);
32
+
33
+ ret = await mb.get('张三');
34
+ console.log("查询张三",ret);
35
+
36
+ // 选择要操作的表
37
+ mb.table = 'mm_table';
38
+
39
+ // 添加一条数据
40
+ ret = await mb.add("张三", {
41
+ age: 21,
42
+ sex: true
43
+ });
44
+ console.log("添加一条数据", ret);
45
+
46
+
47
+ // 修改一条数据
48
+ ret = await mb.add("李四", {
49
+ age: 24,
50
+ sex: false
51
+ });
52
+ console.log("修改一条数据", ret);
53
+
54
+ console.log("测试完成");
55
+ }
56
+
57
+ test();