A wrapper for an IPFS datastore that adds optional before and after hooks to it's methods.
go get github.com/alanshaw/ipfs-hookds
Hook for after a Put
:
package main
import (
"fmt"
"github.com/ipfs/go-datastore"
"github.com/alanshaw/ipfs-hookds"
)
func main() {
ds := datastore.NewMapDatastore()
hds := hook.NewDatastore(ds, hook.WithAfterPut(func(k datastore.Key, v []byte, err error) error {
fmt.Printf("key: %v value: %s was put to the datastore\n", k, v)
return err
}))
defer hds.Close()
key := datastore.NewKey("test")
value := []byte("test")
hds.Put(key, value)
// Output:
// key: /test value: test was put to the datastore
}
Hook into a batch Put
:
package main
import (
"fmt"
"github.com/ipfs/go-datastore"
"github.com/alanshaw/ipfs-hookds/batch"
"github.com/alanshaw/ipfs-hookds"
)
func main() {
ds := datastore.NewMapDatastore()
hds := hook.NewDatastore(ds, hook.WithAfterBatch(func(b datastore.Batch, err error) (datastore.Batch, error) {
return batch.NewBatch(b, batch.WithAfterPut(func(datastore.Key, []byte, error) error {
fmt.Printf("key: %v value: %s was put to a batch\n", k, v)
return err
})), err
}))
defer hds.Close()
key := datastore.NewKey("test")
value := []byte("test")
bch := hds.Batch()
bch.Put(key, value)
bch.Commit()
// Output:
// key: /test value: test was put to a batch
}
Hook into a query NextSync
:
package main
import (
"fmt"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/alanshaw/ipfs-hookds/query/results"
"github.com/alanshaw/ipfs-hookds"
)
func main() {
ds := datastore.NewMapDatastore()
hds := hook.NewDatastore(ds, hook.WithAfterQuery(func(q query.Query, res query.Results, err error) (query.Results, error) {
return results.NewResults(res, results.WithAfterNextSync(func(r query.Result, ok bool) (query.Result, bool) {
fmt.Printf("result: %v ok: %s was next\n", r, ok)
return r, ok
})), err
}))
defer hds.Close()
key := datastore.NewKey("test")
value := []byte("test")
hds.Put(key, value)
res := hds.Query(query.Query{
Prefix: "/test"
})
res.NextSync()
}
Feel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw