Get subquery columns in a way similar to `getTableColumns`

Keeping track manually of all subquery fields can quickly become annoying, and I'm looking for a simple helper to allow specifying fields with a pattern similar to what getTableColumns enables. Has anyone implemented a small helper that's somewhat "battle-tested"? Here's what I have at the moment, but I'm not trusting it that much due to how naive it is and due to the necessary typecasting.
export function getSubqueryColumns<S extends ColumnsSelection, A extends string>(
query: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
): (typeof query)['_']['selectedFields'] {
return query[SubqueryConfig as unknown as string].selection;
}
export function getSubqueryColumns<S extends ColumnsSelection, A extends string>(
query: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
): (typeof query)['_']['selectedFields'] {
return query[SubqueryConfig as unknown as string].selection;
}
8 Replies
Angelelz
Angelelz14mo ago
interesting What is SubqueryConfig? I see, you imported it from drizzle. That's smart. This should work no problem. But could you please add a issue for this in GH? to add this helper function to drizzle: getSubqueryColumns
iolyd
iolydOP14mo ago
GitHub
[FEATURE]: Add getSubqueryFields helper to goodies · Issue #1459 ...
Describe what you want The getTableColumns helper is really useful, but an equivalent lacks for subqueries. This can easily be implemented in userland, although it feels like the kind of helper tha...
Angelelz
Angelelz14mo ago
Thank you, this could be a pretty easy PR
rphlmr ⚡
rphlmr ⚡12mo ago
I have this one too. Does the same :p
function getSubQueryColumns<S extends ColumnsSelection, A extends string>(
subQuery: SubqueryWithSelection<S, A> | WithSubqueryWithSelection<S, A>,
) {
const { selection } = (subQuery as any)[SubqueryConfig] as {
selection: (typeof subQuery)["_"]["selectedFields"];
};

return selection;
}
function getSubQueryColumns<S extends ColumnsSelection, A extends string>(
subQuery: SubqueryWithSelection<S, A> | WithSubqueryWithSelection<S, A>,
) {
const { selection } = (subQuery as any)[SubqueryConfig] as {
selection: (typeof subQuery)["_"]["selectedFields"];
};

return selection;
}
iolyd
iolydOP12mo ago
Personally my vote goes to striving for making things less specific and providing a single helper. This PR should answer most use case: https://github.com/drizzle-team/drizzle-orm/pull/1789
GitHub
Feat: add getColumns function to utils by Angelelz · Pull Request...
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.
rphlmr ⚡
rphlmr ⚡12mo ago
Sure, I've dropped a like on the PR!
Angelelz
Angelelz12mo ago
Hey @iolyd, I've seen that you've played with this. I haven't have time to add anything else to this PR. Do we still need to add the type for a subquery without the alias?
iolyd
iolydOP12mo ago
Good question, I personally didn't end up having cases where I had to use it, but I still think it could be useful for others.
Want results from more Discord servers?
Add your server