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: pickTableColumns utility function and update documentation #3921

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/table-introspect-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ const table = pgTable('table', {

const columns/*: { id: ..., name: ... } */ = getTableColumns(table);
```

```ts
import { pgTable, pickTableColumns } from 'drizzle-orm/pg-core';

const table = pgTable('table', {
id: integer('id').primaryKey(),
name: text('name'),
});

const colums = pickTableColumns(table, {
id: true,
name: false,
});
```
10 changes: 10 additions & 0 deletions drizzle-orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ export function getTableColumns<T extends Table>(table: T): T['_']['columns'] {
return table[Table.Symbol.Columns];
}

export function pickTableColumns<T extends Table>(
table: T,
includedColumns: Partial<Record<keyof T['_']['columns'], true>>
): Pick<T['_']['columns'], keyof typeof includedColumns> {
const allColumns = table[Table.Symbol.Columns];
return Object.fromEntries(
Object.entries(allColumns).filter(([key]) => key in includedColumns)
) as any;
}

export function getViewSelectedFields<T extends View>(view: T): T['_']['selectedFields'] {
return view[ViewBaseConfig].selectedFields;
}
Expand Down
37 changes: 37 additions & 0 deletions integration-tests/tests/utils/table-columns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from 'vitest';
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { getTableColumns, pickTableColumns } from 'drizzle-orm';

describe('table columns utils', () => {
const users = sqliteTable('users', {
id: integer('id').primaryKey(),
name: text('name'),
email: text('email'),
});

test('getTableColumns should return all columns', () => {
const columns = getTableColumns(users);

expect(Object.keys(columns)).toEqual(['id', 'name', 'email']);
expect(columns.id.name).toBe('id');
expect(columns.name.name).toBe('name');
expect(columns.email.name).toBe('email');
});

test('pickTableColumns should return only selected columns', () => {
const columns = pickTableColumns(users, {
id: true,
name: true
});

expect(Object.keys(columns)).toEqual(['id', 'name']);
expect(columns.id.name).toBe('id');
expect(columns.name.name).toBe('name');
expect(columns.email).toBeUndefined();
});

test('pickTableColumns should return empty object if no columns selected', () => {
const columns = pickTableColumns(users, {});
expect(Object.keys(columns)).toEqual([]);
});
});