K
Kysely•2w ago
fed

Flatten type of single column select?

Hi! I have helpers that look roughly like this:
function ownerId(organizationId: Expression<number>) {
eb.selectFrom('organization')
.where('id', '=', organizationId)
.select('ownerId')
}

function personName(id: Expression<number>) {
eb.selectFrom('person')
.where('id', '=', id)
.select('name')
}
function ownerId(organizationId: Expression<number>) {
eb.selectFrom('organization')
.where('id', '=', organizationId)
.select('ownerId')
}

function personName(id: Expression<number>) {
eb.selectFrom('person')
.where('id', '=', id)
.select('name')
}
the problem is that i cannot compose these helpers. in other words, i cannot do the following:
const orgOwnerId = ownerId(ref('organization.id'));
const orgOwnerName = personName(ownerId);
const orgOwnerId = ownerId(ref('organization.id'));
const orgOwnerName = personName(ownerId);
because the type of orgOwnerId does not match the type of the id arg required by personName any suggestions on how to solve this? somehow "flattening" the return type of ownerId to Expression<number> would be ideal, if that's possible and that 👆 should be valid, since ownerId is a single-row single-column select? i thought about using $castTo, but one concern is that $castTo gives up type safety...
Solution:
Hey 👋 There's a $asScalar() utility method that's probably landing in v0.28. https://github.com/kysely-org/kysely/blob/v0.28/src/query-builder/select-query-builder.ts#L1938-L1986...
Jump to solution
2 Replies
Solution
Igal
Igal•2w ago
Hey 👋 There's a $asScalar() utility method that's probably landing in v0.28. https://github.com/kysely-org/kysely/blob/v0.28/src/query-builder/select-query-builder.ts#L1938-L1986
fed
fedOP•2w ago
nice thanks, i'll keep an eye out

Did you find this page helpful?