go-orbit-db icon indicating copy to clipboard operation
go-orbit-db copied to clipboard

Add an example, and link it from the README

Open moul opened this issue 5 years ago • 7 comments

moul avatar Aug 08 '19 15:08 moul

+1, just came across this awesome package and would be great to see an example with how joins work.

jryebread avatar Oct 18 '21 03:10 jryebread

A complete but not so simple example: https://github.com/mrusme/superhighway84/blob/master/database/database.go

GitHub
USENET-inspired, uncensorable, decentralized internet discussion system running on IPFS & OrbitDB - superhighway84/database.go at master · mrusme/superhighway84

diyism avatar May 19 '22 07:05 diyism

I'm trying to figure out a simplest go-orbit-db example code (test.go):

package main

import
(	"context"
	"time"
	"fmt"
	goOrbitDb "berty.tech/go-orbit-db"
	ifGoIpfsCore "github.com/ipfs/interface-go-ipfs-core"
	//"go.uber.org/zap"
	"berty.tech/go-orbit-db/stores/documentstore"
	"berty.tech/go-orbit-db/accesscontroller"
	"berty.tech/go-orbit-db/iface"
	"github.com/ipfs/go-ipfs/core"
	"github.com/ipfs/go-ipfs/repo/fsrepo"
	"github.com/ipfs/go-ipfs/core/node/libp2p"
	"github.com/ipfs/go-ipfs/core/coreapi"
)

func main()
{	CachePath:="./db_cache"
	storetype:="docstore"
	ac:=&accesscontroller.CreateAccessControllerOptions{Access: map[string][]string{"write": {"*"}}}

	//var Logger *zap.Logger
	var ConnectionString string="/orbitdb/bafyreifdpagppa7ve45odxuvudz5snbzcybwyfer777huckl4li4zbc5k4/superhighway84"

	ctx,cancel := context.WithTimeout(context.Background(), 600*time.Second)
	defer cancel()


	_,ifIpfs,err := createNode(ctx, "./")

	//Logger.Debug("initializing NewOrbitDB ...")
	OrbitDB,err := goOrbitDb.NewOrbitDB(	ctx,
											ifIpfs,
											&goOrbitDb.NewOrbitDBOptions{	Directory:&CachePath},
										)
	if err!=nil {	fmt.Printf("%+v", err); return}

	//Logger.Debug("initializing OrbitDB.Docs ...")
	Store,err := OrbitDB.Docs(	ctx,
								ConnectionString,
								&goOrbitDb.CreateDBOptions{	AccessController: ac,
															StoreType: &storetype,
															StoreSpecificOpts: documentstore.DefaultStoreOptsForMap("id"),
															Timeout: time.Second*600,
														},
							)
	if err != nil {	fmt.Printf("%+v", err); return}

	doc1 := map[string]interface{}{"_id": "doc1", "hello": "world"}
	_, err = Store.Put(ctx, doc1)
	fmt.Printf("%+v", "jack")

	docs, err := Store.Get(ctx, "doc1", &iface.DocumentStoreGetOptions{CaseInsensitive: false})
	fmt.Printf("%+v", docs)
}


func createNode(ctx context.Context, repoPath string) (*core.IpfsNode, ifGoIpfsCore.CoreAPI, error) {
  repo, err := fsrepo.Open(repoPath)
  if err != nil {
    return nil, nil, err
  }

  nodeOptions := &core.BuildCfg{
    Online:  true,
    Routing: libp2p.DHTClientOption, // DHTOption
    Repo: repo,
    ExtraOpts: map[string]bool{
      "pubsub": true,
    },
  }

  node, err := core.NewNode(ctx, nodeOptions)
  if err != nil {
    return nil, nil, err
  }

  coreAPI, err := coreapi.NewCoreAPI(node)
  if err != nil {
    return nil, nil, err
  }

  return node, coreAPI, nil
}

// the go.mod file:

module test
go 1.18
require (
	github.com/ipfs/interface-go-ipfs-core v0.5.2
)
require (
	github.com/lucas-clemente/quic-go v0.26.0 // indirect
)

// must lock the interface-go-ipfs-core version, or else show error ""undefined: blockstore.ErrNotFound"" $ go mod tidy $ export IPFS_PATH=./ $ ipfs init $ go run test.go

No luck , it shows:

unknown datastore type: flatfs

diyism avatar May 19 '22 18:05 diyism

@diyism I had the same issue until I added IPFS Plugins. It seems flatfs is implemented as a builtin plugin, so you need to include plugin support. Any luck creating a working example?

mp-netis avatar Jun 08 '22 17:06 mp-netis

based on the above, i bolted together a dirty example. no guarantees. https://gist.github.com/flowpoint/0643f7239920387e0de8e335fdbd18dd

Gist
dirty example to use go-orbitdb eventlog. GitHub Gist: instantly share code, notes, and snippets.

flowpoint avatar Nov 08 '22 17:11 flowpoint

When I run the example in the gist, it prints:

Computed default go-libp2p Resource Manager limits based on:
    - 'Swarm.ResourceMgr.MaxMemory': "8.6 GB"
    - 'Swarm.ResourceMgr.MaxFileDescriptors': 30720

Theses can be inspected with 'ipfs swarm resources'.

bafyreigotslg6nydukp3eqhybnhnkottgtjgrhqyd6hklz23j6qdn4w5cu&{Key:<nil> Op:ADD Value:[1 2] Docs:[] Entry:0x1400210c780}%

jefft0 avatar Nov 29 '23 09:11 jefft0

Keep this open as a tracking issue for adding examples. Put in backlog until we have more time to work on it.

jefft0 avatar Feb 16 '24 15:02 jefft0