Class TraversedJSONPathBuilder<S, O>

An expression with an as method.

Type Parameters

  • S

  • O

Hierarchy

Implements

Constructors

Methods

  • Access an element of a JSON array in a specific location.

    Since there's no guarantee an element exists in the given array location, the resulting type is always nullable. If you're sure the element exists, you should use $assertType to narrow the type safely.

    See also key to access properties of JSON objects.

    Examples

    db.selectFrom('person').select(eb =>
    eb.ref('nicknames', '->').at(0).as('primary_nickname')
    )

    The generated SQL (PostgreSQL):

    ```sql
    select "nicknames"->0 as "primary_nickname" from "person"

    Combined with {@link key}:

    db.selectFrom('person').select(eb =>
    eb.ref('experience', '->').at(0).key('role').as('first_role')
    )

    The generated SQL (PostgreSQL):

    select "experience"->0->'role' as "first_role" from "person"
    

    You can use 'last' to access the last element of the array in MySQL:

    db.selectFrom('person').select(eb =>
    eb.ref('nicknames', '->$').at('last').as('last_nickname')
    )

    The generated SQL (MySQL):

    select `nicknames`->'$[last]' as `last_nickname` from `person`
    

    Or '#-1' in SQLite:

    db.selectFrom('person').select(eb =>
    eb.ref('nicknames', '->>$').at('#-1').as('last_nickname')
    )

    The generated SQL (SQLite):

    select "nicknames"->>'$[#-1]' as `last_nickname` from `person`
    

    Type Parameters

    • I extends number | `#-${number}` | "last"

    • O2 = null | NonNullable<NonNullable<O>[keyof NonNullable<O> & number]>

    Parameters

    • index: `${I}` extends `${any}.${any}` | `#--${any}`
          ? never
          : I

    Returns TraversedJSONPathBuilder<S, O2>

  • Access a property of a JSON object.

    If a field is optional, the resulting type will be nullable.

    See also at to access elements of JSON arrays.

    Examples

    db.selectFrom('person').select(eb =>
    eb.ref('address', '->').key('city').as('city')
    )

    The generated SQL (PostgreSQL):

    select "address"->'city' as "city" from "person"
    

    Going deeper:

    db.selectFrom('person').select(eb =>
    eb.ref('profile', '->$').key('website').key('url').as('website_url')
    )

    The generated SQL (MySQL):

    select `profile`->'$.website.url' as `website_url` from `person`
    

    Combined with at:

    db.selectFrom('person').select(eb =>
    eb.ref('profile', '->').key('addresses').at(0).key('city').as('city')
    )

    The generated SQL (PostgreSQL):

    select "profile"->'addresses'->0->'city' as "city" from "person"
    

    Type Parameters

    • K extends string

    • O2 = undefined extends O
          ? null | NonNullable<NonNullable<O>[K]>
          : null extends O
              ? NonNullable<NonNullable<O>[K]> | O & null
              : string extends keyof NonNullable<O>
                  ? null | NonNullable<NonNullable<O>[K]>
                  : NonNullable<O>[K]

    Parameters

    • key: K

    Returns TraversedJSONPathBuilder<S, O2>

Generated using TypeDoc