getViewColumns function? (similar to getTableColumns)
Hello, in a situation where I need to join a view to a table. Here's the gist of my query code:
This works, however I'd like to be able to select only the view columns. For tables, we have the help of getTableColumns(), which is defined in the source code as:
I couldn't find an equivalent for views. So I tried writing my own
getViewColumns()
function. Here's my attempt:
Which is definitely incorrect, but I'm stuck. Any ideas on how one could write a working getViewColumns()
function? I'm connecting to a PostgreSQL database, and the view is an existing, regular (non-materialized) view, if it makes a difference.Goodies – DrizzleORM
Drizzle ORM | %s
GitHub
drizzle-orm/drizzle-orm/src/utils.ts at main · drizzle-team/drizzle...
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
Views (WIP) – DrizzleORM
Drizzle ORM | %s
6 Replies
Interesting, what is your driver?
It looks like in a view, they get the columns in this line: https://github.com/drizzle-team/drizzle-orm/blob/cfcdb823ae8e6a6c43e2e129868977ea56a7393a/drizzle-orm/src/pg-core/view.ts#L88
GitHub
drizzle-orm/drizzle-orm/src/pg-core/view.ts at cfcdb823ae8e6a6c43e2...
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
this.columns = getTableColumns(pgTable(name, columns));
You should be able to get the columns with:
I'm using the PostgresJS driver.
Interesting, so looking at view.ts closely, they're basically generating a new
pgTable
to get the columns for this.columns
. Was hoping to not have to list out all the columns already defined in the view. Maybe I'll consider submitting a feature request or PR for this.columns
to be made public readonly
.GitHub
drizzle-orm/drizzle-orm/src/pg-core/view.ts at main · drizzle-team/...
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
I discovered a work-around, in case anyone finds it useful:
I tried to translate this logic into a generic
getViewColumns()
function, but didn't have success.