Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.JSONPathBuilder#key

Kysely dialect for PostgreSQL using the Postgres.js client.
method kysely.JSONPathBuilder.prototype.key
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/mod.ts";
const { JSONPathBuilder } = kysely;

Access a property of a JSON object.

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

See also {@link 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 {@link 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 any[] extends O ? never : O extends object ? keyof NonNullable<O> & string : never
optional
O2 = undefined extends O ? null | NonNullable<NonNullable<O>[K]> : null extends O ? null | NonNullable<NonNullable<O>[K]> : string extends keyof NonNullable<O> ? null | NonNullable<NonNullable<O>[K]> : NonNullable<O>[K]