Skip to content

Commit

Permalink
feat: support multiple libsql exports (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jul 21, 2023
1 parent 77b6093 commit 59df1d2
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 25 deletions.
48 changes: 42 additions & 6 deletions docs/content/100.connectors/libsql.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,53 @@ navigation.title: LibSQL

Connect to a [LibSQL](https://libsql.org/) database.

```js
## Usage

For this connector, you need to install [`@libsql/client`](https://www.npmjs.com/package/@libsql/client) dependency:

::code-group

```sh [npm]
npm install @libsql/client
```

```sh [Yarn]
yarn add @libsql/client
```

```sh [pnpm]
pnpm add @libsql/client
```

::

<br>

::code-group

```ts [Node.js]
import { createDB, sql } from "db0";
import libSql from "db0/connectors/libsql";

const db = createDB(
libSql({
url: `file:local.db`,
})
);
const db = createDB(libSql({ url: `file:local.db` }));
```

```ts [Http]
import { createDB, sql } from "db0";
import libSql from "db0/connectors/libsql/http";

const db = createDB(libSql({}));
```

```ts [Web]
import { createDB, sql } from "db0";
import libSql from "db0/connectors/libsql/web";

const db = createDB(libSql({}));
```

::

## Options

### `url`
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
"license": "MIT",
"sideEffects": false,
"exports": {
"./connectors/libsql": {
"import": "./connectors/libsql/node.mjs",
"require": "./connectors/libsql/node.cjs",
"web": {
"import": "./connectors/libsql/node.mjs",
"require": "./connectors/libsql/node.cjs"
}
},
"./connectors/*": {
"types": "./connectors/*.d.ts",
"import": "./connectors/*.mjs",
Expand Down
26 changes: 9 additions & 17 deletions src/connectors/libsql.ts → src/connectors/libsql/core.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import { createClient } from "@libsql/client";
import type { Client, InStatement, Config } from "@libsql/client";
import type { Connector, Statement } from "../types";
import type { Client, InStatement } from "@libsql/client";
import type { Connector, Statement } from "../../types";

export type ConnectorOptions = Config;

export default function libSqlConnector(opts: ConnectorOptions) {
let _client: undefined | Client;
function getClient() {
if (_client) {
return _client;
}
// TODO: Normalize options for file: protocol to be relative to project .data
_client = createClient(opts);
return _client;
}
export type ConnectorOptions = {
getClient: () => Client;
name?: string;
};

export default function libSqlCoreConnector(opts: ConnectorOptions) {
function query(sql: InStatement) {
const client = getClient();
const client = opts.getClient();
return client.execute(sql);
}

return <Connector>{
name: "libsql",
name: opts.name || "libsql-core",
exec(sql: string) {
return query(sql);
},
Expand Down
19 changes: 19 additions & 0 deletions src/connectors/libsql/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Config } from "@libsql/client";
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core";

export type ConnectorOptions = Config;

export default function libSqlConnector(opts: ConnectorOptions) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-http",
getClient,
});
}
19 changes: 19 additions & 0 deletions src/connectors/libsql/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Config } from "@libsql/client";
import { createClient } from "@libsql/client";
import libSqlCore from "./core";

export type ConnectorOptions = Config;

export default function libSqlConnector(opts: ConnectorOptions) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-node",
getClient,
});
}
19 changes: 19 additions & 0 deletions src/connectors/libsql/web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Config } from "@libsql/client";
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core";

export type ConnectorOptions = Config;

export default function libSqlConnector(opts: ConnectorOptions) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-web",
getClient,
});
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ export * from "./types";

export const connectors = {
sqlite: "db0/connectors/better-sqlite3",
libsql: "db0/connectors/libsql",
postgresql: "db0/connectors/postgresql",
"cloudflare-d1": "db0/connectors/cloudflare-d1",
libsql: "db0/connectors/libsql/node",
"libsql-node": "db0/connectors/libsql/node",
"libsql-http": "db0/connectors/libsql/http",
"libsql-web": "db0/connectors/libsql/web",
} as const;

export type ConnectorName = keyof typeof connectors;
2 changes: 1 addition & 1 deletion test/connectors/libsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileURLToPath } from "node:url";
import { existsSync, unlinkSync, mkdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { describe } from "vitest";
import libSql from "../../src/connectors/libsql";
import libSql from "../../src/connectors/libsql/node";
import { testConnector } from "./_tests";

describe("connectors: libsql", () => {
Expand Down

0 comments on commit 59df1d2

Please sign in to comment.