Help us shape the future of Go! Take the 2026 Go Developer Survey

cosmos-record-keeper

module
v0.0.0-...-be10673 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2019 License: Apache-2.0

README

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
})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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