|
1 | | -# kysely-postgres-js |
| 1 | +# kysely-postgres-js |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +[Kysely](https://github.com/koskimas/kysely) dialect for [PostgreSQL](https://www.postgresql.org/) using the [Postgres.js](https://github.com/porsager/postgres) client under the hood. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +#### NPM 7+ |
| 10 | + |
| 11 | +```bash |
| 12 | +npm i kysely-postgres-js |
| 13 | +``` |
| 14 | + |
| 15 | +#### NPM <7 |
| 16 | + |
| 17 | +```bash |
| 18 | +npm i kysely-postgres-js kysely postgres |
| 19 | +``` |
| 20 | + |
| 21 | +#### Yarn |
| 22 | + |
| 23 | +```bash |
| 24 | +yarn add kysely-postgres-js kysely postgres |
| 25 | +``` |
| 26 | + |
| 27 | +#### PNPM |
| 28 | + |
| 29 | +```bash |
| 30 | +pnpm add kysely-postgres-js kysely postgres |
| 31 | +``` |
| 32 | + |
| 33 | +### Deno |
| 34 | + |
| 35 | +This package uses/extends some [Kysely](https://github.com/koskimas/kysely) types and classes, which are imported using its NPM package name -- not a relative file path or CDN url. It also uses [Postgres.js] which is imported using its NPM package name -- not a relative file path or CDN url. |
| 36 | + |
| 37 | +To fix that, add an [`import_map.json`](https://deno.land/[email protected]/linking_to_external_code/import_maps) file. |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "imports": { |
| 42 | + "kysely": "https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/index.js", |
| 43 | + "postgres": "https://deno.land/x/[email protected]" |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +```ts |
| 51 | +import {Kysely} from 'kysely' |
| 52 | +import {PostgresJSDialect} from 'kysely-postgres-js' |
| 53 | +import postgres from 'postgres' |
| 54 | + |
| 55 | +interface Database { |
| 56 | + person: { |
| 57 | + id: GeneratedAlways<number> |
| 58 | + first_name: string | null |
| 59 | + last_name: string | null |
| 60 | + age: number |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const db = new Kysely<Database>({ |
| 65 | + dialect: new PostgresJSDialect({ |
| 66 | + options: { |
| 67 | + database: 'test', |
| 68 | + host: 'localhost', |
| 69 | + port: 5434, |
| 70 | + user: 'admin', |
| 71 | + }, |
| 72 | + postgres, |
| 73 | + }), |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +## Caveats |
| 78 | + |
| 79 | +### Single connection |
| 80 | + |
| 81 | +[Postgres.js](https://github.com/porsager/postgres) doesn't provide single connection getter method/s. To get a single connection, you have to create an instance with a pool that has at most one connection (`max: 1`). This is not aligned with Kysely's current design. As a result, `db.connection()` will not work as expected when using a pool with more than one connection. |
| 82 | +If you need to use a single connection, you should instantiate a new `Kysely` |
| 83 | +instance with a pool that has at most one connection. |
| 84 | + |
| 85 | +### Transactions |
| 86 | + |
| 87 | +For transactions, this dialect creates additional pools with at most one connection, so `db.transaction().execute(...)` will work as expected. Keep in mind, this means that total number of connections to the database might exceed the pool size passed to Kysely initially. |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +MIT License, see `LICENSE` |
0 commit comments