mm_sqlite 1.5.5 → 1.5.7

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.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * mm_sqlite 最小可运行示例
3
+ * @description 演示 SQLite 类的实例化、建表、增查操作(使用本地 SQLite 文件存储,无需连接任何外部服务)
4
+ */
5
+ const { SQLite } = 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_sqlite 示例开始 ==');
14
+
15
+ // 实例化:数据库文件存储到本地 example/db 目录
16
+ const db = new SQLite({
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
+ await db.exec('CREATE TABLE IF NOT EXISTS example_users (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), age INTEGER)');
28
+
29
+ // 写入并查询数据
30
+ const table = db.db();
31
+ table.table = 'example_users';
32
+ const addResult = await table.add({ name: '示例用户', age: 18 });
33
+ console.log('插入记录,ID:', addResult);
34
+
35
+ const list = await table.get({ name: '示例用户' });
36
+ console.log('查询结果:', JSON.stringify(list));
37
+
38
+ // 关闭并清理示例数据文件
39
+ await db.close();
40
+ const dbDir = path.join(__dirname, './db/');
41
+ if (fs.existsSync(dbDir)) {
42
+ await fs.promises.rm(dbDir, { recursive: true, force: true });
43
+ }
44
+
45
+ console.log('== mm_sqlite 示例结束 ==');
46
+ }
47
+
48
+ main().catch((err) => {
49
+ console.error('示例执行失败:', err);
50
+ process.exit(1);
51
+ });