autobotkilla
autobotkilla
Explore posts from servers
KKysely
Created by autobotkilla on 5/6/2023 in #help
Is there a way to execute an ExpressionBuilder?
oh. That's pretty simple. Thanks!!
8 replies
KKysely
Created by autobotkilla on 5/6/2023 in #help
Is there a way to execute an ExpressionBuilder?
Hmmm. I'm trying to create reusable select queries so that the same shape can be returned in different circumstances. Do you have an alternative suggestion? Here's an example.
export function selectJob(db?: DittoDb) {
const eb = createExpressionBuilder<DB, 'Job'>(db?.getExecutor());

// prettier-ignore
return eb
.selectFrom('Job')
.select([
'Job.id',
'Job.status',
'Job.title',
withUserInfo('Job.ownerId').as('owner'),
withUserInfo('Job.managerId').as('manager'),
])
}

export function withJob(jobIdColumn: any) {
return jsonObjectFrom(selectJob().whereRef('Job.id', '=', jobIdColumn));
}

export function getJobById(id: string) {
return selectJob(db).where('Job.id', '=', id).execute();
}
export function selectJob(db?: DittoDb) {
const eb = createExpressionBuilder<DB, 'Job'>(db?.getExecutor());

// prettier-ignore
return eb
.selectFrom('Job')
.select([
'Job.id',
'Job.status',
'Job.title',
withUserInfo('Job.ownerId').as('owner'),
withUserInfo('Job.managerId').as('manager'),
])
}

export function withJob(jobIdColumn: any) {
return jsonObjectFrom(selectJob().whereRef('Job.id', '=', jobIdColumn));
}

export function getJobById(id: string) {
return selectJob(db).where('Job.id', '=', id).execute();
}
This way I can get the same Job object when I select a job directly or include a Job in another query, like getting a JobApplication. I'm looking for a way to define reusable shapes.
8 replies
KKysely
Created by autobotkilla on 5/6/2023 in #help
Is there a way to execute an ExpressionBuilder?
Figured it out. The function createExpressionBuilder accepts an optional QueryExecutor
export function createExpressionBuilder<DB, TB extends keyof DB>(
executor: QueryExecutor = NOOP_QUERY_EXECUTOR
): ExpressionBuilder<DB, TB
export function createExpressionBuilder<DB, TB extends keyof DB>(
executor: QueryExecutor = NOOP_QUERY_EXECUTOR
): ExpressionBuilder<DB, TB
You can get a QueryExecutor from your Kysely<DB> instance.
const eb = createExpressionBuilder<DB, 'User'>(db.getExecutor());
const eb = createExpressionBuilder<DB, 'User'>(db.getExecutor());
8 replies
KKysely
Created by autobotkilla on 4/14/2023 in #help
Problems typing a generic withPerson helper
Thanks for looking at this @Igal. The workaround isn't terrible actually since it allows me to limit the callers to tables with a foreign key.
9 replies
KKysely
Created by autobotkilla on 4/14/2023 in #help
Problems typing a generic withPerson helper
Here's a repro on Kysely playground https://kyse.link/?p=s&i=8gXYLeE8i8LQVeEFSjsv
9 replies
KKysely
Created by autobotkilla on 4/14/2023 in #help
Problems typing a generic withPerson helper
I'm trying to make it so that any table that has a foreign key to the person table can load the same person data, but pass in the column reference for that table. For example, Message.senderId or Project.ownerId both point to the person table.
9 replies
KKysely
Created by autobotkilla on 4/14/2023 in #help
Problems typing a generic withPerson helper
So I tried making the function more generic.
export function withPerson<
TDB extends DB,
TTB extends keyof TDB,
As extends string>(
builder: ExpressionBuilder<TDB, TTB>,
personIdCol: ReferenceExpression<TDB, TTB>,
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<
TDB extends DB,
TTB extends keyof TDB,
As extends string>(
builder: ExpressionBuilder<TDB, TTB>,
personIdCol: ReferenceExpression<TDB, TTB>,
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);
}
This fixes the typescript error when I call the function, but messes up the typing inside the function on the .whereRef and .select calls. Argument of type 'string' is not assignable to parameter of type 'ReferenceExpression<TDB, TTB | ("Person" extends keyof TDB ? keyof TDB & "Person" : never)>'. I just can't seem to figure this out. I'd really appreciate any help.
9 replies