Open cyrilchapon opened 1 week ago
I found a way, which sounds more like a workaround — given the very complex syntax — but that works :
// Common CTEs
const _withs = (drizzle: Drizzle) =>
drizzle.with(
cte1,
cte2,
)
// Common joins
export const _joins = <T extends MySqlSelect>(qb: T) => {
return qb
.innerJoin(table2, eq(table1.id, table2.table1Id))
.leftJoin(table3, eq(table2.id, table3.table2Id))
}
// Select builders
export const _itemsSelectBuilder = (drizzle: Drizzle) =>
_withs(drizzle).select()
export const _countSelectBuilder = (drizzle: Drizzle) =>
_withs(drizzle).select({
count: count(table1.id),
})
// Query builders
export const queryItems = (drizzle: Drizzle) =>
_joins(
_itemsSelectBuilder(drizzle)
.from(table1)
.$dynamic()
)
export const queryExtendedSaasDirectoryCount = (drizzle: Drizzle) =>
_joins(
_countSelectBuilder(drizzle)
.from(table1)
.$dynamic()
)
Feature hasn't been suggested before.
(closed one, and related ones but not that complete)
Describe the enhancement you want to request
Hey,
This is a reopening of dynamic selection topic, with a pretty decent use-case I believe.
I'm making yet another one full featured REST API, which always take the same query parameters
This inevitably and systematically leads to 2 queries which are almost the same, but not exactly :
From there we can observe all of the :
with
CTEsfrom
join
where
are common to both queriesFrom many attempts, and readings (#948, #2954, #1817), I'm struggling at factorizing all the common parts.
More specifically :
I managed to factorize the
.with
CTEs basically like so :I factorized the
where
by just instanciating aSQL<bool>
producing functionI'm struggling at factorizing the
from
+join
with different.select()
sI'm coming from kysely where I was basically doing :
which were allowing insane modularity like so :
Is there any known strategy to achieve the same thing with Drizzle, or any consideration to be able to build a dynamic
select
or even moving theselect
at the end of the statement ?