Map casing when using sql`` operator
If I query my table using the magic sql operator. How can I get back the column names like
userId
instad of how they are in the tabel user_id
?29 Replies
https://orm.drizzle.team/docs/sql#sqlmapwith probably this?
Magical sql operator 🪄 - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
sql`select user_id AS "userId" from users``;
@ericmartinezr So, I was looking at this, I'm not sure how to get it to work with all the columns, you know?
@Roberto Duran Okay, if that's what I need to do that's cool. I was just wondering if I could use
.as<T>()
or .mapWith()
instead
But the examples in the documentation only show how to use them with single column names. So I'm confusedfor your query I think the .as is a better option
You could build your request like this
sorry is the syntax is not right... Typing from my phone
Okay okay, no worries about the sytax. The high level approach is what I needed. Thanks!
Question. I you select like this:
Do you not get an object with
userId
?
That might just be a bug. Can you show the query you're trying to create?@Angelelz Here's what I have so far
I need to put a where clause on
Genres
that is joined to the GamesTable
by a join table
Since I couldn't really get that to work I decided to just write a raw query
But it's not converting the casing like drizzle usualy doesOh got you! Yeah, raw queries don't get mapped to the camel case
Yeahhhhh
But translating that query to drizzle syntax should be pretty simple
Oh really? I thought I couldn't do a search on the
Genres
via the inner join? That's where I got stuckThat query should be a one to one translation
I don't see anything weird.
What was the part you couldn't figure out?
Well, I haven't been able to figure out two things at this point
1. How to query games that have a genre with a certain id
2. How to automatically map the casing when writing a raw query
If I could do 1 I wouldn't need 2
But I can't figure out either really
I'm assuming several of the names, you'll have to check if that's correct
Okay on it
It's not loving the platforms syntax
Do I need to await that or something?
It also doesn't like the
PlatformsTable
Where are you importing
sql
from?import { sql } from 'drizzle-orm'
Weird, I'll check later today and get back with you. If you ignore the type errors, do you get the correct data back?
ohhhh
That's a good questioh
Let me try when i get off work
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
You'll see it if you hover over user
My guess is without noticing you imported
sql
from another libraryWhere is that
platforms
come from? How does drizzle understand the relationship?
Things are looking better, I'm just not sure about that platforms
definition, here is what I have so far
But I'm getting the following error
column "platforms" does not exist at evalAny ideas?
It's your own table, I just translated from your own query
Well yeah but like, the GamesTable doesn't have a platforms column on it. The platforms are coming from a join table
Well, in your example query it doesn't seem to care about your key at all
You just had
something
hmmmm
i wonder why my query would be blowing up on thatI was under the implession that platforms was a table, not a column
I used a table in my example, I'm not even sure that a correct query lol
I was just following your query
So, the
PlatformsTable
is but I'm just not sure about the key platforms:
in your select
well i was thinking that the only reference to platforms
was the key but that's dumb, the actual table is platforms
So why would that table not exist
But why is it looking at platforms
as a column I guess is my question?
like, it should just be creating that column on the query
okay okay okay, I think we've got it!
Well that was a trip, thanks so much for your help!Good deal