npm.io
0.2.1 • Published yesterday

@xfcfam/xf-sql-postgres

Licence
MIT
Version
0.2.1
Deps
1
Size
30 kB
Vulns
0
Weekly
11

@xfcfam/xf-sql-postgres

PostgreSQL dialect adapter for @xfcfam/xf-sql — Kysely's PostgresDialect over pg, plus the SQLSTATE → typed-Exception translation, so the Business Layer never sees pg errors.

pg and kysely are bundled and wired internally — the adapter builds the pg.Pool and the dialect in its constructor. You never install or import either.

Install

npm i @xfcfam/xf @xfcfam/xf-sql @xfcfam/xf-sql-postgres

Quick start

import { PostgresDatabaseRepository } from '@xfcfam/xf-sql-postgres'
import { UniqueViolationException } from '@xfcfam/xf-sql'

export class UsersDb extends PostgresDatabaseRepository<Schema> {
  constructor() { super({ connectionString: process.env.DATABASE_URL!, pool: { max: 10 } }) }

  createUser(input: NewUser) {
    return this.exec(() =>
      this.db.insertInto('users').values(input).returningAll().executeTakeFirstOrThrow())
  }
}

Exported Components

Generalizations
Component Description
PostgresDatabaseRepository<Schema> Extends TransactionalDatabaseRepository; wires PostgresDialect + pg.Pool from PostgresOptions (connectionString + optional pool). Its translateError maps pg SQLSTATEs to the @xfcfam/xf-sql exceptions.
Utilities
Component Description
PostgresErrorUtils Maps any pg / Kysely error to the matching @xfcfam/xf-sql exception.

SQLSTATE mapping — 23505UniqueViolation, 23503ForeignKeyViolation, 23514Check, 23502NotNull, 40P01Deadlock, transport codes → Connection. table / column / constraint are forwarded onto the exception.

Documentation

Full specification → xfcfam.org · SQLSTATE codes

License

MIT

Keywords