Open loicleray opened 2 weeks ago
This issue perfectly outlines what I have been looking for as well. I have been messing with a few ORM's and ergonomically transforming relational queries into a nested presentation seems to be something they all struggle with. Dirzzle ORM is already really good with that. Adding this flatwith
functionality would be the cherry on top.
Totally agree with this, I had a hard time to understand what was going on with relations in general for many-to-many. Some more documentation would be very helpful. Especially since there is no Infer functions in place yet for queries with relations.
Describe what you want
Overview / Explanation
Default aggregation behaviour of Drizzle Queries is to provide nested response objects with the table name of the relation as the
key
, and an object of the information it contains as it'svalue
.This makes sense when a project is simple, but is quite cumbersome when dealing with many relations or complex schemas; especially where a schema has many-to-many relations (via join tables). Accessing the data from deeply nested and repeated objects is inefficient / unintuitive.
One-to-one, one-to-many relationships are easier to follow with simple schemas - but messy to access when there are a lot of columns in a table.
Responses can be modified with custom functions that remap responses into the desired shape. Though requires getting more data than needed from the database, then reducing it with JS. That process defeats the stated purpose of drizzle queries:
Alternatively, you could just provide raw SQL and call it a day. But accessing aggregated related data in a flattened response is a common enough process to warrant a quality of life way of doing it...
This issue has been mentioned previously (here, here, here, here). Purpose of raising this issue is to have a definitive response from the drizzle team and provide work around solutions for those experiencing similar frustrations.
Example Schemas
Note using the drizzle postgres adapter in the examples below. Minor modifications necessary if you're using SQLite, etc.
Build Schemas
Define Schema Relations
Instantiate your db
Ways of fetching "posts" data and shapes of responses
Option 1 - Default nested querying
Explanation: Least complex method, least intuitive result.
It is annoying to access items in response in this shape.
userId
andauthor
refer to the sameuser
, effectively duplicating content references despite; one-to-many (one author to many posts).Similar is true for many-to-many via a join table. The
categoryTags
is an array of objects which contain apostId
,categoryTagId
, thecategoryTag
key and its object value.Option 2
Explanation: Slightly reduce duplicates with a more complex query.
userId
andauthor
still have duplicate content, but thecategoryTags
is an array of objects whichcategoryTag
key and the object itself. There is no longer apostId
,categoryTagId
in each of thecategoryTags
object.Still annoying to access content in a response of this shape, but reduces references to the join table (removes
postId
andcategoryTagId
).Option 3
Explanation: Most intuitive response, most complex
fetchFunction()
which transforms database response and reshapes it with javascript = which is less efficient than getting data from a single database call.The
categoryTags
is an array ofcategoryTag
objects. There is no longer nested and duplicated references to thecategoryTag
objects.userId
andauthor
still have duplicate content, but could also be fixed with the transform.Far less annoying to access content in a response of this shape, but less efficient than if response was to be collected from SQL directly. Javascript is reshaping data after getting it from the database.
Feature request to fix behaviour.
Suggesting a feature to be able to override the default behavior of drizzle queries that access related tables to avoid duplicate references to the same object. Solutions would provide a "flattened" object.
Using the example schemas above, the ideal response would have the following shape:
Drizzle team would be better suited to answering this, but some API modification to get data in the shape above would be awesome - ideally straight from the database in one step.
I may be overlooking existing solutions or misunderstanding some aspects of Drizzle ORM. If I am mistaken or you have a cleaner approach to this issue, I'd be incredibly grateful if you shared it.
Thank you for your hard work, Drizzle Team!