-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
[FEATURE]: pick columns from getTableColumns
#3034
Labels
Comments
Could do somthing simular to the query syntax maybe? So you could reuse them. await db.select(getTableColumns(users, { id: true, name: true })).from(users); |
L-Mario564
added
improvement
qb/crud
and removed
enhancement
New feature or request
labels
Oct 24, 2024
I made a simple util function that works for me: import type { Table} from "drizzle-orm";
export const getFilteredTableColumns = <
T extends Table,
K extends keyof T["_"]["columns"],
>(
table: T,
includedColumns: Record<K, true>,
): Pick<T["_"]["columns"], K> => {
const allColumns = getTableColumns(table);
const result = {} as Pick<T["_"]["columns"], K>;
for (const key in includedColumns) {
if (includedColumns[key]) {
result[key] = allColumns[key];
}
}
return result;
}; I am not a typescript wizard, but I have tested it with the drizzle selects and it should infere everything: const users = table(
"users",
{
id: uuid().primaryKey().defaultRandom(),
firstName: text(),
lastName: text(),
},
(table) => [index().on(table.clerkUserId)],
);
const test = () => {
return db
.select(getFilteredTableColumns(users, { lastName: true }))
.from(users);
};
const result = await test();
/**
Return type:
const result: {
lastName: string | null;
}[]
**/ |
@lasseklovstad pr sent, I included you in the commit. For your information. I played a little bit in the code. |
Nice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe what you want
Current
getTableColumns()
does not support partial select.You need to destructure and leave unused variables behind:
https://orm.drizzle.team/docs/goodies#get-table-information
I hope
pickTableColumns
is provided by Drizzle for better DX.This is the source code I am using:
The text was updated successfully, but these errors were encountered: