How to use Response DTO with Drizzle and Zod. (Nest.js)

In Nest.js, when using the default TypeORM, you create and use DTOs separately for responses, apart from the Entity. With DrizzleORM, you can use createSelectSchema provided by Drizzle to reference an existing schema, generate types, and use them as DTOs.

export const SelectUsersSchema = createSelectSchema(users);
export type SelectUserDTO = z.infer<typeof SelectUsersSchema>;
You can now specify SelectUserDTO as the response type:
@Get(':id')
public async findOne(@Param('id') id: string): Promise<CreateExhibitionDTO> {
...
}

With DrizzleORM's Relational Query, you can also adjust the returned columns:

public async findOne(id: number): Promise<SelectExhibitionDTO> {
return this.db.query.users.findFirst({
columns: {
updatedAt: false,
createdAt: false,
deletedAt: false,
},
...
});
}