Giter Site home page Giter Site logo

Empty model on BeforeInsert hook about bun HOT 7 CLOSED

uptrace avatar uptrace commented on August 18, 2024 1
Empty model on BeforeInsert hook

from bun.

Comments (7)

vmihailenco avatar vmihailenco commented on August 18, 2024 2

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.

vmihailenco avatar vmihailenco commented on August 18, 2024 1

Also see https://bun.uptrace.dev/guide/hooks.html#disclaimer

from bun.

vmihailenco avatar vmihailenco commented on August 18, 2024 1

IDB was added in v0.2.1. See https://github.com/uptrace/bun/tree/master/example/tx-composition

from bun.

jpascal avatar jpascal commented on August 18, 2024

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.

jpascal avatar jpascal commented on August 18, 2024

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.

jpascal avatar jpascal commented on August 18, 2024

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.

vmihailenco avatar vmihailenco commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.