| 123456789101112131415161718192021222324252627282930313233 |
- 'use strict';
- /**
- * SQLite 引擎适配。
- *
- * Node 侧用内置 node:sqlite 的 DatabaseSync;bun 编译产物里没有这个内置模块,只有 bun:sqlite。
- * 两者能力已用 scripts/sqlite-engine-probe.mjs 逐项实测对等(WAL、busy_timeout、CHECK、外键、
- * 显式事务、upsert、只读模式),差异只在构造签名,这里抹平后调用方沿用 DatabaseSync 的写法即可。
- *
- * `.get()` 未命中时 node 返回 undefined、bun 返回 null,两者在本仓库的调用点(`?.` 与 `|| null`)
- * 行为一致,因此不额外包装 Statement。
- */
- const isBun = typeof globalThis.Bun !== 'undefined';
- function resolveDatabaseSync() {
- if (!isBun) return require('node:sqlite').DatabaseSync;
- const { Database } = require('bun:sqlite');
- return class BunDatabaseSync extends Database {
- constructor(filePath, options = {}) {
- // bun 用全小写 readonly;且不给任何 open flag 会直接抛 SQLITE_MISUSE
- super(filePath, options.readOnly
- ? { readonly: true }
- : { create: true, readwrite: true });
- }
- };
- }
- module.exports = {
- DatabaseSync: resolveDatabaseSync(),
- sqliteEngine: isBun ? 'bun:sqlite' : 'node:sqlite',
- };
|