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.
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"
Generated using TypeDoc
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
Combined with {@link key}:
The generated SQL (PostgreSQL):
You can use
'last'
to access the last element of the array in MySQL:The generated SQL (MySQL):
Or
'#-1'
in SQLite:The generated SQL (SQLite):