-
-
Notifications
You must be signed in to change notification settings - Fork 766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: add getColumns
function to utils
#1789
base: beta
Are you sure you want to change the base?
Conversation
…Subquery` and returns the columns
Dont know if this is an appropriate use, but I think this util could also handle /**
* 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 |
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 |
You are a livesaver sir |
Will this ever be merged? It would be so helpful! |
@Angelelz will |
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;
} |
close #1459
getColumns
function that acceptsTable | View | Subquery
and returns the columns (selected fields).getTableColumns
.