P
Prisma•4d ago
piscopancer

Infer type from create query?

here's the function that creates a new Schedule. Attention to the shape, not data
async function createSchedule() {
return db.schedule.create({
data: {
name: '',
days: {
create: [
{
holiday: true,
independentWorkDay: true,
lessons: {
create: [
{
place: 'some place',
type: 'lecture' satisfies LessonType,
subject: {
connect: {
id: 12, // the important part that the type would have this subject's id. There's no such field in `Prisma.LessonCreateArgs` ofc, so I need to infer the type from this function
},
},
},
],
},
},
],
},
},
})
}
async function createSchedule() {
return db.schedule.create({
data: {
name: '',
days: {
create: [
{
holiday: true,
independentWorkDay: true,
lessons: {
create: [
{
place: 'some place',
type: 'lecture' satisfies LessonType,
subject: {
connect: {
id: 12, // the important part that the type would have this subject's id. There's no such field in `Prisma.LessonCreateArgs` ofc, so I need to infer the type from this function
},
},
},
],
},
},
],
},
},
})
}
I want to get the type of the Lesson of the shape in this function. so it has to look like this
/**
* place: string
* type: string
* subject: {
* id: number
* }
*/
/**
* place: string
* type: string
* subject: {
* id: number
* }
*/
so that I can use somewhere else
const [lessons, setLessons] = useState<LessonToCreate[]>([])
setLessons([
{
place: '402',
type: 'seminar',
subject: {
id: 1
}
}
])
const [lessons, setLessons] = useState<LessonToCreate[]>([])
setLessons([
{
place: '402',
type: 'seminar',
subject: {
id: 1
}
}
])
Is there a utility type in prisma to infer this shape?
Solution:
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
Jump to solution
2 Replies
Solution
Nurul
Nurul•2d ago
Hello @piscopancer 👋 Have you seen this guide? https://prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types#problem-using-variations-of-the-generated-model-type To me, it seems that you want to generate a type with relation fields (subject relation in this example)
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
piscopancer
piscopancerOP•2d ago
thanks it will help me sounds like what I need ofc
Want results from more Discord servers?
Add your server