K
Kysely13mo ago
oof2win2

Is there a way to not have as many Selectable<> generics?

Hi. I'm using prisma-kysely to generate my Kysely types, and it generates them with the Generated generic. I therefore need to use the Selectable generic to change it to the return type of what is selected when I want to use the type. Is there a way to avoid using it so often?
6 Replies
koskimas
koskimas13mo ago
You can give the selectable version a shorter alias:
export type Person = Selectable<PersonTable>
export type Person = Selectable<PersonTable>
Or better yet, trust type inference whenever possible and don't use an explicit type.
oof2win2
oof2win213mo ago
the issue is that I can't assign the right type to function calls without using selectables, but otherwise it makes sense thanks 👍
koskimas
koskimas13mo ago
Could you provide an example of what you mean?
oof2win2
oof2win213mo ago
const applications = await ctx.db
.selectFrom("DelegationApplication")
.selectAll()
.execute()

const fnA = (a: DelegationApplication): number => a.actualSize // this line errors, imported type which as actualSize as Generated
const fnB = (b: (typeof applications)[0]): number => b.actualSize
const fnB = (b: Selectable<DelegationApplication>): number => b.actualSize
const applications = await ctx.db
.selectFrom("DelegationApplication")
.selectAll()
.execute()

const fnA = (a: DelegationApplication): number => a.actualSize // this line errors, imported type which as actualSize as Generated
const fnB = (b: (typeof applications)[0]): number => b.actualSize
const fnB = (b: Selectable<DelegationApplication>): number => b.actualSize
fnA will have a TS error because actualSize is Generated<number>, whilst fnB and fnC will work without TS errors. kind of annoying to have to specify everything is Selectable but i guess it makes sense
koskimas
koskimas13mo ago
Well as I mentioned, you only need to do it once and then import that type. The table interfaces are not meant to be used as row types. Instead of importing DelegationApplication you'd import DelegationApplicationRow or whatever you want to call it. The DelegationApplication should only really be used in the database "schema" type, and never anywhere else. This is what I do:
interface PersonTable {
id: Generated<string>
first_name: string
last_name: string null
}

export interface Database {
person: PersonTable
}

// Only export these
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type PersonUpdate = Updateable<PersonTable>
interface PersonTable {
id: Generated<string>
first_name: string
last_name: string null
}

export interface Database {
person: PersonTable
}

// Only export these
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type PersonUpdate = Updateable<PersonTable>
You could open an issue in prisma-kysely github if they'd consider exporting those by default from the generated file
oof2win2
oof2win213mo ago
sounds great 👍
Want results from more Discord servers?
Add your server
More Posts