/cosmos-record-keeper

Keeper utilities for @cosmos SDK that implement Active Record

Primary LanguageGoApache License 2.0Apache-2.0

logo

Keeper utilities for Cosmos SDK that attempt to implement Active Record.

Currently, three interfaces are supported:

  • Uint64KeyedIterableKeeper - An auto-incrementing uint64 indexed keeper
  • StringKeyedKeeper - A string indexed keeper
  • Uint64AssociationKeeper - One-to-many associations with another store

CircleCI Go Report Card

Getting Started

Install library

go get github.com/shanev/cosmos-record-keeper

Example

Embed a RecordKeeper struct inside a Keeper.

type Keeper struct {
    RecordKeeper
}

Initialization

keeper := Keeper{
    RecordKeeper(storeKey, codec),
}

Adding

Adds an object to a store and increments a unique id that will track the object.

record := Record{}
id := k.Add(ctx, record)

Getting

var record Record
k.Get(ctx, id, &record)

Setting

If you want control over an object's unique id, you can manually set it:

id := k.IncrementID(ctx)
k.Set(ctx, id, Record{})

Iterating

k.Each(ctx, func(recordBytes []byte) bool {
    var r Record
    k.codec.MustUnmarshalBinaryLengthPrefixed(recordBytes, &r)
    // do something with `Record` r
    return true
})

Deleting

k.Delete(ctx, id)

Updating

updatedRecord := Record{}
k.Update(ctx, id, updatedRecord)

String Example

// setter
record := Record{}
k.StringSet(ctx, "key1", record)

// getter
k.StringGet(ctx, "key1", &record)

One-to-many Association Example

k.Push(ctx, k.StoreKey, k2.StoreKey, uint64(1), uint64(2))
k.Map(ctx, k2.StoreKey, uint64(2), func(id uint64) {
    // id == 1
})