PrismaP
Prisma15mo ago
6 replies
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
                    },
                  },
                },
              ],
            },
          },
        ],
      },
    },
  })
}

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
 * }
 */

so that I can use somewhere else
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
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)
This page documents various scenarios for using the generated types from the Prisma namespace
Operating against partial structures of your model types | Prisma D...
Was this page helpful?