Skip to content

Commit 421d3d0

Browse files
committed
Support createClient option
1 parent 7721752 commit 421d3d0

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# kysely-libsql
22

3-
A [Kysely][kysely] dialect for [libSQL][libsql], compatible with [@libsql/client][libsql-client-ts].
3+
A [Kysely][kysely] dialect for [libSQL][libsql], compatible with
4+
[@libsql/client][libsql-client-ts].
45

56
[kysely]: https://github.com/koskimas/kysely
67
[libsql]: https://github.com/tursodatabase/libsql
@@ -13,7 +14,8 @@ npm install @libsql/kysely-libsql
1314

1415
## Usage
1516

16-
Pass a `LibsqlDialect` instance as the `dialect` when creating the `Kysely` object:
17+
Pass a `LibsqlDialect` instance as the `dialect` when creating the `Kysely`
18+
object:
1719

1820
```typescript
1921
import { Kysely } from "kysely";
@@ -31,7 +33,8 @@ const db = new Kysely<Database>({
3133
});
3234
```
3335

34-
Instead of a `url`, you can also pass an existing `Client` from [`@libsql/client`][libsql-client-ts]:
36+
Instead of a `url`, you can also pass an existing `Client` from
37+
[`@libsql/client`][libsql-client-ts]:
3538

3639
```typescript
3740
import { createClient } from "@libsql/client";
@@ -48,9 +51,25 @@ const db = new Kysely<Database>({
4851
client.close();
4952
```
5053

54+
Or pass a `createClient` function to have Kysely manage the client's lifecycle.
55+
56+
```typescript
57+
import { createClient } from "@libsql/client";
58+
59+
const db = new Kysely<Database>({
60+
createClient: () =>
61+
createClient({
62+
url: "file:local.db",
63+
syncUrl: "libsql://localhost:8080",
64+
authToken: "<token>",
65+
}),
66+
});
67+
```
68+
5169
## Supported Configuration Options
5270

53-
The library accepts the exact same [options][client-options] as [`@libsql/client`][libsql-client-ts].
71+
The library accepts the exact same [options][client-options] as
72+
[`@libsql/client`][libsql-client-ts].
5473

5574
[libsql-client-ts]: https://github.com/tursodatabase/libsql-client-ts
5675
[client-options]: https://docs.turso.tech/sdk/ts/reference#initializing
@@ -61,4 +80,6 @@ This project is licensed under the MIT license.
6180

6281
### Contribution
6382

64-
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.
83+
Unless you explicitly state otherwise, any contribution intentionally submitted
84+
for inclusion in `@libsql/kysely-libsql` by you, shall be licensed as MIT,
85+
without any additional terms or conditions.

package-lock.json

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as libsql from "@libsql/client";
22
import * as kysely from "kysely";
33

44
export type LibsqlDialectConfig =
5-
| {
6-
client: libsql.Client;
7-
}
5+
| { client: libsql.Client }
6+
| { createClient: () => libsql.Client }
87
| libsql.Config;
98

109
export class LibsqlDialect implements kysely.Dialect {
@@ -24,12 +23,15 @@ export class LibsqlDialect implements kysely.Dialect {
2423
if ("client" in this.#config) {
2524
client = this.#config.client;
2625
closeClient = false;
26+
} else if ("createClient" in this.#config) {
27+
client = this.#config.createClient();
28+
closeClient = true;
2729
} else if (this.#config.url !== undefined) {
2830
client = libsql.createClient(this.#config);
2931
closeClient = true;
3032
} else {
3133
throw new Error(
32-
"Please specify either `client` or `url` in the LibsqlDialect config"
34+
"Please specify either `client`, `createClient` or `url` in the LibsqlDialect config"
3335
);
3436
}
3537

@@ -109,7 +111,7 @@ export class LibsqlConnection implements kysely.DatabaseConnection {
109111

110112
async beginTransaction() {
111113
if (this.#transaction) {
112-
throw new Error("Transaction already in progress");
114+
throw new Error("Transaction already in progress");
113115
}
114116
this.#transaction = await this.client.transaction();
115117
}

0 commit comments

Comments
 (0)