Cloudflare - v1.3.0
    Preparing search index...

    Class D1Binding<E>

    Accessor for the D1 database binding (d1_databases in wrangler.json).

    Every query helper takes the statement and its bind values separately, so user input is always parameterised rather than interpolated into SQL.

    const db = new D1Binding() // binding name from the configuration layer
    const user = await db.first<User>(ctx.env, 'SELECT * FROM users WHERE id = ?', id)

    Bayu Dwiyan Satria

    1.0.0

    1.0.0

    Type Parameters

    Hierarchy (View Summary)

    Index
    • get name(): string

      The binding name this instance resolves.

      Returns string

      The binding name as declared in wrangler.json.

    • Runs a query and returns every row.

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      • env: E

        The Worker environment.

      • query: string

        SQL statement, using ? placeholders for bind values.

      • ...values: unknown[]

        Values bound to the placeholders, in order.

      Returns Promise<T[]>

      The matched rows, empty when the query matched nothing.

    • Runs several prepared statements in a single batch.

      Statements execute in order and are wrapped in an implicit transaction, so a failure rolls the whole batch back.

      Type Parameters

      • T = unknown

      Parameters

      • env: E

        The Worker environment.

      • statements: D1PreparedStatement[]

        Statements built via D1Binding.prepare.

      Returns Promise<D1Result<T>[]>

      One result per statement, in the order supplied.

    • Executes raw SQL, typically a migration or a multi-statement script.

      Parameters

      • env: E

        The Worker environment.

      • query: string

        One or more SQL statements separated by newlines.

      Returns Promise<D1ExecResult>

      The execution summary, including the number of statements run.

      This path does not support bind values — never build a statement here from user input. Use D1Binding.run for anything parameterised.

    • Runs a query and returns the first row.

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      • env: E

        The Worker environment.

      • query: string

        SQL statement, using ? placeholders for bind values.

      • ...values: unknown[]

        Values bound to the placeholders, in order.

      Returns Promise<T>

      The first matched row, or null when the query matched nothing.

    • Checks whether the binding is available on the given environment.

      Use this to degrade gracefully when a binding is optional.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Prepares a statement, binding the supplied values when any are given.

      Parameters

      • env: E

        The Worker environment.

      • query: string

        SQL statement, using ? placeholders for bind values.

      • ...values: unknown[]

        Values bound to the placeholders, in order.

      Returns D1PreparedStatement

      The prepared statement, ready to run.

      MissingBindingError When the D1 binding is absent.

    • Runs a write statement and returns its execution metadata.

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      • env: E

        The Worker environment.

      • query: string

        SQL statement, using ? placeholders for bind values.

      • ...values: unknown[]

        Values bound to the placeholders, in order.

      Returns Promise<D1Result<T>>

      The D1 result, including meta.changes and meta.last_row_id.

    • Resolves the binding without throwing.

      Parameters

      • env: E

        The Worker environment.

      Returns D1Database

      The resolved binding, or null when it is not configured.