Open frederikhors opened 3 years ago
No, that is not supported. I will accept a PR that contributes something like that, but I personally don't find such feature useful so don't expect it from me :)
I will accept a PR that contributes something like that
I wish I was able to do it!
I understand that you see this problem with this perspective, this is not a criticism.
I just want you to understand how maybe another person (me) sees it.
The convenience of writing a model composed of has-many
or has-one
and being able to save and update it automatically is priceless to me!
Something like:
type Player struct {
Name string
Games []Game
}
type Game struct {
World string
Score int
}
with this command
_, err := db.NewInsert().Model(player).Exec(ctx)
which generates one SQL statement both for Player
and all it's Games
. The same for UPDATE
and DELETE
.
This convenience is invaluable (especially if there are many tables related).
But I understand that it is not in your priorities and I RESPECT your opinion!
Maybe someone will help us sooner or later!
Thank you!
Such feature requires you to structure API in a certain way. Specifically, you need to add Player.Games
field and make sure to populate it properly (accept data in a certain way). That is the additional code you need to write in order to use cascading inserts.
But eventually you will want to customize the insert logic, for example, use insert-or-update. Or cache. Or add some validation. Bun can't support any of that.
So you will throw away cascading inserts and all the efforts you've made to support them. And replace it with 20-30 lines of code that just calls a couple of inserts. The last thing to do is to write a blog post / comment saying "ORMs are evil".
PS Another popular alternative is to fix the problem with hooks. I guess we will discuss that in hooks-related thread :)
So you will throw away cascading inserts and all the efforts you've made to support them. And replace it with 20-30 lines of code that just calls a couple of inserts. The last thing to do is to write a blog post / comment saying "ORMs are evil".
LOLOLOLOLOLOLOLOL!!!!!!
You got me!
PS Another popular alternative is to fix the problem with hooks.
I'm curious, what do you mean?
I'm curious, what do you mean?
You could try to implement insert-or-update/cache/validation using BeforeInsert/AfterInsert
model hook. Assuming that hooks are powerful enough.
You could try to implement insert-or-update/cache/validation using
BeforeInsert/AfterInsert
model hook. Assuming that hooks are powerful enough.
How would you access the bun.DB
instance from within a hook function to create a new query though?
Is it possible with Bun to insert a
Team
with it'shas-many
Players
using a single insert?Example:
Is this possible?
Something like https://github.com/go-pg/pg/issues/478.