DominusPrime
DominusPrime
DTDrizzle Team
Created by DominusPrime on 8/4/2023 in #help
Best way to omit a field (e.g. password) after findMany from schema?
Let's say we have the following:
export const User = pgTable(
'user',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
email: varchar('phone', { length: 256 }),
password: text('password'),
createdAt: timestamp('created_at', { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true })
.notNull()
.defaultNow(),
},
(user) => {
return { idIdx: index('id_idx').on(user.id) };
},
);

export const UserRelation = relations(User, ({ many }) => ({
shops: many(Shop),
products: many(Product),
}));

export type NewUser = InferModel<typeof User, 'insert'>;

export type Users = InferModel<typeof User>;
export const User = pgTable(
'user',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
email: varchar('phone', { length: 256 }),
password: text('password'),
createdAt: timestamp('created_at', { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true })
.notNull()
.defaultNow(),
},
(user) => {
return { idIdx: index('id_idx').on(user.id) };
},
);

export const UserRelation = relations(User, ({ many }) => ({
shops: many(Shop),
products: many(Product),
}));

export type NewUser = InferModel<typeof User, 'insert'>;

export type Users = InferModel<typeof User>;
How can we use drizzle-zod to omit the password in (for example) an API response after running a findMany query? The docs mention it can be used for this purpose, but it doesn't show how. Or am I just thinking about this the wrong way? If so, what is the right way?
2 replies