Skip to main content Link Search Menu Expand Document (external link)

Statement overview

Added in v1.0.0


Table of contents


compiler

Compiler (interface)

Signature

export interface Compiler {
  readonly dialect: Dialect
  readonly compile: (
    statement: Fragment,
    withoutTransform: boolean
  ) => readonly [sql: string, params: ReadonlyArray<Primitive>]
}

Added in v1.0.0

makeCompiler

Signature

export declare const makeCompiler: <C extends Custom<any, any, any, any> = any>(options: {
  readonly dialect: Dialect
  readonly placeholder: (index: number, value: unknown) => string
  readonly onIdentifier: (value: string, withoutTransform: boolean) => string
  readonly onRecordUpdate: (
    placeholders: string,
    alias: string,
    columns: string,
    values: ReadonlyArray<ReadonlyArray<Primitive>>,
    returning: readonly [sql: string, params: ReadonlyArray<Primitive>] | undefined
  ) => readonly [sql: string, params: ReadonlyArray<Primitive>]
  readonly onCustom: (
    type: C,
    placeholder: (u: unknown) => string,
    withoutTransform: boolean
  ) => readonly [sql: string, params: ReadonlyArray<Primitive>]
  readonly onInsert?: (
    columns: ReadonlyArray<string>,
    placeholders: string,
    values: ReadonlyArray<ReadonlyArray<Primitive>>,
    returning: readonly [sql: string, params: ReadonlyArray<Primitive>] | undefined
  ) => readonly [sql: string, binds: ReadonlyArray<Primitive>]
  readonly onRecordUpdateSingle?: (
    columns: ReadonlyArray<string>,
    values: ReadonlyArray<Primitive>,
    returning: readonly [sql: string, params: ReadonlyArray<Primitive>] | undefined
  ) => readonly [sql: string, params: ReadonlyArray<Primitive>]
}) => Compiler

Added in v1.0.0

makeCompilerSqlite

Signature

export declare const makeCompilerSqlite: (transform?: ((_: string) => string) | undefined) => Compiler

Added in v1.0.0

constructor

and

Signature

export declare const and: (clauses: ReadonlyArray<string | Fragment>) => Fragment

Added in v1.0.0

csv

Signature

export declare const csv: {
  (values: ReadonlyArray<string | Fragment>): Fragment
  (prefix: string, values: ReadonlyArray<string | Fragment>): Fragment
}

Added in v1.0.0

custom

Signature

export declare const custom: <C extends Custom<any, any, any, any>>(
  kind: C["kind"]
) => (i0: C["i0"], i1: C["i1"], i2: C["i2"]) => Fragment

Added in v1.0.0

join

Signature

export declare const join: (
  literal: string,
  addParens?: boolean,
  fallback?: string
) => (clauses: ReadonlyArray<string | Fragment>) => Fragment

Added in v1.0.0

make

Signature

export declare const make: (
  acquirer: Connection.Acquirer,
  compiler: Compiler,
  spanAttributes?: ReadonlyArray<readonly [string, unknown]>
) => Constructor

Added in v1.0.0

or

Signature

export declare const or: (clauses: ReadonlyArray<string | Fragment>) => Fragment

Added in v1.0.0

unsafeFragment

Signature

export declare const unsafeFragment: (sql: string, params?: ReadonlyArray<Primitive> | undefined) => Fragment

Added in v1.0.0

guard

isCustom

Signature

export declare const isCustom: <A extends Custom<any, any, any, any>>(kind: A["kind"]) => (u: unknown) => u is A

Added in v1.0.0

isFragment

Signature

export declare const isFragment: (u: unknown) => u is Fragment

Added in v1.0.0

model

Argument (type alias)

Signature

export type Argument = Primitive | Helper | Fragment

Added in v1.0.0

ArrayHelper (interface)

Signature

export interface ArrayHelper {
  readonly _tag: "ArrayHelper"
  readonly value: ReadonlyArray<Primitive | Fragment>
}

Added in v1.0.0

Constructor (interface)

