valtyr / prisma-kysely

🪄 Generate Kysely types directly from your Prisma schema!
https://www.npmjs.com/package/prisma-kysely
MIT License
935 stars 36 forks source link

Database results is not a compatible type of generated types #48

Closed oof2win2 closed 1 year ago

oof2win2 commented 1 year ago

Hi. It seems that the results from fetching the database are not compatible to what prisma-kysely generates as types, with this issue occuring only on the Generated field.

The error given: Type 'number' is not assignable to type 'ColumnType<number, number | undefined, number>'

The error occurs when attempting to pass a value to a function which expects a type generated by prisma-kysely, however, it works fine when using the Prisma generated type or the query return type.

const applications = await ctx.db
    .selectFrom("DelegationApplication")
    .selectAll()
    .execute()

const fnA = (a: DelegationApplication): number => a.actualSize // this line errors, imported prisma-kysely type
const fnB = (b: (typeof applications)[0]): number => b.actualSize
const fnC = (c: PrismaDelegationApplication): number => c.actualSize // imported Prisma type
igalklebanov commented 1 year ago

Hey 👋

This is not prisma-kysely related.

You should use:

import { Selectable } from 'kysely'

const fnA = (a: Selectable<DelegationApplication>): number => a.actualSize

Another option:

Import { InferResult } from 'kysely'

const query = await ctx.db
    .selectFrom("DelegationApplication")
    .selectAll()

type Applications = InferResult<typeof query>

const application = await query.execute()

const fnB = (b: Applications[number]): number => b.actualSize
oof2win2 commented 1 year ago

Ah okay, that makes sense. Thanks for the help. Would it however make sense to add this to the Gotchas section of the README? As far as I know, there is no mention of this in the kysely docs.

valtyr commented 1 year ago

I'll take that into consideration the next time I revise the README. Closing this for now :D