Marcel Overdijk
Marcel Overdijk
Explore posts from servers
KKysely
Created by Marcel Overdijk on 9/9/2024 in #help
Dynamic query parts based on user input
Is that a serious answer, or being sarcastic? Nevertheless, I'm looking to see if this is possible with Kylesy or not...
5 replies
KKysely
Created by Marcel Overdijk on 9/9/2024 in #help
Dynamic query parts based on user input
e.g. I did a similar experiment with Drizzle ORM, and there I did something like:
const countryTableAlias = aliasedTable(countryTable, 'coun');
const countryJoin = leftJoin(countryTableAlias, eq(countryTableAlias.id, constructorTable.countryId));

const countryContinentTableAlias = aliasedTable(continentTable, 'coun_cont');
const countryContinentJoin = leftJoin(countryContinentTableAlias, eq(countryContinentTableAlias.id, countryTableAlias.continentId));

queryFields : {
'name': { col: constructorTable.name },
'country.code': { col: countryTableAlias.alpha2Code, join: countryJoin },
'country.continent.code': { col: countryContinentTableAlias.code, join: [countryJoin, countryContinentJoin] },
}
const countryTableAlias = aliasedTable(countryTable, 'coun');
const countryJoin = leftJoin(countryTableAlias, eq(countryTableAlias.id, constructorTable.countryId));

const countryContinentTableAlias = aliasedTable(continentTable, 'coun_cont');
const countryContinentJoin = leftJoin(countryContinentTableAlias, eq(countryContinentTableAlias.id, countryTableAlias.continentId));

queryFields : {
'name': { col: constructorTable.name },
'country.code': { col: countryTableAlias.alpha2Code, join: countryJoin },
'country.continent.code': { col: countryContinentTableAlias.code, join: [countryJoin, countryContinentJoin] },
}
which if fully type safe.
5 replies
KKysely
Created by Marcel Overdijk on 9/5/2024 in #help
Error: don't await SelectQueryBuilder instances directly.
I'm now using this:
const qb = c.var.mydb
.selectFrom('customer')
.selectAll('customer');

const { query } = await this.applyQueryParams(qb)

const customers = await query.execute();
const qb = c.var.mydb
.selectFrom('customer')
.selectAll('customer');

const { query } = await this.applyQueryParams(qb)

const customers = await query.execute();
9 replies
KKysely
Created by Marcel Overdijk on 9/5/2024 in #help
Error: don't await SelectQueryBuilder instances directly.
thx @koskimas I already was using that (hopefully temporary) when I got the error:
async applyQueryParams<DB, TB extends keyof DB, O>(qb: SelectQueryBuilder<DB, TB, O>): Promise<{ qb: SelectQueryBuilder<DB, TB, O> }> {
..
return { qb };
}
async applyQueryParams<DB, TB extends keyof DB, O>(qb: SelectQueryBuilder<DB, TB, O>): Promise<{ qb: SelectQueryBuilder<DB, TB, O> }> {
..
return { qb };
}
and then using it like:
qb = (await this.applyQueryParams(qb)).qb;
qb = (await this.applyQueryParams(qb)).qb;
and that works. I must admit, it doesn't really look pretty 😉 my other alternative is to split retrieving the async request data and enhancing the query. that way I can make it non async
9 replies
KKysely
Created by Marcel Overdijk on 9/5/2024 in #help
Error: don't await SelectQueryBuilder instances directly.
9 replies
KKysely
Created by Marcel Overdijk on 9/5/2024 in #help
Error: don't await SelectQueryBuilder instances directly.
9 replies
KKysely
Created by Marcel Overdijk on 9/5/2024 in #help
Error: don't await SelectQueryBuilder instances directly.
I found this related issue as well: https://github.com/kysely-org/kysely/issues/838 but it does not give a solution, or maybe there is none, and an async function can't return a query builder?
9 replies