Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add planetscale support #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
POSTGRESQL_URL=postgresql://<user>:@localhost:5432/db0

# PlanetScale
PLANETSCALE_HOST=aws.connect.psdb.cloud
PLANETSCALE_USERNAME=username
PLANETSCALE_PASSWORD=password

1 change: 1 addition & 0 deletions docs/2.connectors/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Currently supported connectors:
- [Bun](/connectors/bun)
- [Cloudflare D1](/connectors/cloudflare)
- [LibSQL](/connectors/libsql)
- [PlanetScale](/connectors/planetscale)
- [PostgreSQL](/connectors/postgresql)
- [SQLite](/connectors/sqlite)

Expand Down
46 changes: 43 additions & 3 deletions docs/2.connectors/planetscale.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ icon: simple-icons:planetscale

:read-more{to="https://planetscale.com"}

::read-more{to="https://github.com/unjs/db0/issues/32"}
This connector is planned to be supported. Follow up via [unjs/db0#32](https://github.com/unjs/db0/issues/32).
::
## Usage

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

:pm-install{name="@planetscale/database"}

Use `planetscale` connector:

```js
import { createDatabase, sql } from "db0";
import planetscale from "db0/connectors/planetscale";

const db = createDatabase(
planetscale({
host: "aws.connect.psdb.cloud",
username: "username",
password: "password",
}),
);
```

## Options

### `host`

Planetscale host.

### `username`

Planetscale username.

### `password`

Planetscale password.

### 'url'

Connection URL string.
The `host`, `username` and `password` are extracted from the URL.

:read-more{title="Create a database password" to="https://planetscale.com/docs/tutorials/planetscale-serverless-driver"}

:read-more{title="@planetscale/database client options" to="https://github.com/planetscale/database-js"}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"devDependencies": {
"@libsql/client": "^0.5.2",
"@planetscale/database": "^1.16.0",
"@types/better-sqlite3": "^7.6.9",
"@types/bun": "^1.0.8",
"@types/pg": "^8.11.2",
Expand Down
15 changes: 12 additions & 3 deletions pnpm-lock.yaml

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

56 changes: 56 additions & 0 deletions src/connectors/planetscale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Client, type Config } from "@planetscale/database";

import type { Connector, Statement } from "../types";

export default function planetscaleConnector(opts: Config) {
let _client: undefined | Client;
function getClient() {
if (_client) {
return _client;
}
const client = new Client(opts);
_client = client;
return client;
}

// Discussion on how @planetscale/database client works:
// https://github.com/drizzle-team/drizzle-orm/issues/1743#issuecomment-1879479647
function query(sql: string, params?: unknown[]) {
const client = getClient();
return client.execute(sql, params);
}

return <Connector>{
name: "planetscale",
exec(sql: string) {
return query(sql);
},
prepare(sql: string) {
const stmt = <Statement>{
_sql: sql,
_params: [],
bind(...params) {
if (params.length > 0) {
this._params = params;
}
return stmt;
},
all(...params) {
return query(this._sql, params || this._params).then((r) => r.rows);
},
run(...params) {
return query(this._sql, params || this._params).then((r) => ({
result: r,
rows: r.rows,
}));
},
get(...params) {
return query(this._sql, params || this._params).then(
(r) => r.rows[0],
);
},
};
return stmt;
},
};
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const connectors = {
"libsql-web": "db0/connectors/libsql/web",
bun: "db0/connectors/bun-sqlite",
"bun-sqlite": "db0/connectors/bun-sqlite",
planetscale: "db0/connectors/planetscale",
} as const;

export type ConnectorName = keyof typeof connectors;
21 changes: 19 additions & 2 deletions test/connectors/_tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { beforeAll, expect, it } from "vitest";
import { Connector, Database, createDatabase } from "../../src";

export function testConnector(opts: { connector: Connector }) {
const dialects = [
"mysql",
"postgresql",
"sqlite",
"libsql",
] as const;
type SQLDialect = typeof dialects[number];

export function testConnector(opts: { connector: Connector, dialect?: SQLDialect }) {
let db: Database;
beforeAll(() => {
db = createDatabase(opts.connector);
Expand All @@ -11,7 +19,16 @@ export function testConnector(opts: { connector: Connector }) {

it("drop and create table", async () => {
await db.sql`DROP TABLE IF EXISTS users`;
await db.sql`CREATE TABLE users ("id" TEXT PRIMARY KEY, "firstName" TEXT, "lastName" TEXT, "email" TEXT)`;
switch (opts.dialect) {
case "mysql": {
await db.sql`CREATE TABLE users (\`id\` VARCHAR(4) PRIMARY KEY, \`firstName\` TEXT, \`lastName\` TEXT, \`email\` TEXT)`;
break;
}
default: {
await db.sql`CREATE TABLE users ("id" TEXT PRIMARY KEY, "firstName" TEXT, "lastName" TEXT, "email" TEXT)`;
break;
}
}
});

it("insert", async () => {
Expand Down
1 change: 1 addition & 0 deletions test/connectors/better-sqlite3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("connectors: better-sqlite3", () => {
const tmpDir = fileURLToPath(new URL(".tmp/better-sqlite3", import.meta.url));
rmSync(tmpDir, { recursive: true, force: true });
testConnector({
dialect: "sqlite",
connector: connector({
cwd: tmpDir,
}),
Expand Down
1 change: 1 addition & 0 deletions test/connectors/libsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("connectors: libsql", () => {
}
mkdirSync(dirname(dbPath), { recursive: true });
testConnector({
dialect: "libsql",
connector: libSql({
url: `file:${dbPath}`,
}),
Expand Down
17 changes: 17 additions & 0 deletions test/connectors/planetscale.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe } from "vitest";
import connector from "../../src/connectors/planetscale";
import { testConnector } from "./_tests";

describe.runIf(process.env.PLANETSCALE_HOST && process.env.PLANETSCALE_USERNAME && process.env.PLANETSCALE_PASSWORD)(
"connectors: planetscale.test",
() => {
testConnector({
dialect: "mysql",
connector: connector({
host: process.env.PLANETSCALE_HOST!,
username: process.env.PLANETSCALE_USERNAME!,
password: process.env.PLANETSCALE_PASSWORD!,
}),
});
},
);
1 change: 1 addition & 0 deletions test/connectors/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe.runIf(process.env.POSTGRESQL_URL)(
"connectors: postgresql.test",
() => {
testConnector({
dialect: "postgresql",
connector: connector({
url: process.env.POSTGRESQL_URL!,
}),
Expand Down