Interface SqlDialect

SQL dialect abstraction for cross-platform SQL generation.

Each method returns a SQL string fragment or full statement. Implementations are pure string transformers — no database calls.

Expressions and placeholders passed in are inserted as written; numbers, units, types and JSON paths are checked, so nothing a caller passes as a value reaches the SQL text unchecked.

interface SqlDialect {
    name: "sqlite" | "postgres";
    insertOrIgnore(table: string, columns: string[], placeholders: string[]): string;
    insertOrReplace(table: string, columns: string[], placeholders: string[], primaryKey?: string): string;
    upsert(table: string, columns: string[], placeholders: string[], conflictColumns: string[], updateColumns?: string[]): string;
    jsonExtract(column: string, jsonPath: string): string;
    jsonExtractAs(column: string, jsonPath: string, type: JsonValueType): string;
    jsonArrayContains(jsonArray: string, valueExpr: string): string;
    ifnull(expr: string, fallback: string): string;
    autoIncrementPrimaryKey(options?: AutoIncrementOptions): string;
    syncIdentitySequence?(table: string, column: string): null | string;
    changeNotificationSql(table: string, channel: string): null | string[];
    columnType(kind: ColumnKind): string;
    pragma(key: string, value: string): null | string;
    placeholder(index: number): string;
    now(): string;
    cast(expr: string, type: CastType): string;
    addInterval(expr: string, amount: number, unit: IntervalUnit): string;
    dateBucket(unit: DateBucketUnit, expr: string): string;
    ilike(expr: string, pattern: string): string;
    inList(expr: string, items: string[]): string;
    inArray(expr: string, arrayParam: string): string;
    rowLock(mode: "update" | "share", wait?: "wait" | "skipLocked" | "noWait"): string;
    advisoryTransactionLock(keyExpr: string): null | string;
    greatest(...exprs: string[]): string;
    least(...exprs: string[]): string;
    uuid(): string;
}

Implemented by

Properties

name: "sqlite" | "postgres"

Dialect identifier.

