#How GORM's First() method works under the hood?
48 messages · Page 1 of 1 (latest)
lots of reflection
I've attempted to do so but I'm stuck where I need to assign different values such as a slice of int.
use that to build an SQL query and update fields based on column names, struct field names and tags
does it hurt the performance?
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
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?
give up on gorm, wrote SQL yourself and simplify your code and reduce magic
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.
not sure what you mean by that
Yes, it does but sometimes it's needed, see: json
If you can replace it with interfaces to get/set values or with generics, then i would try that first instead of that, tbh
Assuming you're doing something else than just messing with gorm, correct me if im wrong tho
I'm not sure how generics are relevant in a dynamic query building and marshalling context
yea i don't think can use generics for this subject
Just felt they wanted to know how it worked or/and how to do similar things, rather than actually doing something with query building, sorry
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.
no worries. just as a general note, backseating is generally not helpful for the person receiving help or the other person helping, I know you're trying to help but it can also be a hindrance
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
Also, I was thinking if can return the result in map[string]any but the maps are not goroutine safe... 🤔
you can still provide a struct for it to unmarshal the resulting rows to a structnwirh something like jmoiron/sqlx
That's true. we must invest and share knowledge with the team. but the current team is a group of freelancers who change from time to time. So teaching them is time-consuming.
the other problem was when we let them write their queries. So our time was spent checking and testing their queries.
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
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
I'm on your team. but freelancer don't care 😄
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
btw, i'm checking the sqlx. apparently, we need to write the query ourselves?
yes. if you intend to use a tool which doesn't, you can throw performance out of the window
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..
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
okay. we can't change DB now. but any suggestion except mongodb?
nosql?
yes
are you using postgres
mainly
just use a json column tbh
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
you can do it with postgres. just store raw bytes. a []byte
that's true but the reading speed of mongodb was more than psql
if that's all you care about look at something like boltdb then
not sure how they measured it but that was the numbers i read
that's archived