Comments (7)
Why I must use query.GetModel() all time instead use self object?
Because Bun also works with slice-based models, e.g. *[]User
. And in some cases it might be useful to have access to a whole slice.
Overall it is best to only use hooks as a last resort when everything else fails. In your case you should create InsertUser
func and have everything there.
from bun.
Also see https://bun.uptrace.dev/guide/hooks.html#disclaimer
from bun.
IDB
was added in v0.2.1. See https://github.com/uptrace/bun/tree/master/example/tx-composition
from bun.
Overall it is best to only use hooks as a last resort when everything else fails. In your case you should create
InsertUser
func and have everything there.
If I will create function Insert User
and for other operations like this, then I didn't see some profit from model hooks overall. ) May be model hooks in this lib must not to be? :)
from bun.
Another case...
I have models User
and Profile
.
One logic need to create user directly, other need create user and profile inside transaction. In this case I must realize InsertUser(bun.DB), InsertUserTx(bun.Tx), InsertProfile(bun.DB), InsertProfileTx(bun.Tx), ... a lot of code ... In old variant of hooks all work perfectly.
from bun.
Better example for https://bun.uptrace.dev/guide/hooks.html#disclaimer
func UpdateUser(ctx context.Context, query bun.UpdateQuery, user *User) error { ... }
func InsertUser(ctx context.Context, query bun.InsertQuery, user *User) error { ... }
from bun.
I am not sure sure how you managed transactions with hooks, but that does not sound right :)
A better solution would be to create a common interface for bun.DB
and bun.Tx
. They both have methods like NewSelect
and NewInsert
so you can pass a tx instead of db:
tx := db.Begin()
InsertUser(ctx, tx)
InsertProfile(ctx, tx)
tx.Commit()
I guess Bun must provide such interface.
May be model hooks in this lib must not to be? :)
Ideally, yes. Practically you should not use hooks for such common things like transactions and validation.
from bun.
Related Issues (20)
- [bug] Query Select uses wrong table name when ModelTableExpr is called HOT 2
- I want to be able to use PostgreSQL's parametric placeholders.
- inconsistent behavior for ModelTableExpr (select <--> create/insert/update/delete)
- race detected in ```schema.Field.Clone()``` HOT 2
- Issue with BeforeAppendModel when selecting columns to update HOT 1
- Go migrations are not discovered
- Custom sum column on a relation
- In v1.2, about 'unique' tag option in embedded structures HOT 1
- Call BeforeAppendModel on embedded types
- incorrect CTE generation
- Is []int64 supported for Arrays
- Support for DuckDB dialect
- Ambiguous Column Reference Id
- Scan error on column: bun: Scan(unsupported [32]uint8)"
- m2m relations with composite keys
- can't scan 1 (float32) into float32 HOT 2
- Issue: Panic on Bulk Upsert with Pointer Fields in Slice of Interfaces HOT 1
- Documentation Pgx section update HOT 2
- Is it possible to insert a comment before or after a sql query?
- how can i tag index in struct filed?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bun.