diff --git a/app/src/data/connection/postgres/custom.ts b/app/src/data/connection/postgres/custom.ts index 4229638..22e7809 100644 --- a/app/src/data/connection/postgres/custom.ts +++ b/app/src/data/connection/postgres/custom.ts @@ -6,7 +6,7 @@ import { PostgresIntrospector } from "./PostgresIntrospector"; export type Constructor = new (...args: any[]) => T; export type CustomPostgresConnection = { - supports?: PostgresConnection["supported"]; + supports?: Partial; fn?: Partial; plugins?: KyselyPlugin[]; excludeTables?: string[]; diff --git a/app/src/index.ts b/app/src/index.ts index 154d510..89b4cc7 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -165,7 +165,10 @@ export { PostgresJsConnection, type PostgresJsConfig, } from "data/connection/postgres/PostgresJsConnection"; -export { createCustomPostgresConnection } from "data/connection/postgres/custom"; +export { + createCustomPostgresConnection, + type CustomPostgresConnection, +} from "data/connection/postgres/custom"; // data prototype export { diff --git a/packages/postgres/examples/neon.ts b/examples/postgres/neon.ts similarity index 72% rename from packages/postgres/examples/neon.ts rename to examples/postgres/neon.ts index f4efd02..0261f90 100644 --- a/packages/postgres/examples/neon.ts +++ b/examples/postgres/neon.ts @@ -1,8 +1,8 @@ import { serve } from "bknd/adapter/bun"; -import { createCustomPostgresConnection } from "../src"; +import { createCustomPostgresConnection } from "bknd"; import { NeonDialect } from "kysely-neon"; -const neon = createCustomPostgresConnection(NeonDialect); +const neon = createCustomPostgresConnection("neon", NeonDialect); export default serve({ connection: neon({ diff --git a/examples/postgres/package.json b/examples/postgres/package.json new file mode 100644 index 0000000..ea1ad6b --- /dev/null +++ b/examples/postgres/package.json @@ -0,0 +1,20 @@ +{ + "name": "postgres", + "version": "0.0.0", + "private": true, + "type": "module", + "dependencies": { + "pg": "^8.14.0", + "postgres": "^3.4.7", + "@xata.io/client": "^0.0.0-next.v93343b9646f57a1e5c51c35eccf0767c2bb80baa", + "@xata.io/kysely": "^0.2.1", + "kysely-neon": "^1.3.0", + "bknd": "file:../app", + "kysely": "0.27.6" + }, + "devDependencies": { + "@types/bun": "^1.2.5", + "@types/node": "^22.13.10", + "@types/pg": "^8.11.11" + } +} diff --git a/packages/postgres/tsconfig.json b/examples/postgres/tsconfig.json similarity index 92% rename from packages/postgres/tsconfig.json rename to examples/postgres/tsconfig.json index 5bb10f6..7fb1ec5 100644 --- a/packages/postgres/tsconfig.json +++ b/examples/postgres/tsconfig.json @@ -25,7 +25,8 @@ "skipLibCheck": true, "baseUrl": ".", "paths": { - "$bknd/*": ["../../app/src/*"] + "bknd": ["../app/src/index.ts"], + "bknd/*": ["../app/src/*"] } }, "include": ["./src/**/*.ts"], diff --git a/packages/postgres/examples/xata.ts b/examples/postgres/xata.ts similarity index 72% rename from packages/postgres/examples/xata.ts rename to examples/postgres/xata.ts index 75e2a32..d6b5c07 100644 --- a/packages/postgres/examples/xata.ts +++ b/examples/postgres/xata.ts @@ -1,23 +1,23 @@ import { serve } from "bknd/adapter/bun"; -import { createCustomPostgresConnection } from "../src"; +import { createCustomPostgresConnection } from "bknd"; import { XataDialect } from "@xata.io/kysely"; import { buildClient } from "@xata.io/client"; const client = buildClient(); -const xata = new client({ +const xataClient = new client({ databaseURL: process.env.XATA_URL, apiKey: process.env.XATA_API_KEY, branch: process.env.XATA_BRANCH, }); -const connection = createCustomPostgresConnection(XataDialect, { +const xata = createCustomPostgresConnection("xata", XataDialect, { supports: { batching: false, }, -})({ xata }); +}); export default serve({ - connection, + connection: xata(xataClient), // ignore this, it's only required within this repository // because bknd is installed via "workspace:*" distPath: "../../../app/dist", diff --git a/packages/postgres/README.md b/packages/postgres/README.md deleted file mode 100644 index ed5d1e6..0000000 --- a/packages/postgres/README.md +++ /dev/null @@ -1,102 +0,0 @@ -# Postgres adapter for `bknd` (experimental) -This packages adds an adapter to use a Postgres database with [`bknd`](https://github.com/bknd-io/bknd). It works with both `pg` and `postgres` drivers, and supports custom postgres connections. -* works with any Postgres database (tested with Supabase, Neon, Xata, and RDS) -* choose between `pg` and `postgres` drivers -* create custom postgres connections with any kysely postgres dialect - -## Installation -Install the adapter with: -```bash -npm install @bknd/postgres -``` - -## Using `pg` driver -Install the [`pg`](https://github.com/brianc/node-postgres) driver with: -```bash -npm install pg -``` - -Create a connection: - -```ts -import { pg } from "@bknd/postgres"; - -// accepts `pg` configuration -const connection = pg({ - host: "localhost", - port: 5432, - user: "postgres", - password: "postgres", - database: "postgres", -}); - -// or with a connection string -const connection = pg({ - connectionString: "postgres://postgres:postgres@localhost:5432/postgres", -}); -``` - -## Using `postgres` driver - -Install the [`postgres`](https://github.com/porsager/postgres) driver with: -```bash -npm install postgres -``` - -Create a connection: - -```ts -import { postgresJs } from "@bknd/postgres"; - -// accepts `postgres` configuration -const connection = postgresJs("postgres://postgres:postgres@localhost:5432/postgres"); -``` - -## Using custom postgres dialects - -You can create a custom kysely postgres dialect by using the `createCustomPostgresConnection` function. - -```ts -import { createCustomPostgresConnection } from "@bknd/postgres"; - -const connection = createCustomPostgresConnection("my_postgres_dialect", MyDialect)({ - // your custom dialect configuration - supports: { - batching: true - }, - excludeTables: ["my_table"], - plugins: [new MyKyselyPlugin()], -}); -``` - -### Custom `neon` connection - -```typescript -import { createCustomPostgresConnection } from "@bknd/postgres"; -import { NeonDialect } from "kysely-neon"; - -const connection = createCustomPostgresConnection("neon", NeonDialect)({ - connectionString: process.env.NEON, -}); -``` - -### Custom `xata` connection - -```typescript -import { createCustomPostgresConnection } from "@bknd/postgres"; -import { XataDialect } from "@xata.io/kysely"; -import { buildClient } from "@xata.io/client"; - -const client = buildClient(); -const xata = new client({ - databaseURL: process.env.XATA_URL, - apiKey: process.env.XATA_API_KEY, - branch: process.env.XATA_BRANCH, -}); - -const connection = createCustomPostgresConnection("xata", XataDialect, { - supports: { - batching: false, - }, -})({ xata }); -``` \ No newline at end of file diff --git a/packages/postgres/package.json b/packages/postgres/package.json deleted file mode 100644 index 036d6a9..0000000 --- a/packages/postgres/package.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "name": "@bknd/postgres", - "version": "0.2.0", - "type": "module", - "main": "dist/index.js", - "module": "dist/index.js", - "types": "dist/index.d.ts", - "publishConfig": { - "access": "public" - }, - "scripts": { - "build": "tsup", - "test": "bun test", - "typecheck": "tsc --noEmit", - "updater": "bun x npm-check-updates -ui", - "prepublishOnly": "bun run typecheck && bun run test && bun run build", - "docker:start": "docker run --rm --name bknd-test-postgres -d -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=bknd -p 5430:5432 postgres:17", - "docker:stop": "docker stop bknd-test-postgres" - }, - "optionalDependencies": { - "kysely": "^0.27.6", - "kysely-postgres-js": "^2.0.0", - "pg": "^8.14.0", - "postgres": "^3.4.7" - }, - "devDependencies": { - "@types/bun": "^1.2.5", - "@types/node": "^22.13.10", - "@types/pg": "^8.11.11", - "@xata.io/client": "^0.0.0-next.v93343b9646f57a1e5c51c35eccf0767c2bb80baa", - "@xata.io/kysely": "^0.2.1", - "bknd": "workspace:*", - "kysely-neon": "^1.3.0", - "tsup": "^8.4.0" - }, - "tsup": { - "entry": ["src/index.ts"], - "format": ["esm"], - "target": "es2022", - "metafile": true, - "clean": true, - "minify": true, - "dts": true, - "external": ["bknd", "pg", "postgres", "kysely", "kysely-postgres-js"] - }, - "files": ["dist", "README.md", "!*.map", "!metafile*.json"] -}