import { ISQLiteAPIFactory, ISQLiteAPIOptions } from "./type" import { ensureDbDir } from "./utils/ensureDbDir" // @ts-ignore import { Database } from "bun:sqlite" export const getSQLiteAPI_Bun: ISQLiteAPIFactory = (dbPath: string | Buffer, options?: ISQLiteAPIOptions) => { let sqliteOptions: any = { create: true } if (options?.readonly) sqliteOptions["readonly"] = options.readonly if (options?.nativeBinding) sqliteOptions["nativeBinding"] = options.nativeBinding // 自动创建父目录 let sqlite: Database if (typeof dbPath === "string") { ensureDbDir(dbPath) sqlite = new Database(dbPath, sqliteOptions) } else { sqlite = Database.deserialize(dbPath) } sqlite.pragma = (command: string) => { sqlite.exec(`PRAGMA ${command};`) } if (options?.safeMode) { sqlite.pragma("synchronous = EXTRA") } else if (options?.fastMode) { sqlite.pragma("journal_mode = WAL") sqlite.pragma("synchronous = OFF") } else { sqlite.pragma("journal_mode = WAL") sqlite.pragma("synchronous = NORMAL") } if (sqlite.function === undefined) { console.warn("Bun SQLite 目前不支持自定义函数注册,追踪:https://github.com/oven-sh/bun/pull/8558") sqlite.function = (name: string, fn: (...args: any[]) => any) => { console.warn(`Bun SQLite 不支持注册自定义函数. 也就是无法正常使用 REGEXP 等功能 `) } } return sqlite }