Keeper utilities for Cosmos SDK that attempt to implement Active Record.
Currently, three interfaces are supported:
Uint64KeyedIterableKeeper
- An auto-incrementinguint64
indexed keeperStringKeyedKeeper
- Astring
indexed keeperUint64AssociationKeeper
- One-to-many associations with another store
go get github.com/shanev/cosmos-record-keeper
Embed a RecordKeeper
struct inside a Keeper
.
type Keeper struct {
RecordKeeper
}
keeper := Keeper{
RecordKeeper(storeKey, codec),
}
Adds an object to a store and increments a unique id that will track the object.
record := Record{}
id := k.Add(ctx, record)
var record Record
k.Get(ctx, id, &record)
If you want control over an object's unique id, you can manually set it:
id := k.IncrementID(ctx)
k.Set(ctx, id, Record{})
k.Each(ctx, func(recordBytes []byte) bool {
var r Record
k.codec.MustUnmarshalBinaryLengthPrefixed(recordBytes, &r)
// do something with `Record` r
return true
})
k.Delete(ctx, id)
updatedRecord := Record{}
k.Update(ctx, id, updatedRecord)
// setter
record := Record{}
k.StringSet(ctx, "key1", record)
// getter
k.StringGet(ctx, "key1", &record)
k.Push(ctx, k.StoreKey, k2.StoreKey, uint64(1), uint64(2))
k.Map(ctx, k2.StoreKey, uint64(2), func(id uint64) {
// id == 1
})