#How GORM's First() method works under the hood?

48 messages · Page 1 of 1 (latest)

winter delta
#

I see from the docs that the First() method accepts a parameter with interface{} type, which is for example &User struct.
But how this actually assigning the values to the given interface while don't have any access to the actual struct(type User struct{}) ?

molten rover
#

lots of reflection

winter delta
#

I've attempted to do so but I'm stuck where I need to assign different values such as a slice of int.

molten rover
#

use that to build an SQL query and update fields based on column names, struct field names and tags

winter delta
molten rover
#

to a significant extent, less because of reflection, even though it slows things down, but also since gorm creates very poor quality queries amongst other things

winter delta
#

okay, is there any way around this approach? e.g we want to return values without having access to the actual struct.
Is there any way to copy or register a struct with the third-party package?

molten rover
#

give up on gorm, wrote SQL yourself and simplify your code and reduce magic

winter delta
#

actually i need to write a package to force the developer to reuse the query. the reason is to avoid their mistake or poor quality query writing.

molten rover
#

not sure what you mean by that

viral isle
#

Assuming you're doing something else than just messing with gorm, correct me if im wrong tho

molten rover
#

I'm not sure how generics are relevant in a dynamic query building and marshalling context

winter delta
#

yea i don't think can use generics for this subject

viral isle
winter delta
# molten rover not sure what you mean by that

i mean the current problem is that most of the developers we get, don't have enough knowledge of the database query. So must tell them for example, if you wanna get relational data from tables, instead of writing your join query, use functions A & B instead.

molten rover
molten rover
# winter delta i mean the current problem is that most of the developers we get, don't have eno...

I understand: but keep in mind that SQL is the language for db communication. abstracting is fine, but masking away like this and using seperate queries can really impact performance negatively, especially in cases where you are retrieving 100 rows. and getting into from another table for each entry. that could potentially be an extra 100 queries where 2 would otherwise be sufficient. getting down the basics of SQL is an investment worth a few days at most but the resulting quality and performance of your code and queries will be worth the headache you will have. it's an investment that will pay dividends for the entirety of your backend development career

winter delta
#

Also, I was thinking if can return the result in map[string]any but the maps are not goroutine safe... 🤔

molten rover
#

you can still provide a struct for it to unmarshal the resulting rows to a structnwirh something like jmoiron/sqlx

winter delta
#

the other problem was when we let them write their queries. So our time was spent checking and testing their queries.

molten rover
#

I understand. it's unfortunate. but I like to think of SQL at the same level of importance as knowing how http works, e.g. what cookies and headers are and how json works

winter delta
#

That's why we look for ORM and then we found out that due to that "magic" stuff, the performance dropped. So we need to create a lightweight internal orm for our need only

winter delta
molten rover
#

you probably want to look into something like sqlc. you write queries in one place and generate the code. that way you can make it possible for fewer people to potentially need to handle writing the sql and you can just focus on writing the code to use the queries

#

writing your own in house orm is much more painful than you'd expect. they just don't mesh great with go

winter delta
#

btw, i'm checking the sqlx. apparently, we need to write the query ourselves?

molten rover
#

yes. if you intend to use a tool which doesn't, you can throw performance out of the window

winter delta
#

well, the performance is important to us too.
But, what if, some how we send the structure over and rebuild it and use it? I mean just thinking loud...

#

like how we send and receive JSON using marshal/unmarshal to struct..

molten rover
#

then you might want a kv database/ nosql

#

disadvantages there too, but main advantage is you don't have to use SQL. (just don't use mongodb)

#

if you describe the scale of your app and what you're doing I can give you some suggestions

winter delta
molten rover
#

nosql?

winter delta
#

yes

molten rover
#

are you using postgres

winter delta
#

mainly

molten rover
#

just use a json column tbh

winter delta
#

some files such as audio file need to be encrypted and store which is going to be hard to use psql

#

but last time we used mongodb for that

molten rover
#

you can do it with postgres. just store raw bytes. a []byte

winter delta
#

that's true but the reading speed of mongodb was more than psql

molten rover
#

if that's all you care about look at something like boltdb then

winter delta
#

not sure how they measured it but that was the numbers i read

molten rover