Documentation
¶
Overview ¶
Package orbitdb is a Go implementation of OrbitDB, a serverless, peer-to-peer database built on IPFS and libp2p.
It is a port of @orbitdb/core 4 and interoperates with it: identities, log entries, manifests, access controllers and the sync protocol are byte-compatible, so Go and JavaScript peers can open and replicate the same databases.
node, _ := ipfs.New(ctx, ipfs.Options{})
odb, _ := orbitdb.New(ctx, orbitdb.Options{IPFS: node})
defer odb.Stop()
kv, _ := odb.OpenKeyValue(ctx, "settings")
kv.Put(ctx, "theme", "dark")
fmt.Println(kv.Address()) // share this with peers
A peer that opens the same address replicates the database.
Example ¶
Two peers replicate a keyvalue database. The second one only needs the address; everything else comes from the first over the network.
package main
import (
"context"
"fmt"
"os"
"time"
orbitdb "github.com/orbitdb/go-orbitdb"
"github.com/orbitdb/go-orbitdb/accesscontrollers"
"github.com/orbitdb/go-orbitdb/ipfs"
)
func main() {
ctx := context.Background()
newPeer := func() (*ipfs.Node, *orbitdb.OrbitDB, func()) {
dir, _ := os.MkdirTemp("", "orbitdb-example")
node, err := ipfs.New(ctx, ipfs.Options{})
if err != nil {
panic(err)
}
odb, err := orbitdb.New(ctx, orbitdb.Options{IPFS: node, Directory: dir})
if err != nil {
panic(err)
}
return node, odb, func() {
_ = odb.Stop()
_ = node.Close()
_ = os.RemoveAll(dir)
}
}
nodeA, alice, closeA := newPeer()
defer closeA()
nodeB, bob, closeB := newPeer()
defer closeB()
if err := nodeB.Connect(ctx, nodeA.AddrInfo()); err != nil {
panic(err)
}
// Alice creates a database anyone may write to.
settings, err := alice.OpenKeyValue(ctx, "settings",
orbitdb.WithAccessController(accesscontrollers.IPFS(accesscontrollers.IPFSOptions{Write: []string{"*"}})))
if err != nil {
panic(err)
}
if _, err := settings.Put(ctx, "theme", "dark"); err != nil {
panic(err)
}
// Bob opens it by address and waits for the value to arrive.
replica, err := bob.OpenKeyValue(ctx, settings.Address())
if err != nil {
panic(err)
}
for range 100 {
if v, err := replica.Get(ctx, "theme"); err == nil {
fmt.Println("bob sees theme =", v)
break
}
time.Sleep(100 * time.Millisecond)
}
}
Output: bob sees theme = dark
Index ¶
- Constants
- func IsValidAddress(address string) bool
- func OpenAs[T databases.Store](ctx context.Context, o *OrbitDB, address string, opts ...OpenOption) (T, error)
- type Address
- type Manifest
- type OpenOption
- func WithAccessController(factory accesscontrollers.Factory) OpenOption
- func WithDatabase(factory databases.Factory) OpenOption
- func WithEncryption(e oplog.Encryption) OpenOption
- func WithEntryStorage(s storage.Storage) OpenOption
- func WithHeadsStorage(s storage.Storage) OpenOption
- func WithIndexStorage(s storage.Storage) OpenOption
- func WithMeta(meta any) OpenOption
- func WithReferencesCount(n int) OpenOption
- func WithSync(enabled bool) OpenOption
- func WithType(typ string) OpenOption
- type Options
- type OrbitDB
- func (o *OrbitDB) Directory() string
- func (o *OrbitDB) ID() string
- func (o *OrbitDB) IPFS() *ipfs.Node
- func (o *OrbitDB) Identities() *identities.Identities
- func (o *OrbitDB) Identity() *identitytypes.Identity
- func (o *OrbitDB) Keystore() *keystore.KeyStore
- func (o *OrbitDB) Open(ctx context.Context, address string, opts ...OpenOption) (databases.Store, error)
- func (o *OrbitDB) OpenDocuments(ctx context.Context, address string, opts ...OpenOption) (*databases.Documents, error)
- func (o *OrbitDB) OpenEvents(ctx context.Context, address string, opts ...OpenOption) (*databases.Events, error)
- func (o *OrbitDB) OpenKeyValue(ctx context.Context, address string, opts ...OpenOption) (*databases.KeyValue, error)
- func (o *OrbitDB) OpenKeyValueIndexed(ctx context.Context, address string, opts ...OpenOption) (*databases.KeyValueIndexed, error)
- func (o *OrbitDB) PeerID() peer.ID
- func (o *OrbitDB) Stop() error
Examples ¶
Constants ¶
const DefaultDatabaseType = databases.EventsType
DefaultDatabaseType is the type Open creates when none is given.
Variables ¶
This section is empty.
Functions ¶
func IsValidAddress ¶
IsValidAddress reports whether address is an OrbitDB address: it starts with /orbitdb (or \orbitdb) and the rest is a base58btc CID. It accepts what @orbitdb/core's isValidAddress accepts.
Types ¶
type Address ¶
type Address struct {
// Hash is the base58btc CID of the database manifest.
Hash string
}
Address is a database address, "/orbitdb/<manifest hash>".
func ParseAddress ¶
ParseAddress parses a database address.
type Manifest ¶
type Manifest struct {
Name string
Type string
// AccessController is the address of the access controller, e.g.
// "/ipfs/zdpu...".
AccessController string
// Meta is optional metadata; nil means the manifest has no meta field.
Meta any
}
Manifest describes a database. Its hash is the database address.
type OpenOption ¶
type OpenOption func(*openConfig)
OpenOption configures Open.
func WithAccessController ¶
func WithAccessController(factory accesscontrollers.Factory) OpenOption
WithAccessController sets the access controller of a new database. Defaults to accesscontrollers.IPFS with the local identity as sole writer. An existing database uses the controller in its manifest.
func WithDatabase ¶
func WithDatabase(factory databases.Factory) OpenOption
WithDatabase opens the database with factory instead of the one registered for its type, e.g. databases.KeyValueIndexedFactory.
func WithEncryption ¶
func WithEncryption(e oplog.Encryption) OpenOption
WithEncryption encrypts entries and/or payloads. Every peer must use the same encryption to read the database.
func WithEntryStorage ¶
func WithEntryStorage(s storage.Storage) OpenOption
WithEntryStorage replaces the default entry storage.
func WithHeadsStorage ¶
func WithHeadsStorage(s storage.Storage) OpenOption
WithHeadsStorage, WithEntryStorage and WithIndexStorage replace the database's default storages.
func WithIndexStorage ¶
func WithIndexStorage(s storage.Storage) OpenOption
WithIndexStorage replaces the default index storage.
func WithMeta ¶
func WithMeta(meta any) OpenOption
WithMeta sets the manifest metadata of a new database.
func WithReferencesCount ¶
func WithReferencesCount(n int) OpenOption
WithReferencesCount sets how many older entries each new entry refers to; see databases.Params.ReferencesCount.
func WithSync ¶
func WithSync(enabled bool) OpenOption
WithSync turns automatic syncing on (the default) or off.
func WithType ¶
func WithType(typ string) OpenOption
WithType sets the database type ("events", "keyvalue", "documents" or a registered custom type). It applies when creating a database; an existing database keeps the type in its manifest unless this is set.
type Options ¶
type Options struct {
// IPFS is the node databases store blocks on and sync over. Required.
IPFS *ipfs.Node
// ID is the id the identity is created for. Defaults to a random id.
ID string
// Identity is used as is instead of creating one. It must have been
// created by Identities (so its key is in the keystore).
Identity *identitytypes.Identity
// IdentityProvider creates the identity instead of the default
// publickey provider.
IdentityProvider providers.Provider
// Identities is used instead of creating one over a keystore in
// Directory/keystore.
Identities *identities.Identities
// Directory holds the keystore and the databases' local state.
// Defaults to "./orbitdb".
Directory string
}
Options configures New.
type OrbitDB ¶
type OrbitDB struct {
// contains filtered or unexported fields
}
OrbitDB opens and manages databases.
func (*OrbitDB) Identities ¶
func (o *OrbitDB) Identities() *identities.Identities
Identities returns the identities manager.
func (*OrbitDB) Identity ¶
func (o *OrbitDB) Identity() *identitytypes.Identity
Identity returns the identity databases are written as.
func (*OrbitDB) Open ¶
func (o *OrbitDB) Open(ctx context.Context, address string, opts ...OpenOption) (databases.Store, error)
Open opens the database at address, or creates a database named address if it is not an OrbitDB address. Opening an open database returns the same instance.
func (*OrbitDB) OpenDocuments ¶
func (o *OrbitDB) OpenDocuments(ctx context.Context, address string, opts ...OpenOption) (*databases.Documents, error)
OpenDocuments opens or creates a documents database. To index documents by a field other than "_id", pass WithDatabase(databases.DocumentsFactory(databases.DocumentsOptions{IndexBy: field})).
func (*OrbitDB) OpenEvents ¶
func (o *OrbitDB) OpenEvents(ctx context.Context, address string, opts ...OpenOption) (*databases.Events, error)
OpenEvents opens or creates an events database.
func (*OrbitDB) OpenKeyValue ¶
func (o *OrbitDB) OpenKeyValue(ctx context.Context, address string, opts ...OpenOption) (*databases.KeyValue, error)
OpenKeyValue opens or creates a keyvalue database.
func (*OrbitDB) OpenKeyValueIndexed ¶
func (o *OrbitDB) OpenKeyValueIndexed(ctx context.Context, address string, opts ...OpenOption) (*databases.KeyValueIndexed, error)
OpenKeyValueIndexed opens or creates a keyvalue database with a local index.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package accesscontrollers decides who may write to a database.
|
Package accesscontrollers decides who may write to a database. |
|
cmd
|
|
|
orbitdb-demo
command
Command orbitdb-demo is a small interactive OrbitDB peer.
|
Command orbitdb-demo is a small interactive OrbitDB peer. |
|
Package databases implements the OrbitDB database types on top of the operation log: Events (an append-only event log), KeyValue, KeyValueIndexed and Documents.
|
Package databases implements the OrbitDB database types on top of the operation log: Events (an append-only event log), KeyValue, KeyValueIndexed and Documents. |
|
Package encryption provides encryption modules for oplog.Encryption.
|
Package encryption provides encryption modules for oplog.Encryption. |
|
Package identities creates, stores, signs with and verifies OrbitDB identities.
|
Package identities creates, stores, signs with and verifies OrbitDB identities. |
|
identitytypes
Package identitytypes defines the Identity record that signs OrbitDB log entries, and its dag-cbor encoding.
|
Package identitytypes defines the Identity record that signs OrbitDB log entries, and its dag-cbor encoding. |
|
providers
Package providers implements identity providers, which decide what an identity id is and vouch for the identity's signing key.
|
Package providers implements identity providers, which decide what an identity id is and vouch for the identity's signing key. |
|
internal
|
|
|
block
Package block encodes and decodes OrbitDB's dag-cbor blocks.
|
Package block encodes and decodes OrbitDB's dag-cbor blocks. |
|
queue
Package queue runs tasks one at a time, in the order they were added, like the p-queue (concurrency 1) instances @orbitdb/core uses to serialise sync messages.
|
Package queue runs tasks one at a time, in the order they were added, like the p-queue (concurrency 1) instances @orbitdb/core uses to serialise sync messages. |
|
testutil
Package testutil holds fixtures shared by the package tests.
|
Package testutil holds fixtures shared by the package tests. |
|
Package ipfs assembles the IPFS pieces OrbitDB runs on: a libp2p host, gossipsub for database update announcements, and a block service whose bitswap exchange fetches entries, identities and manifests from peers.
|
Package ipfs assembles the IPFS pieces OrbitDB runs on: a libp2p host, gossipsub for database update announcements, and a block service whose bitswap exchange fetches entries, identities and manifests from peers. |
|
Package keystore manages the private keys OrbitDB identities sign with.
|
Package keystore manages the private keys OrbitDB identities sign with. |
|
Package oplog implements OrbitDB's operation log: an append-only, signed Merkle-CRDT of entries.
|
Package oplog implements OrbitDB's operation log: an append-only, signed Merkle-CRDT of entries. |
|
Package storage provides the key/value storage backends OrbitDB uses for log entries, heads, indexes, keys and identities.
|
Package storage provides the key/value storage backends OrbitDB uses for log entries, heads, indexes, keys and identities. |
|
Package syncutils implements the OrbitDB sync protocol, which replicates a log between peers.
|
Package syncutils implements the OrbitDB sync protocol, which replicates a log between peers. |