Initial Commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { SQLITE_CODES, SqliteError } from "../errors/index.js";
|
||||
import type { BunDriverOptions } from "../schemas/index.js";
|
||||
|
||||
export interface BunDriverResult {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: bun:sqlite Database type varies by environment
|
||||
sqlite: any;
|
||||
close: () => Promise<void>;
|
||||
}
|
||||
|
||||
export function isBunEnvironment(): boolean {
|
||||
return typeof Bun !== "undefined";
|
||||
}
|
||||
|
||||
export async function createBunDriver(
|
||||
options?: BunDriverOptions
|
||||
): Promise<BunDriverResult> {
|
||||
/* c8 ignore next 5 */
|
||||
if (!isBunEnvironment()) {
|
||||
throw new SqliteError(
|
||||
SQLITE_CODES.ENVIRONMENT_ERROR,
|
||||
"createBunDriver requires Bun runtime — use createWasmDriver in browser environments"
|
||||
);
|
||||
}
|
||||
|
||||
let sqlite: BunDriverResult["sqlite"];
|
||||
try {
|
||||
const { Database } = await import("bun:sqlite");
|
||||
const filename = options?.filename ?? ":memory:";
|
||||
sqlite = new Database(filename, {
|
||||
create: true,
|
||||
readonly: options?.readonly ?? false,
|
||||
});
|
||||
} catch (err) {
|
||||
throw new SqliteError(
|
||||
SQLITE_CODES.DRIVER_INIT_FAILED,
|
||||
`Failed to open SQLite database: ${err instanceof Error ? err.message : String(err)}`,
|
||||
{ cause: err instanceof Error ? err : undefined }
|
||||
);
|
||||
}
|
||||
|
||||
let closed = false;
|
||||
|
||||
return {
|
||||
sqlite,
|
||||
close: async () => {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
sqlite.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user