Access values in "where" query
Hey, so I wan't to do some calculations, specifically geo radius. How can I access the data while doing the query?
Or how would I achieve this?
40 Replies
the long and lat are just some object without the actual value
The apartment object is your table and lat is a column. It's just to help you create the query. They don't hold any values at runtime
How would I achieve doing math in my queries?
To do what you want, you need to retrieve the data from the database if I understood your problem correctly
Or, do the math in SQL
What math are you trying to do?
I mean this would make me have to get all data
I just want to find the points that are in a radius of another point
Do you know how to construct that query in SQL only?
I believe I do but this makes me question it
Let me see, then you can construct that query using typesafe drizzle syntax
Ight give me a few
@Angelelz Allright, so sorry for the horrible sql but this works
This finds all the apartments in the radius of 10km (might be a little wrong math)
Lol you're almost there. Now, I think that some DBMSs have functions for stuff like this. Have you checked that?
I think supa base has some kind of way to do geo
but I was kinda curious if drizzle could do this
@Angelelz The return checks if lat is
lte
to the distance, though I'd like to check if the distance is lte
the given radius
The problem I have is that the lte
function requires a column
would be perfect if I could just pass the radius
Like that then
This seems to work as well
Not sure how safe that is for injection
@Angelelz oh, just realized it was the exact same
so yeah, like that
thanks a lot dude !
@AngelelzFor the final question, if I would like to also include the calculation in my result, normally I'd just select that as well in sql but that won't get me the type
like output
in sql
Tried the extra thing, but no clue how to reuse it
You have to change the calculation function to what it was before.
But I'm thinking you'll need to use db.select for this:
ahh I see, calculation column in the result will remain unknown though
It won't, that's why I used mapWith
It'll be a number
ah I missed that
@Angelelz did your last sql statement actually work?
mine says "calculation" doesnt exist
No, I'm not trying it
fair
Can you show the sql that was generated?
Where can I see that?
If you pass
logger: true
to the drizzle function when instantiating it
It will log all your queries to the console@Angelelz hmm I passed the logger: true but it doesn't do much
Can I see how you're doing it?
sure thing
You're running the query with logger true and it's not being logged?
correct
I do the query with this
Can you copy the error message from the console?
I got the query now
Heres the error:
Keep in mind I actually called the column distance and so on
The query that was built looks right
Try this:
gives this query, same issue though
Look at the calculation function
You missed the as distance
If not try sql`"distance" < 10
same thing
it just adds the as calculation at the end
which is the same as the very first try
Guess I'll have to look into $with
if interested i ended up with this
i mean not perfect but I'll take it
That works!
That's even better IMO
does (sqlite) drizzle have a built in way to atomic increment a field during an update? I'm doing this:
but wondering if there was a built in function
This is the way
can we use prepared statements in batches? I'm trying with:
but getting `Property 'returning' does not exist on type 'PreparedQuery<...' errors