UNPKG

kysely-bun-sqlite

Version:

Use kysely with `bun:sqlite`, for an example please see the [Simple Example](https://github.com/dylanblokhuis/kysely-bun-sqlite/tree/master/examples/simple) in this repository.

139 lines (134 loc) 3.83 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { BunSqliteDialect: () => BunSqliteDialect }); module.exports = __toCommonJS(src_exports); // src/dialect.ts var import_kysely2 = require("kysely"); // src/driver.ts var import_kysely = require("kysely"); var BunSqliteDriver = class { #config; #connectionMutex = new ConnectionMutex(); #db; #connection; constructor(config) { this.#config = { ...config }; } async init() { this.#db = this.#config.database; this.#connection = new BunSqliteConnection(this.#db); if (this.#config.onCreateConnection) { await this.#config.onCreateConnection(this.#connection); } } async acquireConnection() { await this.#connectionMutex.lock(); return this.#connection; } async beginTransaction(connection) { await connection.executeQuery(import_kysely.CompiledQuery.raw("begin")); } async commitTransaction(connection) { await connection.executeQuery(import_kysely.CompiledQuery.raw("commit")); } async rollbackTransaction(connection) { await connection.executeQuery(import_kysely.CompiledQuery.raw("rollback")); } async releaseConnection() { this.#connectionMutex.unlock(); } async destroy() { this.#db?.close(); } }; var BunSqliteConnection = class { #db; constructor(db) { this.#db = db; } executeQuery(compiledQuery) { const { sql, parameters } = compiledQuery; const stmt = this.#db.prepare(sql); if (stmt.columnNames.length > 0) { return Promise.resolve({ rows: stmt.all(parameters) }); } const results = stmt.run(parameters); return Promise.resolve({ insertId: BigInt(results.lastInsertRowid), numAffectedRows: BigInt(results.changes), rows: [] }); } async *streamQuery(compiledQuery) { const { sql, parameters } = compiledQuery; const stmt = this.#db.prepare(sql); if (!("iterator" in stmt)) { throw new Error("bun:sqlite supports streaming in 1.1.31 or above. Please upgrade to use streaming."); } for await (const row of stmt.iterate(parameters)) { yield { rows: [row] }; } } }; var ConnectionMutex = class { #promise; #resolve; async lock() { while (this.#promise) { await this.#promise; } this.#promise = new Promise((resolve) => { this.#resolve = resolve; }); } unlock() { const resolve = this.#resolve; this.#promise = void 0; this.#resolve = void 0; resolve?.(); } }; // src/dialect.ts var BunSqliteDialect = class { #config; constructor(config) { this.#config = { ...config }; } createDriver() { return new BunSqliteDriver(this.#config); } createQueryCompiler() { return new import_kysely2.SqliteQueryCompiler(); } createAdapter() { return new import_kysely2.SqliteAdapter(); } createIntrospector(db) { return new import_kysely2.SqliteIntrospector(db); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BunSqliteDialect });