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:
await db.select().from(myView).innerJoin(myTable, eq(myTable.someId, myView.someId));
await db.select().from(myView).innerJoin(myTable, eq(myTable.someId, myView.someId));
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:
export function getTableColumns<T extends Table>(table: T): T['_']['columns'] {
return table[Table.Symbol.Columns];
}
export function getTableColumns<T extends Table>(table: T): T['_']['columns'] {
return table[Table.Symbol.Columns];
}
I couldn't find an equivalent for views. So I tried writing my own getViewColumns() function. Here's my attempt:
import { View } from 'drizzle-orm';

export function getViewColumns<V extends View>(view : V): V['_']['selectedFields'] {
return view[View.Symbol.SelectedFields]; // error on this line
}
import { View } from 'drizzle-orm';

export function getViewColumns<V extends View>(view : V): V['_']['selectedFields'] {
return view[View.Symbol.SelectedFields]; // error on this line
}
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.
6 Replies
Angelelz
Angelelz16mo ago
Interesting, what is your driver?
Angelelz
Angelelz16mo ago
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.
Angelelz
Angelelz16mo ago
this.columns = getTableColumns(pgTable(name, columns)); You should be able to get the columns with:
getTableColumns([your view name], [your view columns]);
getTableColumns([your view name], [your view columns]);
sietecero
sieteceroOP16mo ago
I'm using the PostgresJS driver.
sietecero
sieteceroOP16mo ago
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.
sietecero
sieteceroOP16mo ago
I discovered a work-around, in case anyone finds it useful:
import { ViewBaseConfig } from 'drizzle-orm'

const viewColumns = myView[ViewBaseConfig].selectedFields
await db.select(viewColumns).from(myView).innerJoin(myTable, eq(myTable.someId, myView.someId))
import { ViewBaseConfig } from 'drizzle-orm'

const viewColumns = myView[ViewBaseConfig].selectedFields
await db.select(viewColumns).from(myView).innerJoin(myTable, eq(myTable.someId, myView.someId))
I tried to translate this logic into a generic getViewColumns() function, but didn't have success.
Want results from more Discord servers?
Add your server