orbit-db/README.md
2016-10-04 22:18:49 +02:00

5.2 KiB
Raw Blame History

OrbitDB

Distributed, peer-to-peer database on IPFS.

npm version CircleCI Status Project Status

Introduction

orbit-db is a serverless, distributed, peer-to-peer database. orbit-db uses IPFS as its data storage and IPFS Pubsub to automatically sync databases with peers.

Data in orbit-db can be stored in a

  • Key-Value Store
  • Eventlog (append-only log)
  • Feed (add and remove log)
  • Counters

This is the Javascript implementation and it works both in Node.js and Browsers.

Usage

npm install orbit-db
const IpfsApi = require('ipfs-api')
const OrbitDB = require('orbit-db')

const ipfs = IpfsApi('localhost', '5001')
const orbitdb = new OrbitDB(ipfs)

const db = orbitdb.eventlog("feed name")

db.add("hello world")
  .then(() => {
    const latest = db.iterator({ limit: 5 }).collect()
    console.log(latest.join("\n"))
  })

Examples

Clone

git clone https://github.com/haadcode/orbit-db.git
cd orbit-db
npm install

Browser example

npm run build:examples
npm run examples:browser

Node.js example

npm run examples:node

See details in example and run it with:

LOG=debug node examples/eventlog.js

API

WORK IN PROGRESS

Getting Started

const ipfs = require('ipfs')
const OrbitDB = require('orbit-db')
const orbitdb = new OrbitDB(ipfs)

orbitdb

  • kvstore(name)

    const db = orbitdb.kvstore('application.settings')
    
    • put(key, value)

      db.put('hello', { name: 'World' }).then(() => ...)
      
    • set(key, value)

      db.set('hello', { name: 'Friend' }).then(() => ...)
      
    • get(key)

      const value = db.get('hello')
      // { name: 'Friend' }
      
  • eventlog(name)

    const db = orbitdb.eventlog('site.visitors')
    
    • add(event)

      db.add({ name: 'User1' }).then((hash) => ...)
      
    • get(hash)

      const event = db.get(hash)
        .map((e) => e.payload.value)
      // { name: 'User1' }
      
    • iterator(options)

      // TODO: add all options - gt, gte, lt, lte, limit, reverse
      const all = db.iterator({ limit: -1 })
        .collect()
        .map((e) => e.payload.value)
      // [{ name: 'User1' }]
      
  • feed(name)

    const db = orbitdb.feed('orbit-db.issues')
    
    • add(value)

      db.add({ name: 'User1' }).then((hash) => ...)
      
    • get(hash)

      const event = db.get(hash)
        .map((e) => e.payload.value)
      // { name: 'User1' }
      
    • iterator(options)

      // TODO: add all options - gt, gte, lt, lte, limit, reverse
      const all = db.iterator({ limit: -1 })
        .collect()
        .map((e) => e.payload.value)
      // [{ name: 'User1' }]
      
    • remove(hash)

      db.remove(hash).then((removed) => ...)
      
  • counter(name)

    const counter = orbitdb.counter('song_123.play_count')
    
    • value

      counter.value // 0
      
    • inc([value])

      counter.inc()
      counter.value // 1
      counter.inc(7)
      counter.value // 8
      counter.inc(-2)
      counter.value // 8
      
  • events

    • data - (dbname, event)

      orbitdb.events.on('data', (dbname, event) => ...)
      
  • disconnect()

    orbitdb.disconnect()
    

Development

Run Tests

npm test

Build

npm run build

Background

TODO

Check out a visualization of the data flow at https://github.com/haadcode/proto2

Live demo: http://celebdil.benet.ai:8080/ipfs/Qmezm7g8mBpWyuPk6D84CNcfLKJwU6mpXuEN5GJZNkX3XK/

Screenshot

TODO: list of modules used, orbit-db-pubsub, etc.

Contributing

TODO

Issues and PRs welcome!

License

MIT (c) 2016 Haadcode