autobotkilla
autobotkilla
Explore posts from servers
KKysely
Created by autobotkilla on 5/6/2023 in #help
Is there a way to execute an ExpressionBuilder?
Using the expression hasDogNamed example from the docs
const eb = expressionBuilder<DB, 'person'>()

const query = eb.selectFrom('pet')
.select('pet.id')
.whereRef('pet.owner_id', '=', 'person.id')
.where('pet.species', '=', 'dog')
.where('pet.name', '=', name)

const result = await query.execute(); <--- This compiles, but doesn't work...
const eb = expressionBuilder<DB, 'person'>()

const query = eb.selectFrom('pet')
.select('pet.id')
.whereRef('pet.owner_id', '=', 'person.id')
.where('pet.species', '=', 'dog')
.where('pet.name', '=', name)

const result = await query.execute(); <--- This compiles, but doesn't work...
Is there a way I can provide an ExpressionBuilder an instance of Kysely<DB> so that it can execute the query?
8 replies
KKysely
Created by autobotkilla on 4/14/2023 in #help
Problems typing a generic withPerson helper
First I want to say that I'm in love with Kysely! It's amazing. I could use some help getting the types right for a helper function I'm creating. I'm trying to follow the relations recipe (the withPets and withMom example from the docs), but I want to make it slightly more reusable. I have a withPerson method
export function withPerson<As extends string>(
builder: ExpressionBuilder<DB, keyof DB>,
personIdCol: ReferenceExpression<DB, keyof DB>,
as: As = 'person' as As
) {
return jsonObjectFrom(
builder
.selectFrom('Person')
.whereRef('Person.id', '=', personIdCol)
.select([
'Person.id',
'Person.givenName',
'Person.familyName',
'Person.picture80',
'Person.picture144'
])
).as(as);
}
export function withPerson<As extends string>(
builder: ExpressionBuilder<DB, keyof DB>,
personIdCol: ReferenceExpression<DB, keyof DB>,
as: As = 'person' as As
) {
return jsonObjectFrom(
builder
.selectFrom('Person')
.whereRef('Person.id', '=', personIdCol)
.select([
'Person.id',
'Person.givenName',
'Person.familyName',
'Person.picture80',
'Person.picture144'
])
).as(as);
}
When I try and use it like this
const result = db
.selectFrom('Prospect')
.select((eb) => withPerson(eb, 'person'))
.execute();
const result = db
.selectFrom('Prospect')
.select((eb) => withPerson(eb, 'person'))
.execute();
I get a typescript error: Argument of type 'ExpressionBuilder<DB, "Prospect">' is not assignable to parameter of type 'ExpressionBuilder<DB, keyof DB>'
9 replies