sietecero
sietecero
DTDrizzle Team
Created by sietecero on 9/6/2023 in #help
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.
8 replies