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>
commentCount: sql<number>
})
.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.
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.