Is there a way to modify the select() on an existing query ?

Let's say I have this query that I pass to a function
const query = db.select().from(myTable)
const query = db.select().from(myTable)
in my function, I'd like to clone the query, and modify its select() clause to something like this:
.select({
totalCount: sql<number>`COUNT(*)`,
})
.select({
totalCount: sql<number>`COUNT(*)`,
})
Is this possible? My current workaround is to inspect the config property on my query to create a new one, but I know that I shouldn't mess with internals:
db.select({
totalCount: sql<number>`COUNT(*)`,
})
// @ts-ignore since config is protected
.from(query.config.table)
db.select({
totalCount: sql<number>`COUNT(*)`,
})
// @ts-ignore since config is protected
.from(query.config.table)
4 Replies
machour
machourOP2y ago
I feel like the answer to my question is no, since the initial .select().from() is crucial for Drizzle typing.. In that case, would changing config visibility something that could be considered? My use case is for a data provider function, where I pass a query, and it gives me back paginated results (adding limit().offset()) and and the global pagination info
Dan
Dan2y ago
Yes, exposing the config might be a good idea Will add to backlog
machour
machourOP2y ago
I also needed the SelectResult type, but it seems to only be defined in generated files under nodemodules/drizzle-orm/ :/ generating an additional generated.ts file re-exporting from the generated files would also help I guess And I also needed to tap into `query..selectedFields` 😁
Want results from more Discord servers?
Add your server