Documentation
¶
Overview ¶
Package bunx is the seed subsystem's optional bun adapter: FirstOrCreate, the idempotent primitive seeders are built around. It mirrors dbx/bunx: nothing here is linked into a binary that does not import it, so the core seed package stays bun-free and usable with a hand-written native (pgx) seeder instead.
FirstOrCreate looks up an existing row by one or more column values taken straight from model, and inserts model only if no row matches:
user := &User{Email: "admin@example.com", Name: "Admin"}
created, err := bunx.FirstOrCreate(ctx, deps.DB, user, "email")
It runs through bunx.From (see the kit's dbx/bunx package), so it joins the bun transaction WithinTransaction opened, if any. There is no equivalent helper for the native pgx path: a native seeder writes its own "INSERT ... ON CONFLICT (col) DO NOTHING" against dbx/pg's DBTX, since bun is not on that path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FirstOrCreate ¶
func FirstOrCreate[T any](ctx context.Context, db bun.IDB, model *T, conflictCols ...string) (created bool, err error)
FirstOrCreate finds the row whose conflictCols match model's current field values and loads it into model, or inserts model as-is if none exists. conflictCols name bun columns (the "bun:" tag name, e.g. "email"), not Go struct field names, and must be non-empty. created is true only when a new row was inserted.
func FirstOrCreateWhere ¶
func FirstOrCreateWhere[T any](ctx context.Context, db bun.IDB, model *T, lookup func(*bun.SelectQuery) *bun.SelectQuery) (created bool, err error)
FirstOrCreateWhere is the general primitive behind FirstOrCreate: lookup narrows the select to whatever row should count as "the same one", for callers whose identity check isn't a flat column-equals-value match (a case-insensitive compare, a composite condition, ...). If lookup finds a row, it is scanned into model and created is false; otherwise model is inserted as-is and created is true.
Types ¶
This section is empty.