mapWith, any special things to make it work ?
I can't seem to have mapWith() work - I am logging inside the function and it nvever gets called.
I am doing some sql-fu and want to return a collection that is declared in drizzle, and would like to have it mapped to my js object.
I tryed on sql``.mapWith(), sql.join().mapWith() without any success.
Also I was wondering if there was a way to pass the drizzle table object somehow so that the return of the sql gets mapped with the default mapping of this table, not having to declare a mapper again ?
the doc is not super clear on this front.
26 Replies
Let's do it over here, can you share the code that doesn't work for you?
let me first discribe the use case. I am doing a complicated sql query that looks like this:
this query returns a list of customerPlan objects
however those are object with keys set to the database column names, which are un snake_case
when declaring the drizzle model, we used snake_case for the db, and camelCase in js side
What i am looking to get is a regular customerPlan with camelCase names
You don't need to do the whole thing in sql
Just the complicated parts
Like this:
I might have errors, cause I haven't tested it, but that'll get you in the right direction
ok. What about another use cas - we do a complex update with a temp table and want to return the updated lines
this kinda have to be in sql, and we use the returning statement
I gues smapWith is not used for that
BTW, the mapWith method only works at the column level, for
externalReferences
column for example
Not for the row or query levelok
No, you can make it happen the same way
there is no way to leveragle drizzle internal mapping when returning a sql query that we know are of a certain type ?
I believe you should be able to do subqueries and/or ctes in updates
Yes there is
But you have to do it by column
you mean call mapWith repeatedly ?
I mean, use mapWith on each column you need it
so if I retrun 5 cols, I can chain the mapWith calls, one for each col ?
You are thinking too bare metal
mapWith won't work in almost plain sql
I take this as a compliment 😄
any other trick than mapWith then ?
You are supposed to use db.select() to use mapWith
the doc does not mention that, hence my confusion
Oh, wait...
You have an update from query
Let me check that
yes
Lol, here I am saying that you can do it and there's an open issue: https://github.com/drizzle-team/drizzle-orm/issues/208
a ok - so for this query I won't be able to use the query builder right ?
Unfortunately no
so no way to map the returning statement to js object ?
The problem is the from clause
No, you have to do it manually
I was hoping I could access the mapping function from the model object but ok
i'll wait for the update from to be implemented 😄
thnaks for the great work, and the support