drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: add "blacklist" parameter to getTableColumns

Open
#1,673 0 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

For the function `getTableColumns`, I think adding an optional parameter called "blacklist" or "exclude" that accepts an array of columns to be excluded from the result of getTableColumns can be useful.. specially when we do projections on a table with so many columns and we want to exclude 1 or 2 columns from them (for example: sensitive columns)

I've already implemented a hack for postgres but an internal implementation would be be much simpler, more general, and less buggy than:
```
import { PgColumn, PgTableWithColumns, TableConfig } from 'drizzle-orm/pg-core';
import _ from 'underscore';

export const tableProjections = (
pgTable: PgTableWithColumns,
blacklist: PgColumn[]
): PgProjection, PgNonColumn> => {
return _.omit(pgTable, [
...blacklist.map(x => x.name),
'_',
'getSQL',
'$inferSelect',
'$inferInsert'
]) as PgProjection, PgNonColumn>;
};

```
(update: I found that the the previous snippet doesn't work as expected because `x.name` returns the column name in the database not the ORM alias.
I was trying to implement it and make a PR.. but I didn't have a clue how to test the implementation and didn't want to waste more time on this, here is what I got :
```
export function getTableColumns2(table: T, blacklist: (keyof T['_']['columns'])[] = []
): T['_']['columns'] {
const allColumns = table[Table.Symbol.Columns];
const filteredColumns: T['_']['columns'] = Object.fromEntries(Object.entries(allColumns)
.filter(([key,_value])=>!blacklist.includes(key as keyof T['_']['columns'])))
return filteredColumns;
}
```
Is that code even right? not sure tbh XD

Contributor guide

Open the contributing guide

Research direction

Start at the getTableColumns entry point and trace how its returned column map is typed and used for projections. Check existing tests for this helper, then add coverage showing that an optional blacklist excludes ORM column keys while the default still returns all columns; done means the behavior works across the supported table dialects.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
database
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.