drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

Add `createUpdateSchema` to drizzle-zod

Open dankochetov opened this issue 2 years ago • 2 comments

dankochetov avatar Apr 23 '23 14:04 dankochetov

I think the current createInsertSchema with partial() works pretty well, but causes confusion because of the name. Having a shortcut for createUpdateSchema is not not useful, ...but as a point of consideration have you thought about renaming the current createInsertSchema to something universal like createMutationSchema or createMutateSchema?

tacomanator avatar Apr 24 '23 00:04 tacomanator

+1

tylerzey avatar Jun 02 '23 13:06 tylerzey

I think the current createInsertSchema with partial() works pretty well.

Some sort of selection criteria (typically a key, like ID) seems like it would have to be mandatory for update, but should generally not be present for insert.

Here's an example of approximately what I've landed on for CRUD, but you can imagine why you might want to separate that need and use the selection schema for an update criteria.

const apiMutateUser = createInsertSchema(users, refine).omit({
  created_at: true,
  updated_at: true,
});
export const apiInsertUser = apiMutateUser.omit({ id: true });
export const apiUpdateUser = apiMutateUser.partial().required({ id: true });
export const apiDeleteUser = apiMutateUser.pick({ id: true });
export const apiSelectUser = createSelectSchema(users, refine)
  .pick(selectableFields)
  .partial();

gburtini avatar Apr 23 '24 22:04 gburtini