drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Dynamic selection without compromise
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Feature hasn't been suggested before.
- [X] I have verified this feature I'm about to request 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
```json
{
filter: // some filtering expression
sortBy: // some sorting expression
}
```
```json
{
items: // data array,
pagination: {
take: number,
skip: number,
total: // the total count of unpaginated items
}
}
```
This inevitably and systematically leads to 2 queries which are almost the same, but not exactly :
```ts
const items = await drizzle.
.with(/* some CTES */)
.select() // select * from all tables
.from(a)
.join(b, eq(a.id, b.a_id)
.where(where) // filtering
.limit(query.take) // pagination
.offset(query.skip) // pagination
.orderBy(...sortBy) // ordering
.execute()
const count = await drizzle.
.with(/* some CTES */)
.select({ count: count(a.id) }) // select the count
.from(a)
.join(b, eq(a.id, b.a_id)
.where(where) // filtering
// no pagination and ordering
.execute()
```
From there we can observe all of the :
- `with` CTEs
- `from`
- `join`
- `where`
are common to both queries
------
From 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 :
```ts
const _withSaasDirectoryAggregates = (drizzle: Drizzle) =>
drizzle.with(
withLicensesCount,
withExpensesCost,
withGroupsCount,
withAccessRequestsCount,
withMergedLicensesSources
)
```
- I factorized the `where` by just instanciating a `SQL` producing function
- I'm struggling at factorizing the `from` + `join` **with different `.select()`s**
-------
I'm coming from kysely where I was basically doing :
```ts
kysely
.with()
.from()
.join()
.limit()
.offset()
.where()
.select() // select at the very end
```
which were allowing insane modularity like so :
```ts
const commonQuery = kysely
.with()
.from()
.join()
.where()
const itemsQuery = commonQuery
.limit()
.offset()
.select(complexSelect)
const countQuery = commonQuery
.select(count)
```
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 the `select` at the end of the statement ?
Contributor guide
Assessment
This issue has not been assessed yet.