JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35
  • Score
    100M100P100Q80862F
  • License ISC

这是超级美眉sqlite帮助函数模块,用于便捷操作sqlite,使用await方式,可以避免嵌套函数

Package Exports

  • mm_sqlite

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mm_sqlite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mm_sqlite

中文 | English

高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口。适用于Node.js应用的轻量级数据存储解决方案。

安装

npm install mm_sqlite --save

特性

  • 🚀 高性能优化:经过深度优化的SQL构建器和连接管理
  • 🔄 完全兼容:与mm_mysql模块API完全兼容,可无缝切换
  • 📊 内存优化:优化的内存使用,减少GC压力
  • 🔧 链式调用:流畅的SQL构建器链式调用
  • 🛡️ 错误处理:完善的错误处理机制
  • 📈 连接池支持:支持连接池模式,提高并发性能
  • 🔒 事务支持:完整的事务处理机制
  • 🔍 类型安全:完善的JSDoc类型注释

依赖要求

  • Node.js 14.0+
  • SQLite3 3.0+

快速开始

基本使用

初始化和连接

const { Sqlite } = require('mm_sqlite');

// 创建数据库实例
const sqlite = new Sqlite({
    dir: './db/',                  // 数据库文件存储目录
    database: 'test',              // 数据库文件名(不包含扩展名)
    charset: 'utf8mb4',            // 字符集
    timezone: '+08:00',            // 时区
    connect_timeout: 20000,        // 连接超时时间(毫秒)
    acquire_timeout: 20000,        // 获取连接超时时间(毫秒)
    query_timeout: 20000,          // 查询超时时间(毫秒)
    connection_limit: 1,           // 连接限制(>1启用连接池)
    enable_keep_alive: true,       // 启用保活
    keep_alive_initial_delay: 10000, // 保活初始延迟
    enable_reconnect: true,        // 启用重连
    reconnect_interval: 1000,      // 重连间隔
    max_reconnect_attempts: 5,     // 最大重连尝试次数
    wait_for_connections: true,    // 等待连接
    pool_min: 1,                   // 连接池最小连接数
    pool_max: 5,                   // 连接池最大连接数
    pool_acquire_timeout: 30000,   // 连接池获取超时
    pool_idle_timeout: 60000,      // 连接池空闲超时
    pool_reap_interval: 1000       // 连接池清理间隔
});

// 打开数据库连接
await sqlite.open();

基础操作

// 获取数据库管理器
const db = sqlite.db();

// 设置表名
const userDb = db.new('users', 'id');

// 插入数据
const result = await userDb.add({
    username: '张三',
    email: 'zhangsan@example.com',
    age: 28,
    created_at: new Date()
});

// 查询数据
const users = await userDb.get();

// 更新数据
await userDb.set({ id: 1 }, { age: 29 });

// 删除数据
await userDb.del({ id: 1 });

高级查询

// 条件查询
const adultUsers = await userDb.get({
    age: { _gte: 18 }
});

// 分页查询
const pageUsers = await userDb.get({
    _page: 1,
    _size: 10,
    _sort: 'created_at desc'
});

// 聚合查询
const stats = await userDb.aggr({
    _count: 'id',
    _avg: 'age',
    _max: 'age',
    _min: 'age'
});

API参考

Sqlite类

构造函数

new Sqlite(config)

主要方法

  • open() - 打开数据库连接
  • close() - 关闭数据库连接
  • db() - 获取数据库管理器实例
  • run(sql, params) - 执行查询SQL
  • exec(sql, params) - 执行非查询SQL

DB类

表操作

  • addTable(table, field, type, auto, commit, timeout) - 创建表
  • dropTable(table, timeout) - 删除表
  • renameTable(table, new_table, timeout) - 重命名表
  • emptyTable(table, timeout) - 清空表
  • hasTable(table, timeout) - 检查表是否存在

字段操作

  • addField(field, type, value, not_null, auto, comment, timeout) - 添加字段
  • delField(table, field, timeout) - 删除字段
  • renameField(table, field, new_field, type, timeout) - 重命名字段
  • editField(table, field, type, timeout) - 修改字段
  • fields(table, field_name, timeout) - 获取字段信息

数据操作

  • add(data, timeout) - 插入数据
  • set(where, data, timeout) - 更新数据
  • del(where, timeout) - 删除数据
  • get(where, timeout) - 查询数据
  • getTableData(table, batchSize, timeout) - 获取表数据

事务操作

  • start() - 开始事务
  • commit(transaction) - 提交事务
  • back(transaction) - 回滚事务
  • transaction(callback) - 事务中执行操作

配置参数

参数 类型 默认值 描述
dir string './db/' 数据库文件存储目录
database string 'mm' 数据库文件名
charset string 'utf8mb4' 字符集
timezone string '+08:00' 时区
connect_timeout number 20000 连接超时时间(ms)
acquire_timeout number 20000 获取连接超时时间(ms)
query_timeout number 20000 查询超时时间(ms)
connection_limit number 1 连接限制
enable_keep_alive boolean true 启用保活
keep_alive_initial_delay number 10000 保活初始延迟(ms)
enable_reconnect boolean true 启用重连
reconnect_interval number 1000 重连间隔(ms)
max_reconnect_attempts number 5 最大重连尝试次数
wait_for_connections boolean true 等待连接
pool_min number 1 连接池最小连接数
pool_max number 5 连接池最大连接数
pool_acquire_timeout number 30000 连接池获取超时(ms)
pool_idle_timeout number 60000 连接池空闲超时(ms)
pool_reap_interval number 1000 连接池清理间隔(ms)

开发规范

  • 使用JavaScript开发,禁止使用TypeScript
  • 使用JSDoc注释进行类型定义
  • 遵循统一的命名规范和方法签名
  • 完善的错误处理机制
  • 支持异步/等待语法

许可证

MIT License

贡献

欢迎提交Issue和Pull Request来改进这个项目。