Need help improving a custom helper

My dopamine with Kysely is quite high right now, so I am experimenting all sorts of things to create custom helpers for common use-cases. Here's an example of a working demo https://old.kyse.link/?p=s&i=qkTHjgwIuyb847wjoRd3 Following is a trimmed snippet from the above mentioned demo, for which I need help.
function month<DB, TB extends keyof DB & string>(eb: ExpressionBuilder<DB, TB>, col: {
[K in keyof DB[TB]]: DB[TB][K] extends Date | { __select__: Date } ? K : never
}[keyof DB[TB]]) {
return sql<number>`EXTRACT(MONTH from ${sql.ref('created_at')}::date)`
}

const users = await db
.selectFrom('users')
.selectAll()
.where((eb) => {
return eb(month(eb, 'created_at'), '=', 11)
})
.select((eb) => {
return month(eb, 'created_at').as('creationMonth')
})
.executeTakeFirst()
function month<DB, TB extends keyof DB & string>(eb: ExpressionBuilder<DB, TB>, col: {
[K in keyof DB[TB]]: DB[TB][K] extends Date | { __select__: Date } ? K : never
}[keyof DB[TB]]) {
return sql<number>`EXTRACT(MONTH from ${sql.ref('created_at')}::date)`
}

const users = await db
.selectFrom('users')
.selectAll()
.where((eb) => {
return eb(month(eb, 'created_at'), '=', 11)
})
.select((eb) => {
return month(eb, 'created_at').as('creationMonth')
})
.executeTakeFirst()
Is it possible for me use the month helper without passing it the eb instance explicitly? I know for the most part the answer is NO. But still want to check my luck and see if there is something I am unaware of.
Solution:
Yeah unfortunately typescript doesn't bend that way. We wouldn't need the whole ExpressionBuilder callback thingy anywhere if it did. We could just use free functions and they'd somehow catch the context through generics in the return value. another option is month(eb)('created_at')....
Igal
Igal•37d ago
Hey 👋 So far, only possible while passing eb as argument. Anything I tried resulted in <never, unknown>
virk
virk•37d ago
Yeah, I suspected that. I think passing eb explicitly is fine as well. Its just month('created_at') reads a little better than month(eb, 'created_at') Thanks a ton for looking into it 🙂
Solution
koskimas
koskimas•36d ago
Yeah unfortunately typescript doesn't bend that way. We wouldn't need the whole ExpressionBuilder callback thingy anywhere if it did. We could just use free functions and they'd somehow catch the context through generics in the return value. another option is month(eb)('created_at').
Want results from more Discord servers?
Add your server
More Posts
jsonArrayFrom with `as` not being typedI have the following query ```ts await db .selectFrom('user') .selectAll() .select((eb) How to use multiple schema definitions ( e.g. <catalog>.<schema>.<table> )Hey guys, I'm currently working on a new version for our graphql wrapper. Currently we're using kneUsing raw SQL with `or` whereI have been using the raw SQL template tag to work with certain JSON columns and wanted to add a `orCustom Plugin to transform Alias.* to "Alias.Column1", "Alias.Column2", etc.Hi everyone, in the previous question, I asked how to transform the following syntax into a new one.can kysely be extended with custom types within the databasehi. i would like to extend my database with custom types (a date type for sqlite) so that it is easinewbie need help with json_build_objectHi! Just starting with kysely and encountered problem I cant solve. I want to build raw query with kSolved: Exporting query builder classesI saw this PR https://github.com/kysely-org/kysely/pull/763 where you guys have decided not to exporbuilding kysely makes the package size very largehi. i'd be more than happy to figure this out because i like kysely a lot more than drizzle. the thiKysely setup in monolith APIHello, we have a basic http server with a basic router and a PostgreSQL database. We're wondering whIs it possible to get the total count while fetching rows in a single query?I have a query which looks like this: ```typescript const { items, count } = await db .selectFrom(Separating results of join into objects of each typeHey guys, I'm curious if anyone knows a way to unmerge the results of a join into objects of both tySnippet compilationHi, is it somehow possible to get only part of the compiled code to use it as snippet in ORM? Or vicCreating an 'enum' type columnHi all, Trying to re-create this MySQL in Kysely ``` ... CREATE TABLE ...( ... members ENUM('femalError when destructuring QueryCreator in withRecursiveI'm new to Kysely and attempting to migrate a Next app from prisma to kysely. I've generated types,