[FEATURE]: Support for Database Views in Drizzle Zod
Describe what you want
Dear Drizzle Team,
I am currently using Drizzle ORM with drizzle-zod and would like to inquire if there are any plans to support database views. At the moment, it seems that drizzle-zod does not directly generate Zod schemas for database views. However, adding this capability would be highly beneficial for those of us who rely on views to optimize data access and structure.
In my project, I use multiple database views to generate JSON responses for APIs. This approach allows me to efficiently organize and retrieve data for API responses. If drizzle-zod could support views, it would greatly enhance my ability to ensure type safety across these JSON responses, making my development process more efficient and reliable.
Could you let me know if this feature is something the team might consider in the near future?
Thank you very much for your attention, and I look forward to any updates you may have regarding this topic.
Best regards, reinsp5
I've temporarily written my own script for this, it isn't as intuitive as I'd like, so would definitely like this too!
function mapColumnTypeToZod(columnType: string): any {
switch (columnType) {
case 'PgInteger':
return z.number();
case 'PgVarchar':
return z.string();
case 'PgText':
return z.string();
case 'PgTimestampString':
return z.string(); // or z.date() if parsed as Date object
case 'PgBigInt53':
return z.bigint();
default:
return z.any(); // fallback for unknown types
}
}
function buildViewSchema(table: PgView) {
const symbols = Object.getOwnPropertySymbols(table);
const viewBaseConfigSymbol = symbols.find(symbol =>
symbol.toString() === 'Symbol(drizzle:ViewBaseConfig)'
);
if (!viewBaseConfigSymbol) {
throw new Error('ViewBaseConfig symbol not found');
}
const name = (table as any)[viewBaseConfigSymbol].name;
const columns = [];
const selectedFields = (table as any)[viewBaseConfigSymbol].selectedFields;
// Iterate through each field in `selectedFields`
for (const [columnName, columnObj] of Object.entries(selectedFields)) {
const columnType: string = (columnObj as any).columnType || "Unknown Type";
if (columnName === 'id') {
console.log('Skipping id');
continue;
}
columns.push({ columnName, zodType: mapColumnTypeToZod(columnType.toString()) });
}
return {name, columns};
}
I'm facing something similar.
I've bumped drizzle-orm (fromv0.32.1 to v0.36.0) and drizzle-kit (from v0.23.0 to v0.27.0) and executed the introspect command. As a result I've noticed the following change in my schema:
Essentially my PostgreSQL view went from being a pgTable to pgView and as a result the createSelectSchema from drizzle-zod stopped working and started to throw a TypeError: Cannot convert undefined or null to object
I guess that this change was introduced in [email protected] (link: https://github.com/drizzle-team/drizzle-orm/releases/tag/drizzle-kit%400.26.0)
Additionally, the productVw is no longer found in the query property:
return db.query.productVw.findMany({
where: and(...filters),
});
This one was fixed in the latest [email protected]
Dear contributors, thank you for accepting and implementing this feature request! I look forward to putting it to good use right away (โงโโฆ)