Migrating from Prisma client to Kysely issue

I am evaluating various aspects of changing from Prisma client to Kysely but i seem to have hit a small snag basic tutorial i followed was from nexxel https://www.nexxel.dev/blog/typesafe-database in my prisma schema i have
model Recipe {
id String @id @default(cuid())
name String
prepTime String
cookTime String
totalTime String
servings String?
description String?
ingredients Json @default("[]")
instructions Json @default("[]")
updatedAt DateTime @updatedAt
}
model Recipe {
id String @id @default(cuid())
name String
prepTime String
cookTime String
totalTime String
servings String?
description String?
ingredients Json @default("[]")
instructions Json @default("[]")
updatedAt DateTime @updatedAt
}
in prisma i was able to do this
return await ctx.prisma.recipe.create({
data: {
name: input.name,
description: input.description,
ingredients: input.ingredients.split('\n'),
instructions: input.instructions.split('\n'),
prepTime: input.prepTime,
cookTime: input.cookTime,
totalTime: input.totalTime,
servings: input.servings,
},
})
return await ctx.prisma.recipe.create({
data: {
name: input.name,
description: input.description,
ingredients: input.ingredients.split('\n'),
instructions: input.instructions.split('\n'),
prepTime: input.prepTime,
cookTime: input.cookTime,
totalTime: input.totalTime,
servings: input.servings,
},
})
but in Kysely its forcing me to add id and updated at otherwise it overloads saying it needs id and updatedAt
return await db.insertInto('Recipe').values({
id: '342432', // how do i handle this autogenrating this from the DB?
updatedAt: new Date(), // how do i handle this autogenrating this from the DB?
name: input.name,
description: input.description,
ingredients: input.ingredients.split('\n'),
instructions: input.instructions.split('\n'),
prepTime: input.prepTime,
cookTime: input.cookTime,
totalTime: input.totalTime,
servings: input.servings,
}).executeTakeFirst()
return await db.insertInto('Recipe').values({
id: '342432', // how do i handle this autogenrating this from the DB?
updatedAt: new Date(), // how do i handle this autogenrating this from the DB?
name: input.name,
description: input.description,
ingredients: input.ingredients.split('\n'),
instructions: input.instructions.split('\n'),
prepTime: input.prepTime,
cookTime: input.cookTime,
totalTime: input.totalTime,
servings: input.servings,
}).executeTakeFirst()
I tried reading the kysely documentation but couldnt find it personally
11 Replies
needmorewood
needmorewood2y ago
my understanding is that the type def given to kysely shows id and updated as mandatory fields ill post that setup as well (more or less the same from the nexxel post)
import type { Recipe } from '@prisma/client/edge'
import { Kysely } from 'kysely'
import { PlanetScaleDialect } from 'kysely-planetscale'

interface Database {
Recipe: Recipe
}

export const db = new Kysely<Database>({
dialect: new PlanetScaleDialect({
host: 'aws.connect.psdb.cloud',
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
}),
})
import type { Recipe } from '@prisma/client/edge'
import { Kysely } from 'kysely'
import { PlanetScaleDialect } from 'kysely-planetscale'

interface Database {
Recipe: Recipe
}

export const db = new Kysely<Database>({
dialect: new PlanetScaleDialect({
host: 'aws.connect.psdb.cloud',
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
}),
})
and the type is
export type Recipe = {
id: string
name: string
prepTime: string
cookTime: string
totalTime: string
servings: string | null
description: string | null
ingredients: Prisma.JsonValue
instructions: Prisma.JsonValue
updatedAt: Date
}
export type Recipe = {
id: string
name: string
prepTime: string
cookTime: string
totalTime: string
servings: string | null
description: string | null
ingredients: Prisma.JsonValue
instructions: Prisma.JsonValue
updatedAt: Date
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
needmorewood
needmorewood2y ago
Ahhh ty Where is generated from?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
needmorewood
needmorewood2y ago
looks good, do you have any way to handle this, i think the appeal of prisma was generating all these things on the fly and it automagically working guess this might be a noob TS question but whats the sane way to extend the recipe type to overide id and updated at?
Igal
Igal2y ago
i think the appeal of prisma was generating all these things on the fly and it automagically working
there's a 3rd party library just for that https://github.com/RobinBlomberg/kysely-codegen
GitHub
GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definit...
Generate Kysely type definitions from your database - GitHub - RobinBlomberg/kysely-codegen: Generate Kysely type definitions from your database
needmorewood
needmorewood2y ago
awesome ill take a look at that
Igal
Igal2y ago
that id field should probably be defined as GeneratedAlways<string> to make it read-only.
guess this might be a noob TS question but whats the sane way to extend the recipe type to overide id and updated at?
probably Omit<Recipe, 'id' | 'updatedAt'> & { id: SomeType, updatedAt: SomeOtherType }
needmorewood
needmorewood2y ago
awesome thanks for both of your help
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
needmorewood
needmorewood2y ago
The cohesiveness and dx is fantastic, just seeing how many ms I can shave off. Having problems with vercel and netlify though which is wild to me
Want results from more Discord servers?
Add your server