postgres: move examples up

This commit is contained in:
dswbx
2025-10-31 20:11:16 +01:00
parent dbb19a27f4
commit ed47c5bf51
8 changed files with 34 additions and 159 deletions

View File

@@ -6,7 +6,7 @@ import { PostgresIntrospector } from "./PostgresIntrospector";
export type Constructor<T> = new (...args: any[]) => T;
export type CustomPostgresConnection = {
supports?: PostgresConnection["supported"];
supports?: Partial<PostgresConnection["supported"]>;
fn?: Partial<DbFunctions>;
plugins?: KyselyPlugin[];
excludeTables?: string[];

View File

@@ -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 {

View File

@@ -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({

View File

@@ -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"
}
}

View File

@@ -25,7 +25,8 @@
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"$bknd/*": ["../../app/src/*"]
"bknd": ["../app/src/index.ts"],
"bknd/*": ["../app/src/*"]
}
},
"include": ["./src/**/*.ts"],

View File

@@ -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",

View File

@@ -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 });
```

View File

@@ -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"]
}