UNPKG

kysely-libsql

Version:
156 lines (154 loc) 4.67 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { LibsqlConnection: () => LibsqlConnection, LibsqlDialect: () => LibsqlDialect, LibsqlDriver: () => LibsqlDriver }); module.exports = __toCommonJS(index_exports); var libsql = __toESM(require("@libsql/client"), 1); var kysely = __toESM(require("kysely"), 1); var LibsqlDialect = class { #config; constructor(config) { this.#config = config; } createAdapter() { return new kysely.SqliteAdapter(); } createDriver() { let client; let closeClient; if ("client" in this.#config) { client = this.#config.client; closeClient = false; } else if (this.#config.url !== void 0) { client = libsql.createClient(this.#config); closeClient = true; } else { throw new Error( "Please specify either `client` or `url` in the LibsqlDialect config" ); } return new LibsqlDriver(client, closeClient); } createIntrospector(db) { return new kysely.SqliteIntrospector(db); } createQueryCompiler() { return new kysely.SqliteQueryCompiler(); } }; var LibsqlDriver = class { client; #closeClient; constructor(client, closeClient) { this.client = client; this.#closeClient = closeClient; } async init() { } async acquireConnection() { return new LibsqlConnection(this.client); } async beginTransaction(connection, _settings) { await connection.beginTransaction(); } async commitTransaction(connection) { await connection.commitTransaction(); } async rollbackTransaction(connection) { await connection.rollbackTransaction(); } async releaseConnection(_conn) { } async destroy() { if (this.#closeClient) { this.client.close(); } } }; var LibsqlConnection = class { client; #transaction; constructor(client) { this.client = client; } async executeQuery(compiledQuery) { const target = this.#transaction ?? this.client; const result = await target.execute({ sql: compiledQuery.sql, args: compiledQuery.parameters }); const rows = result.rows.map((row) => { const stripped = {}; for (const key in row) { stripped[key] = row[key]; } return stripped; }); return { insertId: result.lastInsertRowid, numAffectedRows: BigInt(result.rowsAffected), rows }; } async beginTransaction() { if (this.#transaction) { throw new Error("Transaction already in progress"); } this.#transaction = await this.client.transaction("write"); } async commitTransaction() { if (!this.#transaction) { throw new Error("No transaction to commit"); } await this.#transaction.commit(); this.#transaction = void 0; } async rollbackTransaction() { if (!this.#transaction) { throw new Error("No transaction to rollback"); } await this.#transaction.rollback(); this.#transaction = void 0; } // eslint-disable-next-line require-yield async *streamQuery(_compiledQuery, _chunkSize) { throw new Error("kysely-libsql does not support streaming yet"); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { LibsqlConnection, LibsqlDialect, LibsqlDriver });