getTableColumns for sub-query
I am very new to drizzle so I'm not sure the best way to do this.
I want to create a query, and then query from it and add some calculated fields. This is a rough example:
const first = db
.select({
...getTableColumns(recipe),
likeCount: sql<number>
(select count....
.as('likeCount'),
commentCount: sql<number>(select count....
.as('commentCount'),
})
.from(recipe)
.as('first');
const test = await db.select({
...first,
columnA: 'something...',
columnB: 'something else...'
}).from(first).limit(10);
The problem is I don't know how to add more columns in my second select (const test = await db.select...). There are columns from my first query I want to do some calculations on.
I am using Postgres.3 Replies
There's an open PR for this awaiting review
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.
You can upvote it to see if it gets merged soon