Is this a correct situation to use $castTo (jsonArrayFrom + Left Join)
// snippet from main query:
jsonArrayFrom(
eb
.selectFrom('address')
.leftJoin('stateconst', 'address.stateId', 'stateconst.id')
.leftJoin('contact', 'address.id', 'contact.addressId')
.leftJoin('contacttype', 'contact.typeId', 'contacttype.id')
.whereRef('address.accountId', '=', 'customer.accountId')
.select([
'address.id',
'address.name',
'address.addressName',
'address.address as street',
'address.city',
'stateconst.code as state',
'address.zip',
// contacts
jsonArrayFrom(
eb
.selectFrom('contact')
.leftJoin('contacttype', 'contact.typeId', 'contacttype.id')
.select([
'contact.id',
'contact.contactName as name',
'contact.datus as data',
'contacttype.name as type',
])
.whereRef('contact.addressId', '=', 'address.id')
).as('contacts'),
])
)
.$castTo<
{
id: AddressId;
name: string;
addressName: string;
street: string;
city: string;
state: string;
zip: string;
contacts: {
id: ContactId;
name: string;
data: string;
type: string;
}[];
}[]
>()
.as('addresses'),
// snippet from main query:
jsonArrayFrom(
eb
.selectFrom('address')
.leftJoin('stateconst', 'address.stateId', 'stateconst.id')
.leftJoin('contact', 'address.id', 'contact.addressId')
.leftJoin('contacttype', 'contact.typeId', 'contacttype.id')
.whereRef('address.accountId', '=', 'customer.accountId')
.select([
'address.id',
'address.name',
'address.addressName',
'address.address as street',
'address.city',
'stateconst.code as state',
'address.zip',
// contacts
jsonArrayFrom(
eb
.selectFrom('contact')
.leftJoin('contacttype', 'contact.typeId', 'contacttype.id')
.select([
'contact.id',
'contact.contactName as name',
'contact.datus as data',
'contacttype.name as type',
])
.whereRef('contact.addressId', '=', 'address.id')
).as('contacts'),
])
)
.$castTo<
{
id: AddressId;
name: string;
addressName: string;
street: string;
city: string;
state: string;
zip: string;
contacts: {
id: ContactId;
name: string;
data: string;
type: string;
}[];
}[]
>()
.as('addresses'),
0 Replies