npm.io
0.2.1 • Published yesterday

@xfcfam/xf-sql

Licence
MIT
Version
0.2.1
Deps
1
Size
88 kB
Vulns
0
Weekly
19

@xfcfam/xf-sql

SQL Access Generalization for the XF Architecture Model — the Kysely query builder behind one XF-canonical class, with a typed Exception hierarchy. Dialect-agnostic.

Pair it with a dialect adapter — @xfcfam/xf-sql-postgres today (MySQL / SQLite to follow) — or pass any Kysely Dialect directly.

Install

npm i @xfcfam/xf @xfcfam/xf-sql
# + a dialect adapter (it bundles its own driver), e.g. Postgres:
npm i @xfcfam/xf-sql-postgres

Quick start

import { TransactionalDatabaseRepository } from '@xfcfam/xf-sql'

export class UsersDb extends TransactionalDatabaseRepository<Schema> {
  constructor() { super({ dialect /* from an adapter */ }) }
  getUser(id: number) {
    return this.exec(() =>
      this.db.selectFrom('users').where('id', '=', id).selectAll().executeTakeFirstOrThrow())
  }
}

Exported Components

Generalizations
Component Description
DatabaseRepository<Schema> The SQL contract over Kysely (this.db). exec() funnels driver errors through translateError into the typed DatabaseException family — Connection / UniqueViolation / ForeignKeyViolation / CheckViolation / NotNullViolation / Deadlock. Plus a stringly-typed built-in CRUD surface (no Schema required) — the data analogue of RestRepository's verbs: findById · findOne · find · insert · insertMany · update · updateMany · delete · deleteMany · exists · count · pluck · keymap · group · paginate · run · query (raw $1…) · scalar. Filters { col: value | value[] | null }= / IN / IS NULL.
TransactionalDatabaseRepository<Schema> Adds commit / rollback-on-throw, error-translated.

xf-sql defines the Exception types but does not translate driver errors itself — use a dialect adapter (which overrides translateError) or override it yourself. Wrap queries in this.exec(() => …) so the translation runs.

Batching is a Business concern: pair BatchedBusiness (from @xfcfam/xf) with transaction() to flush many writes as one transaction — dialect-agnostic.

Documentation

Full specification → xfcfam.org · kysely.dev

License

MIT

Keywords