Categorygithub.com/alanshaw/ipfs-hookds
modulepackage
0.3.0
Repository: https://github.com/alanshaw/ipfs-hookds.git
Documentation: pkg.go.dev

# README

ipfs-hookds

Build Status Coverage Standard README GoDoc golang version Go Report Card

A wrapper for an IPFS datastore that adds optional before and after hooks to it's methods.

Install

go get github.com/alanshaw/ipfs-hookds

Usage

Example

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

API

GoDoc Reference

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw

# Packages

No description provided by the author
No description provided by the author

# Functions

NewBatching wraps a datastore.Batching datastore and adds optional before and after hooks into it's methods.
NewDatastore wraps a datastore.Datastore datastore and adds optional before and after hooks into it's methods.
WithAfterBatch configures a hook that is called _after_ Batch.
WithAfterDelete configures a hook that is called _after_ Delete.
WithAfterGet configures a hook that is called _after_ Get.
WithAfterHas configures a hook that is called _after_ Has.
WithAfterPut configures a hook that is called _after_ Put.
WithAfterQuery configures a hook that is called _after_ Query.
WithBeforeBatch configures a hook that is called _before_ Batch.
WithBeforeDelete configures a hook that is called _before_ Delete.
WithBeforeGet configures a hook that is called _before_ Get.
WithBeforeHas configures a hook that is called _before_ Has.
WithBeforePut configures a hook that is called _before_ Put.
WithBeforeQuery configures a hook that is called _before_ Query.

# Structs

Batching is a datastore with hooks that also supports batching.
Datastore is a wrapper for a datastore that adds optional before and after hooks into it's methods.
Options are hook datastore options.

# Type aliases

AfterBatchFunc is a handler for the after Batch hook.
AfterDeleteFunc is a handler for the after Delete hook.
AfterGetFunc is a handler for the after Get hook.
AfterHasFunc is a handler for the after Has hook.
AfterPutFunc is a handler for the after Put hook.
AfterQueryFunc is a handler for the after Query hook.
BeforeBatchFunc is a handler for the before Batch hook.
BeforeDeleteFunc is a handler for the before Delete hook.
BeforeGetFunc is a handler for the before Get hook.
BeforeHasFunc is a handler for the before Has hook.
BeforePutFunc is a handler for the before Put hook.
BeforeQueryFunc is a handler for the before Query hook.
Option is the hook datastore option type.