Skip to content
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

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from

Conversation

Angelelz
Copy link
Collaborator

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.

@emspaced
Copy link

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

@Angelelz
Copy link
Collaborator Author

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

@emspaced
Copy link

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

@pencilcheck
Copy link

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.

@AndriiSherman AndriiSherman changed the base branch from main to beta March 9, 2024 16:20
@jean343
Copy link

jean343 commented Apr 4, 2024

Can we please get this merged in?

@emspaced
Copy link

emspaced commented Apr 4, 2024

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

@NurbekGithub
Copy link

@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

@hauserkristof
Copy link

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

@etx121
Copy link

etx121 commented Oct 2, 2024

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

@zoriya
Copy link

zoriya commented Nov 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE]: Add getSubqueryFields helper to goodies
9 participants