drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

Feat: add `getColumns` function to utils

Open Angelelz opened this issue 1 year ago • 9 comments

close #1459

  • Added new getColumns function that accepts Table | View | Subquery and returns the columns (selected fields).
  • Added deprecation note to getTableColumns.
  • Added tests for all 3 dialects.

Angelelz avatar Jan 13 '24 01:01 Angelelz

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;
}

emmnull avatar Jan 21 '24 18:01 emmnull

I think this is a good idea. I'll see if I can include this case in this PR.

Angelelz avatar Jan 22 '24 17:01 Angelelz

After playing around, I realized AnySelect should probably be loosened to Omit<AnySelect, 'where'> else filtered selects are not handled.

emmnull avatar Jan 22 '24 19:01 emmnull

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.

pencilcheck avatar Feb 02 '24 04:02 pencilcheck

Can we please get this merged in?

jean343 avatar Apr 04 '24 17:04 jean343

@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.

emmnull avatar Apr 04 '24 17:04 emmnull

@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

NurbekGithub avatar Apr 15 '24 08:04 NurbekGithub

Will this ever be merged? It would be so helpful!

hauserkristof avatar Jun 28 '24 08:06 hauserkristof

@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.

etx121 avatar Oct 02 '24 13:10 etx121

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;
}

zoriya avatar Nov 08 '24 21:11 zoriya

@wederchr getColumns from https://github.com/emmnull/drizzle-orm-helpers might help (a lot) 😉

etx121 avatar May 16 '25 15:05 etx121