zod-prisma-types icon indicating copy to clipboard operation
zod-prisma-types copied to clipboard

Implemented on #256 UpdateOperationsInput Type Generation Disabled

Open StringKe opened this issue 1 year ago • 1 comments

The types of SomeUpdateArgsSchema do not use the where condition of methods such as find.

    update: adminProcedure.input(ProductAttributeUpdateArgsSchema).mutation(async ({ input }) => {
        const database = getDatabase();

        if (!input.where.id) {
            throw BusinessError.fromCode('ID_NOT_FOUND');
        }

        const data = await database.productAttribute.findUnique({
            where: {
                id: input.where.id, // SomeUpdateArgsSchema not worker this
            },
        });

        if (!data) {
            throw BusinessError.fromCode('ID_NOT_FOUND');
        }

        return database.productAttribute.update({
            where: {
                id: input.where.id,
            },
            data: input.data,
        });
    }),

StringKe avatar May 31 '24 06:05 StringKe

Hey @StringKe thanks for the pr.

in your example above you describe that the SomeUpdateArgsSchema does not use the where condition of SomeFindUniqueArgsSchema, which would be SomeWhereUniqueInputSchema, but actually it does - at least in my generated schemas.

From the code example I assume you want to have the input.where.id field to be mandatory. There seems to be an issue in my implementation of the generated whereUniqueInputSchema. Prisma uses the AtLeast type, that ensures, that at least the provided unique field is used in the query, but my implementation currently does not reflect that because the mandatory id field gets overwritten by the optional id field like in this example

export const ModelWithOptionsWhereUniqueInputSchema: z.ZodType<Prisma.ModelWithOptionsWhereUniqueInput> = z.object({
  id: z.number().int()
})
.and(z.object({
  id: z.number().int().optional(),
  AND: z.union([ z.lazy(() => ModelWithOptionsWhereInputSchema),z.lazy(() => ModelWithOptionsWhereInputSchema).array() ]).optional(),
  OR: z.lazy(() => ModelWithOptionsWhereInputSchema).array().optional(),
  NOT: z.union([ z.lazy(() => ModelWithOptionsWhereInputSchema),z.lazy(() => ModelWithOptionsWhereInputSchema).array() ]).optional(),
  string: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(),
}).strict());

This schema should reflect the type

  export type ModelWithOptionsWhereUniqueInput = Prisma.AtLeast<{
    id?: number
    AND?: ModelWithOptionsWhereInput | ModelWithOptionsWhereInput[]
    OR?: ModelWithOptionsWhereInput[]
    NOT?: ModelWithOptionsWhereInput | ModelWithOptionsWhereInput[]
    string?: StringFilter<"ModelWithOptions"> | string
  }, "id">

where at least the id field should be mandatory.

so this should have to do nothing with the UpdateOperationsInput like in your PR. Or did you have some other things in mind with the optionality of the UpdateOperationsInput?

chrishoermann avatar Dec 02 '24 21:12 chrishoermann