Drivers
kern-db runs statements through a driver for the PostgreSQL client of your application. Install your client, and pass the driver for it to createDatabase:
- postgres.js:
kern-db/postgres-js. - node-postgres:
kern-db/node-postgres.
In an application of kern-orm, install the client with kern-orm instead of kern-db, and import the drivers from kern-orm/db/postgres-js and kern-orm/db/node-postgres.
For another client, write a driver.
postgres.js
If your application uses postgres.js, install it with kern-db:
npm install kern-db postgrespnpm add kern-db postgresbun add kern-db postgresPass your client to postgresJsDriver:
import { createDatabase } from "kern-db"
import { postgresJsDriver } from "kern-db/postgres-js"
import postgres from "postgres"
export const database = createDatabase({
driver: postgresJsDriver(postgres(process.env.DATABASE_URL!)),
})database.close() ends the client, so do not call sql.end() yourself.
kern-db reads results as PostgreSQL returns them, so create the client without transforms of the column names or rows of results, such as postgres.camel, and give other code that relies on such a transform a client of its own. In an application of kern-orm, name the columns of your tables in snake case with its naming conventions instead.
node-postgres
If your application uses node-postgres, install it with kern-db, and @types/pg for its types:
npm install kern-db pg
npm install --save-dev @types/pgpnpm add kern-db pg
pnpm add --save-dev @types/pgbun add kern-db pg
bun add --dev @types/pgPass your pool to nodePostgresDriver:
import { createDatabase } from "kern-db"
import { nodePostgresDriver } from "kern-db/node-postgres"
import { Pool } from "pg"
export const database = createDatabase({
driver: nodePostgresDriver(new Pool({ connectionString: process.env.DATABASE_URL })),
})database.close() ends the pool, so do not call pool.end() yourself.