Skip to content

升级指南

  1. 准备,建议开发者新开一个项目分支,来做此次升级。
  2. 升级依赖包版本:npm i ee-bin@latest -D && npm i ee-core@latest
  3. 调整配置文件:
  4. 调整 package.json 配置,参照 package.json
  5. 删除 js,ts 文件中这种字符串标识:'[class ExampleController]'。 v5 版本已经不需要了。
  6. SqliteStorage 懒加载迁移:构造器不再打开数据库连接。原生 better-sqlite3 绑定现在通过 init() 懒加载,避免不使用 SQLite 的项目无条件加载该模块。

受影响的代码

之前(v4)之后(v5最新)
new SqliteStorage(name, options) — 构造器打开数据库new SqliteStorage(name) — 构造器仅计算路径
this.db = this.storage.db 立即可用await this.storage.init(options) — 必须在使用 this.db 前调用
basedbService._init() — 同步basedbService._init() — 现为异步,返回 Promise<void>
preload() — 同步preload() — 现为异步,返回 Promise<void>
import Database from 'better-sqlite3' — 导入时加载import type Database from 'better-sqlite3' — 仅类型;实际导入延迟到 init()

迁移步骤

  1. 修改 SqliteStorage 构造器调用:移除第二个 options 参数
  2. 在构造后调用 await storage.init(options),然后才能访问 storage.db
  3. 将数据库初始化相关的同步代码改为 async/await
  4. 如果使用 basedbService_init()changeDataDir() 方法现为异步 — 加 await
  5. 如果使用 preload() 函数,现为异步 — 加 await

示例

javascript
// 之前
const storage = new SqliteStorage('myapp.db', { timeout: 6000 });
const db = storage.db;

// 之后
const storage = new SqliteStorage('myapp.db');
await storage.init({ timeout: 6000 });
const db = storage.db;

// 或链式调用
const { db } = await new SqliteStorage('myapp.db').init({ timeout: 6000 });