Signature

export interface Constructor {
  <A extends object = Row>(strings: TemplateStringsArray, ...args: Array<Argument>): Statement<A>

  (value: string): Identifier

  /**
   * Create unsafe SQL query
   */
  readonly unsafe: <A extends object>(sql: string, params?: ReadonlyArray<Primitive> | undefined) => Statement<A>

  readonly literal: (sql: string) => Fragment

  readonly in: {
    (value: ReadonlyArray<Primitive>): ArrayHelper
    (column: string, value: ReadonlyArray<Primitive>): Fragment
  }

  readonly insert: {
    (value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>): RecordInsertHelper
    (value: Record<string, Primitive | Fragment | undefined>): RecordInsertHelper
  }

  /** Update a single row */
  readonly update: <A extends Record<string, Primitive | Fragment | undefined>>(
    value: A,
    omit?: ReadonlyArray<keyof A>
  ) => RecordUpdateHelperSingle

  /**
   * Update multiple rows
   *
   * **Note:** Not supported in sqlite
   */
  readonly updateValues: (
    value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>,
    alias: string
  ) => RecordUpdateHelper

  /**
   * Create an `AND` chain for a where clause
   */
  readonly and: (clauses: ReadonlyArray<string | Fragment>) => Fragment

  /**
   * Create an `OR` chain for a where clause
   */
  readonly or: (clauses: ReadonlyArray<string | Fragment>) => Fragment

  /**
   * Create comma seperated values, with an optional prefix
   *
   * Useful for `ORDER BY` and `GROUP BY` clauses
   */
  readonly csv: {
    (values: ReadonlyArray<string | Fragment>): Fragment
    (prefix: string, values: ReadonlyArray<string | Fragment>): Fragment
  }

  readonly join: (
    literal: string,
    addParens?: boolean,
    fallback?: string
  ) => (clauses: ReadonlyArray<string | Fragment>) => Fragment

  readonly onDialect: <A, B, C, D, E>(options: {
    readonly sqlite: () => A
    readonly pg: () => B
    readonly mysql: () => C
    readonly mssql: () => D
    readonly clickhouse: () => E
  }) => A | B | C | D | E

  readonly onDialectOrElse: <A, B = never, C = never, D = never, E = never, F = never>(options: {
    readonly orElse: () => A
    readonly sqlite?: () => B
    readonly pg?: () => C
    readonly mysql?: () => D
    readonly mssql?: () => E
    readonly clickhouse?: () => F
  }) => A | B | C | D | E | F
}

Added in v1.0.0

Custom (interface)

Signature

export interface Custom<T extends string = string, A = void, B = void, C = void> {
  readonly _tag: "Custom"
  readonly kind: T
  readonly i0: A
  readonly i1: B
  readonly i2: C
}

Added in v1.0.0

Dialect (type alias)

Signature

export type Dialect = "sqlite" | "pg" | "mysql" | "mssql" | "clickhouse"

Added in v1.0.0

Fragment (interface)

Signature

export interface Fragment {
  readonly [FragmentId]: (_: never) => FragmentId
  readonly segments: ReadonlyArray<Segment>
}

Added in v1.0.0

Helper (type alias)

Signature

export type Helper =
  | ArrayHelper
  | RecordInsertHelper
  | RecordUpdateHelper
  | RecordUpdateHelperSingle
  | Identifier
  | Custom

Added in v1.0.0

Identifier (interface)

Signature

export interface Identifier {
  readonly _tag: "Identifier"
  readonly value: string
}

Added in v1.0.0

Literal (interface)

Signature

export interface Literal {
  readonly _tag: "Literal"
  readonly value: string
  readonly params?: ReadonlyArray<Primitive> | undefined
}

Added in v1.0.0

Parameter (interface)

Signature

export interface Parameter {
  readonly _tag: "Parameter"
  readonly value: Primitive
}

Added in v1.0.0

Primitive (type alias)

Signature

export type Primitive = string | number | bigint | boolean | Date | null | Int8Array | Uint8Array

