package badger import ( "fmt" "git.loyso.art/frx/eway/internal/entity" badger "github.com/dgraph-io/badger/v4" ) var ( categorySequenceIDKey = []byte("!!cat_seq!!") ) type client struct { db *badger.DB nextCategoryIDSeq *badger.Sequence } func NewClient(db *badger.DB) (*client, error) { categorySeqGen, err := db.GetSequence(categorySequenceIDKey, 10) if err != nil { return nil, fmt.Errorf("getting sequence for categories: %w", err) } return &client{ db: db, nextCategoryIDSeq: categorySeqGen, }, nil } // Close closes the underlying sequences in the client. Should be called right before // underlying *badger.DB closed. func (c *client) Close() error { err := c.nextCategoryIDSeq.Release() if err != nil { return fmt.Errorf("releasing next_category_sequence: %w", err) } return nil } func (c *client) Category() entity.CategoryRepository { return newCategoryClient(c.db, c.nextCategoryIDSeq) } func (c *client) GoodsItem() entity.GoodsItemRepository { return newGoodsItemClient(c.db) }