db

package
v0.10.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthorBooksRel = drel.RelationInfo{
	Name:        "Books",
	Type:        drel.HasMany,
	FKColumn:    "author_id",
	RelatedMeta: drel.ToMetaBase(&models.BookMeta),
	FieldSetter: func(parent any, related any) {
		p := parent.(*models.Author)
		items := related.([]any)
		result := make([]*models.Book, len(items))
		for i, item := range items {
			result[i] = item.(*models.Book)
		}
		p.Books = result
	},
}
View Source
var AuthorIncludeBooks = drel.NewIncludeSpec(&AuthorBooksRel)
View Source
var AuthorIncludeProfile = drel.NewIncludeSpec(&AuthorProfileRel)
View Source
var AuthorProfileRel = drel.RelationInfo{
	Name:        "Profile",
	Type:        drel.HasOne,
	FKColumn:    "author_id",
	RelatedMeta: drel.ToMetaBase(&models.AuthorProfileMeta),
	FieldSetter: func(parent any, related any) {
		p := parent.(*models.Author)
		p.Profile = related.(*models.AuthorProfile)
	},
}
View Source
var BookAuthorRel = drel.RelationInfo{
	Name:        "Author",
	Type:        drel.BelongsTo,
	FKColumn:    "author_id",
	RelatedMeta: drel.ToMetaBase(&models.AuthorMeta),
	FieldSetter: func(parent any, related any) {
		p := parent.(*models.Book)
		p.Author = related.(*models.Author)
	},
}
View Source
var BookIncludeAuthor = drel.NewIncludeSpec(&BookAuthorRel)

Functions

This section is empty.

Types

type DB

type DB struct {
	*drel.Engine
	Authors        *models.AuthorRepository
	AuthorProfiles *models.AuthorProfileRepository
	Books          *models.BookRepository
}

func Open

func Open(dsn string, opts ...drel.Option) (*DB, error)

func (*DB) Tx added in v0.6.0

func (db *DB) Tx(ctx context.Context) TxRepos

Tx returns the tracked repositories bound to the transaction in ctx. It panics if no transaction is present, so wrap the call in WithTx.

func (*DB) WithTx added in v0.6.0

func (db *DB) WithTx(ctx context.Context, fn func(ctx context.Context) error, opts ...drel.TxOption) error

WithTx runs fn inside a transaction and puts that transaction in the context it passes to fn. Reach the tracked repositories with db.Tx(ctx). A nested call opens a savepoint on the same transaction.

type TxRepos added in v0.6.0

type TxRepos struct {
	Authors        *models.TxAuthorRepository
	AuthorProfiles *models.TxAuthorProfileRepository
	Books          *models.TxBookRepository
}

TxRepos holds the tracked repositories of one transaction. Load through the fields, stage with Add and Remove, then let WithTx commit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL