Ross
Ross
KKysely
Created by Ross on 8/21/2024 in #help
Return type when fields are conditional
Oh, nice. Thank you
6 replies
KKysely
Created by Ross on 8/21/2024 in #help
Return type when fields are conditional
With some help from ChatGPT 4o, I eventually got to this which works:
export function selectActivitiesByActivityId<T extends (keyof ActivityHistoryTable)[]>(
activityIds: number[],
fields?: T
): Promise<Array<Pick<ActivityHistoryTable, T[number]>>> {
const db = createKysely();
let query = db.selectFrom(ACTIVITIES_TABLE);

if (fields && fields.length > 0) {
query = query.select(fields);
} else {
query = query.selectAll();
}
query = query.where('activity_id', 'in', activityIds);
return query.execute() as Promise<Array<Pick<ActivityHistoryTable, T[number]>>>;
}
export function selectActivitiesByActivityId<T extends (keyof ActivityHistoryTable)[]>(
activityIds: number[],
fields?: T
): Promise<Array<Pick<ActivityHistoryTable, T[number]>>> {
const db = createKysely();
let query = db.selectFrom(ACTIVITIES_TABLE);

if (fields && fields.length > 0) {
query = query.select(fields);
} else {
query = query.selectAll();
}
query = query.where('activity_id', 'in', activityIds);
return query.execute() as Promise<Array<Pick<ActivityHistoryTable, T[number]>>>;
}
6 replies
KKysely
Created by Ross on 1/9/2024 in #help
Argument of type 'RawBuilder<unknown>' is not assignable to...
Your recommendation for the query fails with InvalidArgument, I've reverted as the original works. Providing <number> the sql worked a treat though. Thank you
9 replies
KKysely
Created by Ross on 1/9/2024 in #help
Argument of type 'RawBuilder<unknown>' is not assignable to...
In that it returns the correct results
9 replies
KKysely
Created by Ross on 1/9/2024 in #help
Argument of type 'RawBuilder<unknown>' is not assignable to...
The query does work, however.
9 replies
KKysely
Created by Ross on 1/9/2024 in #help
Argument of type 'RawBuilder<unknown>' is not assignable to...
I did but it didn't register with me for some reason, apologies Types are once again a little bit stricter in some places. You might get type errors from code like where('first_name', '=', sqlsomething). You need to explicitly give a type for sql expressions like this sql<string>something
9 replies
KKysely
Created by Ross on 9/11/2023 in #help
Deferred Join
Worked a treat and sped up my two queries as hoped. Thanks again.
12 replies
KKysely
Created by Ross on 9/11/2023 in #help
Deferred Join
Fantastic, thank you so much
12 replies
KKysely
Created by Ross on 9/11/2023 in #help
Deferred Join
12 replies
KKysely
Created by Ross on 9/11/2023 in #help
Deferred Join
Thank you - I'd just managed to come to a similar point but what I'm struggling with now is generating the order by part dynamically, and then applying that. I'm just putting together a playground to illustrate
12 replies
KKysely
Created by Ross on 9/5/2023 in #help
Subquery on same table
Thanks, I'll have a play with that
7 replies
KKysely
Created by Ross on 9/5/2023 in #help
Subquery on same table
I've managed to achieve it by using the sql tag like this:
query.where(
'activityId',
'in',
sql`(SELECT activityId FROM activities WHERE MATCH(bigTextField) AGAINST (${value} IN BOOLEAN MODE))`,
);
query.where(
'activityId',
'in',
sql`(SELECT activityId FROM activities WHERE MATCH(bigTextField) AGAINST (${value} IN BOOLEAN MODE))`,
);
but I reckon there must be a better way...
7 replies