diff --git a/README.md b/README.md index 7ef201e..63fda0f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # kysely-libsql -A [Kysely][kysely] dialect for [libSQL][libsql], compatible with [@libsql/client][libsql-client-ts]. +A [Kysely][kysely] dialect for [libSQL][libsql], compatible with +[@libsql/client][libsql-client-ts]. [kysely]: https://github.com/koskimas/kysely [libsql]: https://github.com/tursodatabase/libsql @@ -13,7 +14,8 @@ npm install @libsql/kysely-libsql ## Usage -Pass a `LibsqlDialect` instance as the `dialect` when creating the `Kysely` object: +Pass a `LibsqlDialect` instance as the `dialect` when creating the `Kysely` +object: ```typescript import { Kysely } from "kysely"; @@ -31,7 +33,8 @@ const db = new Kysely({ }); ``` -Instead of a `url`, you can also pass an existing `Client` from [`@libsql/client`][libsql-client-ts]: +Instead of a `url`, you can also pass an existing `Client` from +[`@libsql/client`][libsql-client-ts]: ```typescript import { createClient } from "@libsql/client"; @@ -48,9 +51,25 @@ const db = new Kysely({ client.close(); ``` +Or pass a `createClient` function to have Kysely manage the client's lifecycle. + +```typescript +import { createClient } from "@libsql/client"; + +const db = new Kysely({ + createClient: () => + createClient({ + url: "file:local.db", + syncUrl: "libsql://localhost:8080", + authToken: "", + }), +}); +``` + ## Supported Configuration Options -The library accepts the exact same [options][client-options] as [`@libsql/client`][libsql-client-ts]. +The library accepts the exact same [options][client-options] as +[`@libsql/client`][libsql-client-ts]. [libsql-client-ts]: https://github.com/tursodatabase/libsql-client-ts [client-options]: https://docs.turso.tech/sdk/ts/reference#initializing @@ -61,4 +80,6 @@ This project is licensed under the MIT license. ### Contribution -Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in `@libsql/kysely-libsql` by you, shall be licensed as MIT, without any additional terms or conditions. +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in `@libsql/kysely-libsql` by you, shall be licensed as MIT, +without any additional terms or conditions. diff --git a/package-lock.json b/package-lock.json index bf8f31c..4983782 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1383,12 +1383,13 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, + "license": "MIT", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -1619,10 +1620,11 @@ } }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, + "license": "MIT", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", @@ -1901,10 +1903,11 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, + "license": "MIT", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -2163,6 +2166,7 @@ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true, + "license": "MIT", "engines": { "node": ">=0.12.0" } @@ -3141,12 +3145,13 @@ "dev": true }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, + "license": "MIT", "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -3756,6 +3761,7 @@ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dev": true, + "license": "MIT", "dependencies": { "is-number": "^7.0.0" }, diff --git a/src/index.ts b/src/index.ts index 943ff8d..554bec7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,9 +2,8 @@ import * as libsql from "@libsql/client"; import * as kysely from "kysely"; export type LibsqlDialectConfig = - | { - client: libsql.Client; - } + | { client: libsql.Client } + | { createClient: () => libsql.Client } | libsql.Config; export class LibsqlDialect implements kysely.Dialect { @@ -24,12 +23,15 @@ export class LibsqlDialect implements kysely.Dialect { if ("client" in this.#config) { client = this.#config.client; closeClient = false; + } else if ("createClient" in this.#config) { + client = this.#config.createClient(); + closeClient = true; } else if (this.#config.url !== undefined) { client = libsql.createClient(this.#config); closeClient = true; } else { throw new Error( - "Please specify either `client` or `url` in the LibsqlDialect config" + "Please specify either `client`, `createClient` or `url` in the LibsqlDialect config" ); } @@ -109,7 +111,7 @@ export class LibsqlConnection implements kysely.DatabaseConnection { async beginTransaction() { if (this.#transaction) { - throw new Error("Transaction already in progress"); + throw new Error("Transaction already in progress"); } this.#transaction = await this.client.transaction(); }