Skip to content

Commit

Permalink
feat: pickTableColumns utility function and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Jan 9, 2025
1 parent 04c9143 commit 8bb827a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit 8bb827a

Please sign in to comment.