Skip to content

Commit

Permalink
Support createClient option
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Dec 19, 2024
1 parent 7721752 commit 421d3d0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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";
Expand All @@ -31,7 +33,8 @@ const db = new Kysely<Database>({
});
```

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";
Expand All @@ -48,9 +51,25 @@ const db = new Kysely<Database>({
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<Database>({
createClient: () =>
createClient({
url: "file:local.db",
syncUrl: "libsql://localhost:8080",
authToken: "<token>",
}),
});
```

## 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
Expand All @@ -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.
34 changes: 20 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
);
}

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 421d3d0

Please sign in to comment.