Create a view with Drizzle

Hi, need some help to understand how i can create a view with Drizzle. "drizzle-orm": "^0.31.2", "drizzle-kit": "^0.22.6", export const wf_users_in_private_view = pgView("wf_users_in_private_view", { wf_id: text('wf_id'), user_id: text('user_id'), }).as(sqlSELECT DISTINCT ${workflows.id}, jsonb_array_elements(${workflows.data}->'nodes')->'data'->>'id' FROM ${workflows} WHERE ${workflows.is_private} = true) On "npm run generate", and reading Drizzle document i was expected to have an output of "CREATE VIEW", but have: No schema changes, nothing to migrate šŸ˜“
7 Replies
samson
samsonā€¢5mo ago
Running into the same problem. Did you figure this out?
Justin
Justinā€¢5mo ago
No description
Justin
Justinā€¢5mo ago
Basically, currently the code won't generate the views, but will allow you to query against existing ones. It's a known limitation šŸ™‚
goeland
goelandā€¢5mo ago
Hello @Justin , I manually created the view and declared the view in my codebase with the pgView from 'drizzle-orm/pg-core', but I can't find a way to query this view. I expected the db.query.myView.findMany() to work but myView does not exist on type.... Is there some documentation explaining how to query views with drizzle-orm ? Thanks !
Justin
Justinā€¢5mo ago
So here the views are not populating under the db.query object yet, you would need to do your view queries like this instead (for now):
db.select().from(myView)
db.select().from(myView)
I imagine this will improve in time, but this is what works currently
TJ
TJā€¢5mo ago
In the case like this we will have a schema.js to create tables and another SQL file to create the views. I'm wondering how we store and run this SQL file?
goeland
goelandā€¢5mo ago
Hello @Justin , Thanks for the response! Your suggestion works, but I need to use the query builder to avoid refactoring my entire codebase, particularly with the with clauses for including relations. I want to implement an "archived" feature and was considering using a view. However, Iā€™m open to other solutions. I was wondering if Drizzle ORM supports middlewares, as they could potentially simplify this process, but it seems they might not be available. If using views is the best approach, do you know when the query builder will support working with views? Thanks a lot
Want results from more Discord servers?
Add your server