mm_jsondb 1.0.0 → 1.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.
- package/example/index.js +54 -0
- package/lib/json_db.js +2 -1
- package/lib/sql.js +1 -1
- package/lib/store.js +1 -1
- package/package.json +50 -49
package/example/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mm_jsondb 最小可运行示例
|
|
3
|
+
* @description 演示 JsonDB 的实例化、建表、增查操作(使用本地 JSON 文件存储,无需连接任何外部服务)
|
|
4
|
+
*/
|
|
5
|
+
const { JsonDB } = require('../index.js');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 主函数
|
|
11
|
+
*/
|
|
12
|
+
async function main() {
|
|
13
|
+
console.log('== mm_jsondb 示例开始 ==');
|
|
14
|
+
|
|
15
|
+
// 实例化:数据存储到本地 example/db 目录(实际使用时可替换为任意本地目录)
|
|
16
|
+
const db = new JsonDB({
|
|
17
|
+
dir: path.join(__dirname, './db/'),
|
|
18
|
+
database: 'example_db',
|
|
19
|
+
debug: true
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// 打开数据库
|
|
23
|
+
const opened = await db.open();
|
|
24
|
+
console.log('打开数据库:', opened);
|
|
25
|
+
|
|
26
|
+
// 建表并写入数据
|
|
27
|
+
const table = db.db().new('example_users', 'id');
|
|
28
|
+
await db.db().addTable('example_users', 'id', 'int', true, '示例用户表');
|
|
29
|
+
const id = await table.add({ name: '示例用户', age: 18 });
|
|
30
|
+
console.log('插入记录,ID:', id);
|
|
31
|
+
|
|
32
|
+
// 查询数据
|
|
33
|
+
const list = await table.get({ name: '示例用户' });
|
|
34
|
+
console.log('查询结果:', JSON.stringify(list));
|
|
35
|
+
|
|
36
|
+
// 健康检查
|
|
37
|
+
const health = await db.healthCheck();
|
|
38
|
+
console.log('健康检查:', JSON.stringify(health));
|
|
39
|
+
|
|
40
|
+
// 清理示例数据目录(需在 close 前取得存储目录)
|
|
41
|
+
const dbDir = db.getStore()._getDbDir();
|
|
42
|
+
await db.close();
|
|
43
|
+
const rootDir = path.dirname(dbDir);
|
|
44
|
+
if (rootDir && fs.existsSync(rootDir)) {
|
|
45
|
+
await fs.promises.rm(rootDir, { recursive: true, force: true });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log('== mm_jsondb 示例结束 ==');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
main().catch((err) => {
|
|
52
|
+
console.error('示例执行失败:', err);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
});
|
package/lib/json_db.js
CHANGED
package/lib/sql.js
CHANGED
package/lib/store.js
CHANGED
package/package.json
CHANGED
|
@@ -1,50 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
2
|
+
"name": "mm_jsondb",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "JSON文件数据库操作模块,提供与mm_mysql完全兼容的API接口,以JSON文件存储数据,支持字段索引快速搜索",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node index.js",
|
|
8
|
+
"test": "node test.js",
|
|
9
|
+
"lint": "eslint . --config eslint.config.js",
|
|
10
|
+
"lint:fix": "eslint . --config eslint.config.js --fix"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://gitee.com/qiuwenwu91/mm_jsondb.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"mm_mod",
|
|
18
|
+
"mm_infra",
|
|
19
|
+
"mm_out_adapter",
|
|
20
|
+
"json",
|
|
21
|
+
"database",
|
|
22
|
+
"async",
|
|
23
|
+
"await",
|
|
24
|
+
"promise",
|
|
25
|
+
"file",
|
|
26
|
+
"storage",
|
|
27
|
+
"index",
|
|
28
|
+
"search",
|
|
29
|
+
"mm_mysql",
|
|
30
|
+
"compatible",
|
|
31
|
+
"json-db",
|
|
32
|
+
"nosql"
|
|
33
|
+
],
|
|
34
|
+
"layer": "adapter",
|
|
35
|
+
"group": "serialization",
|
|
36
|
+
"author": "邱文武",
|
|
37
|
+
"license": "ISC",
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=12.0.0"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"mm_helper": "^1.0.0",
|
|
43
|
+
"mm_corejs": "^1.1.4",
|
|
44
|
+
"mm_mod": "^1.0.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"eslint": "^10.9.1",
|
|
48
|
+
"eslint-plugin-jsdoc": "^64.2.1",
|
|
49
|
+
"mm_eslint": "^1.7.7"
|
|
50
|
+
}
|
|
51
|
+
}
|