Added in v1.0.0

PrimitiveKind (type alias)

Signature

export type PrimitiveKind = "string" | "number" | "bigint" | "boolean" | "Date" | "null" | "Int8Array" | "Uint8Array"

Added in v1.0.0

RecordInsertHelper (interface)

Signature

export interface RecordInsertHelper {
  readonly _tag: "RecordInsertHelper"
  readonly value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>
  /** @internal */
  readonly returningIdentifier: string | Fragment | undefined
  readonly returning: (sql: string | Identifier | Fragment) => RecordInsertHelper
}

Added in v1.0.0

RecordUpdateHelper (interface)

Signature

export interface RecordUpdateHelper {
  readonly _tag: "RecordUpdateHelper"
  readonly value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>
  readonly alias: string
  /** @internal */
  readonly returningIdentifier: string | Fragment | undefined
  readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelper
}

Added in v1.0.0

RecordUpdateHelperSingle (interface)

Signature

export interface RecordUpdateHelperSingle {
  readonly _tag: "RecordUpdateHelperSingle"
  readonly value: Record<string, Primitive | Fragment | undefined>
  readonly omit: ReadonlyArray<string>
  /** @internal */
  readonly returningIdentifier: string | Fragment | undefined
  readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelperSingle
}

Added in v1.0.0

Segment (type alias)

Signature

export type Segment =
  | Literal
  | Identifier
  | Parameter
  | ArrayHelper
  | RecordInsertHelper
  | RecordUpdateHelper
  | RecordUpdateHelperSingle
  | Custom

Added in v1.0.0

Statement (interface)

Signature

export interface Statement<A> extends Fragment, Effect<ReadonlyArray<A>, SqlError>, Pipeable {
  readonly raw: Effect<unknown, SqlError>
  readonly withoutTransform: Effect<ReadonlyArray<A>, SqlError>
  readonly stream: Stream.Stream<A, SqlError>
  readonly values: Effect<ReadonlyArray<ReadonlyArray<Primitive>>, SqlError>
  readonly unprepared: Effect<ReadonlyArray<A>, SqlError>
  readonly compile: (withoutTransform?: boolean | undefined) => readonly [sql: string, params: ReadonlyArray<Primitive>]
}

Added in v1.0.0

Statement (namespace)

Added in v1.0.0

Transformer (type alias)

Signature

export type Transformer = (
  self: Statement<unknown>,
  sql: Constructor,
  context: FiberRefs.FiberRefs,
  span: Tracer.Span
) => Effect<Statement<unknown>>

Added in v1.0.0

transformer

currentTransformer

Signature

export declare const currentTransformer: FiberRef.FiberRef<Option.Option<Statement.Transformer>>

Added in v1.0.0

setTransformer

Signature

export declare const setTransformer: (f: Statement.Transformer) => Layer.Layer<never, never, never>

Added in v1.0.0

withTransformer

Signature

export declare const withTransformer: {
  (f: Statement.Transformer): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
  <A, E, R>(effect: Effect<A, E, R>, f: Statement.Transformer): Effect<A, E, R>
}

Added in v1.0.0

withTransformerDisabled

Signature

export declare const withTransformerDisabled: <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>

Added in v1.0.0

type id

FragmentId

Signature

export declare const FragmentId: typeof FragmentId

Added in v1.0.0

FragmentId (type alias)

Signature

export type FragmentId = typeof FragmentId

Added in v1.0.0

utils

defaultEscape

Signature

export declare const defaultEscape: (c: string) => (str: string) => string

Added in v1.0.0

defaultTransforms

Signature

export declare const defaultTransforms: (
  transformer: (str: string) => string,
  nested?: boolean
) => {
  readonly value: (value: any) => any
  readonly object: (obj: Record<string, any>) => any
  readonly array: <A extends object>(rows: ReadonlyArray<A>) => ReadonlyArray<A>
}

Added in v1.0.0

primitiveKind

Signature

export declare const primitiveKind: (value: Primitive) => PrimitiveKind

Added in v1.0.0