Methods

  • Generate an INSERT OR IGNORE statement. SQLite: INSERT OR IGNORE INTO t (a, b) VALUES (?, ?) Postgres: INSERT INTO t (a, b) VALUES ($1, $2) ON CONFLICT DO NOTHING

    Parameters

    • table: string
    • columns: string[]
    • placeholders: string[]

    Returns string

  • Generate an upsert on the primary key that updates the existing row in place, on both engines: INSERT INTO t (a, b) VALUES (?, ?) ON CONFLICT (a) DO UPDATE SET b = excluded.b. The row keeps the columns the statement does not name, and its dependents. When every column is the key, the statement does nothing on a conflict.

    Parameters

    • table: string
    • columns: string[]
    • placeholders: string[]
    • OptionalprimaryKey: string

      The conflict target. Defaults to the first column.

    Returns string

  • An upsert with an explicit conflict target that updates the existing row in place on both engines (SQLite 3.24 and later): INSERT INTO t (a, b, c) VALUES (?, ?, ?) ON CONFLICT (a) DO UPDATE SET b = excluded.b, c = excluded.c.

    Parameters

    • table: string
    • columns: string[]
    • placeholders: string[]
    • conflictColumns: string[]

      The unique or primary-key columns of the conflict target.

    • OptionalupdateColumns: string[]

      The columns to update; by default every column outside the target. None left: DO NOTHING.

    Returns string

  • Generate a JSON field extraction expression. SQLite: json_extract(col, '$.key') Postgres: (col::jsonb)->>'key'

    Parameters

    • column: string
    • jsonPath: string

    Returns string

  • A value at jsonPath ($.a.b, $.a[0]) read as text, an integer or a real, with the same result on both engines; json reads the JSON value itself (jsonb on PostgreSQL, JSON text on SQLite), for use inside another expression such as jsonArrayContains. A path outside that form is refused.

    Parameters

    Returns string

  • Whether the JSON array jsonArray holds the value valueExpr, compared as text, so a string or a number matches on both engines.

    Parameters

    • jsonArray: string
    • valueExpr: string

    Returns string

  • Generate a null-coalesce expression. SQLite: ifnull(expr, fallback) Postgres: COALESCE(expr, fallback)

    Parameters

    • expr: string
    • fallback: string

    Returns string

  • Column definition for an auto-incrementing integer primary key. SQLite: INTEGER PRIMARY KEY AUTOINCREMENT, which takes an id an insert gives as well. Postgres: INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, which refuses one; with acceptExplicitIds, GENERATED BY DEFAULT, which takes it. After inserting ids, run syncIdentitySequence.

    Parameters

    Returns string

  • A statement that moves an identity or serial column's sequence past the largest value in the table, so the next generated id does not collide with ids inserted explicitly. Postgres: SELECT setval(...). SQLite: null, since a rowid and AUTOINCREMENT follow the largest id by themselves. The table may be schema-qualified; names are SQL identifiers. Optional for existing custom dialects; callers may fall back to no statement.

    Parameters

    • table: string
    • column: string

    Returns null | string

  • Statements that make every write to table, from any session, send a notification on channel: one per statement, its payload JSON {"schema", "table", "operation"} with the operation insert, update or delete (TRUNCATE too). Postgres: a trigger function and a statement-level trigger, which PostgresAdapter.listen hears. SQLite: null, since SQLite has no notifications between connections.

    Parameters

    • table: string
    • channel: string

    Returns null | string[]

  • A column type for DDL. PostgreSQL: TEXT, INTEGER, BIGINT, DOUBLE PRECISION, BOOLEAN, TIMESTAMPTZ, JSONB, UUID, BYTEA. SQLite: TEXT, INTEGER, INTEGER, REAL, INTEGER, TEXT, TEXT, TEXT, BLOB. The values a column returns follow each engine (a PostgreSQL boolean is true, a SQLite one 1); typeParsers and the bound-value conversions narrow that.

    Parameters

    Returns string

  • Generate a PRAGMA statement or equivalent. SQLite: PRAGMA key = value Postgres: returns null (skip — Postgres enforces FKs by default, etc.)

    Parameters

    • key: string
    • value: string

    Returns null | string

  • Parameter placeholder for the given 0-based index. SQLite: ? Postgres: $1, $2, etc.

    Parameters

    • index: number

    Returns string

  • The current time. PostgreSQL: now() (timestamptz). SQLite: ISO 8601 UTC text with milliseconds, the form a bound Date takes, so text comparisons with stored times order correctly.

    Returns string

  • expr cast to a type both engines read the same way. timestamp is timestamptz on PostgreSQL and ISO 8601 UTC text on SQLite.

    Parameters

    Returns string

  • The time expr moved by amount units (negative moves back), as an exact number of seconds on both engines. PostgreSQL: timestamptz. SQLite: ISO 8601 UTC text.

    Parameters

    Returns string

  • The start of the minute, hour, day, month or year that holds expr, in UTC, as ISO 8601 text (2026-10-15T00:00:00.000Z) on both engines.

    Parameters

    Returns string

  • Case-insensitive LIKE. PostgreSQL: ILIKE. SQLite: LIKE, which folds only ASCII letters.

    Parameters

    • expr: string
    • pattern: string

    Returns string

  • expr IN (...) over the given placeholders or expressions; an empty list matches nothing.

    Parameters

    • expr: string
    • items: string[]

    Returns string

  • Membership in an array bound as one parameter. PostgreSQL: = ANY(param) (pg binds a JavaScript array as an array). SQLite: IN (SELECT value FROM json_each(param)) over the JSON text the array binds as.

    Parameters

    • expr: string
    • arrayParam: string

    Returns string

  • A row-lock clause to append to a SELECT. PostgreSQL: FOR UPDATE or FOR SHARE, with SKIP LOCKED or NOWAIT. SQLite: '', since a write transaction there holds the whole database.

    Parameters

    • mode: "update" | "share"
    • Optionalwait: "wait" | "skipLocked" | "noWait"

    Returns string

  • A statement that takes a transaction-scoped advisory lock on an integer key, released at COMMIT or ROLLBACK. PostgreSQL: SELECT pg_advisory_xact_lock(key). SQLite: null, to skip, since its write transactions already run one at a time.

    Parameters

    • keyExpr: string

    Returns null | string

  • The largest of the expressions, NULLs skipped on both engines; NULL when all are.

    Parameters

    • Rest...exprs: string[]

    Returns string

  • The smallest of the expressions, NULLs skipped on both engines; NULL when all are.

    Parameters

    • Rest...exprs: string[]

    Returns string

  • A random version 4 UUID as text. PostgreSQL: gen_random_uuid() (13 and later). SQLite: built from randomblob().

    Returns string