Feat: add `getColumns` function to utils
close #1459
- Added new
getColumnsfunction that acceptsTable | View | Subqueryand returns the columns (selected fields). - Added deprecation note to
getTableColumns. - Added tests for all 3 dialects.
Dont know if this is an appropriate use, but I think this util could also handle select fields retrieval:
/**
* Dialect-agnostic select
*/
type AnySelect = AnyPgSelect | AnyMySqlSelect | AnySQLiteSelect;
export function getColumns<T extends Table | View | Subquery | AnySelect>(
table: T
): T extends Table
? T['_']['columns']
: T extends View
? T['_']['selectedFields']
: T extends Subquery
? T['_']['selectedFields']
: T extends AnySelect
? T['_']['selectedFields']
: never {
return is(table, Table)
? table[Table.Symbol.Columns]
: is(table, View)
? table[ViewBaseConfig].selectedFields
: is(table, Subquery)
? table[SubqueryConfig].selection
: table._.selectedFields;
}
I think this is a good idea. I'll see if I can include this case in this PR.
After playing around, I realized AnySelect should probably be loosened to Omit<AnySelect, 'where'> else filtered selects are not handled.
This will be very very useful for the use case i'm working on where i am joining multiple tables but want to only select all fields from one table only.
Can we please get this merged in?
@jean343, as it was taking too much time to merge, I published a small package with this helper (along with a variety of others).
Feel free to use it or refer to its implementation of getColumns: https://github.com/iolyd/drizzle-orm-helpers/blob/main/src/utilities.ts.
@jean343, as it was taking too much time to merge, I published a small package with this helper (along with a variety of others).
Feel free to use it or refer to its implementation of
getColumns: https://github.com/iolyd/drizzle-orm-helpers/blob/main/src/utilities.ts.
You are a livesaver sir
Will this ever be merged? It would be so helpful!
@Angelelz will getColumns accept columns from a subquery of type SubqueryWithSelection | WithSubqueryWithSelection. I am trying to find a function which can get the columns of some subquery I make in CTEs.
For those that want this without an external lib or waiting for it to be updated and merged:
import {
is,
type ColumnsSelection,
type Subquery,
Table,
View,
ViewBaseConfig,
} from "drizzle-orm";
import type { AnyMySqlSelect } from "drizzle-orm/mysql-core";
import type { AnyPgSelect } from "drizzle-orm/pg-core";
import type { AnySQLiteSelect } from "drizzle-orm/sqlite-core";
import type { WithSubquery } from "drizzle-orm/subquery";
// https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts#L58
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
// See https://github.com/drizzle-team/drizzle-orm/pull/1789
type Select = AnyPgSelect | AnyMySqlSelect | AnySQLiteSelect;
type AnySelect = Simplify<Omit<Select, "where"> & Partial<Pick<Select, "where">>>;
export function getColumns<
T extends
| Table
| View
| Subquery<string, ColumnsSelection>
| WithSubquery<string, ColumnsSelection>
| AnySelect,
>(
table: T,
): T extends Table
? T["_"]["columns"]
: T extends View | Subquery | WithSubquery | AnySelect
? T["_"]["selectedFields"]
: never {
return is(table, Table)
? (table as any)[(Table as any).Symbol.Columns]
: is(table, View)
? (table as any)[ViewBaseConfig].selectedFields
: table._.selectedFields;
}
@wederchr getColumns from https://github.com/emmnull/drizzle-orm-helpers might help (a lot) 😉