orbit-db/README.md

128 lines
4.1 KiB
Markdown
Raw Normal View History

# OrbitDB
2015-12-26 19:05:51 +02:00
## Introduction
Distributed, peer-to-peer* Key-Value Store and Event Log on IPFS.
2015-12-26 19:05:51 +02:00
2016-01-20 21:12:23 +08:00
Requires `orbit-server` to connect to. Get from https://github.com/haadcode/orbit-server.
2015-12-26 19:05:51 +02:00
_* Currently requires a centralized server. This will change in the future as required p2p features land in IPFS_
2016-01-19 21:59:53 +08:00
## Features
- Distributed kv-store and event log database
- Stores all data in IPFS
- Data encrypted on the wire and at rest
- Per channel access rights
2015-12-26 19:05:51 +02:00
_Channel is similar to "table", "keyspace", "topic", "feed" or "collection" in other systems_
2015-12-26 19:05:51 +02:00
## API
_See Usage below_
2015-12-26 19:05:51 +02:00
connect(host, username, password)
channel(name, password)
2016-01-19 21:59:53 +08:00
.add(data: String) // Insert an event to a channel, returns <ipfs-hash> of the event
.iterator([options]) // Returns an iterator of events
// options : {
// gt: <ipfs-hash>, // Return events newer than <ipfs-hash>
// gte: <ipfs-hash>, // Return events newer then <ipfs-hash> (inclusive)
// lt: <ipfs-hash>, // Return events older than <ipfs-hash>
// lte: <ipfs-hash>, // Return events older than <ipfs-hash> (inclusive)
// limit: -1, // Number of events to return, -1 returns all, default 1
// reverse: true // Return items oldest first, default latest first
// }
.put(key, data: String) // Insert (key,value) to a channel
.get(key) // Retrieve value
2015-12-26 19:05:51 +02:00
2016-02-12 09:27:57 +01:00
.del({ key: <key or hash> }) // Remove entry
2016-01-19 11:43:09 +08:00
2016-01-19 21:59:53 +08:00
.setMode(modes) // Set channel modes, can be an object or an array of objects
2015-12-26 19:05:51 +02:00
2016-01-19 21:59:53 +08:00
// { mode: "+r", params: { password: password } } // Set read mode
// { mode: "-r" } // Remove read-mode
// { mode: "+w", params: { ops: [orbit.user.id] } } // Set write-mode, only users in ops can write
// { mode: "-w" } // Remove write-mode
2015-12-26 19:05:51 +02:00
2016-01-19 21:59:53 +08:00
.info() // Returns channel's current head and modes
2016-01-19 21:59:53 +08:00
.delete() // Deletes the channel, all data will be "removed" (unassociated with the channel, actual data is not deleted)
2015-12-26 19:05:51 +02:00
## Usage
```javascript
2015-12-27 17:27:17 +02:00
var async = require('asyncawait/async');
2015-12-26 19:05:51 +02:00
var OrbitClient = require('./OrbitClient');
var host = 'localhost:3006'; // orbit-server address
2015-12-27 17:27:17 +02:00
async(() => {
// Connect
2016-01-19 21:59:53 +08:00
const orbit = OrbitClient.connect(host, username, password);
2015-12-27 17:27:17 +02:00
2016-01-19 11:43:09 +08:00
const channelName = 'hello-world';
2016-02-12 09:27:57 +01:00
const db = orbit.channel(channelName);
2015-12-27 17:27:17 +02:00
/* Event Log */
2016-02-12 09:27:57 +01:00
const hash = db.add('hello'); // <ipfs-hash>
// Remove event
db.remove(hash);
2015-12-27 17:27:17 +02:00
// Iterator options
2016-02-12 09:27:57 +01:00
const options = { limit: -1 }; // fetch all messages
2016-01-19 21:59:53 +08:00
// Get events
2016-02-12 09:27:57 +01:00
const iter = db.iterator(options); // Symbol.iterator
const next = iter.next(); // { value: <item>, done: false|true}
2016-01-19 21:59:53 +08:00
2016-01-18 18:41:00 +08:00
// OR:
// var all = iter.collect(); // returns all elements as an array
2015-12-27 17:27:17 +02:00
// OR:
// for(let i of iter)
2016-01-19 21:59:53 +08:00
// console.log(i.hash, i.item);
2015-12-27 17:27:17 +02:00
/* KV Store */
2016-02-12 09:27:57 +01:00
db.put('key1', 'hello world');
db.get('key1'); // returns "hello world"
db.remove('key1');
2016-01-18 18:41:00 +08:00
/* Modes */
2016-01-19 11:43:09 +08:00
const password = 'hello';
2016-01-19 22:06:00 +08:00
let channelModes;
2016-02-12 09:27:57 +01:00
channelModes = db.setMode({ mode: '+r', params: { password: password } }); // { modes: { r: { password: 'hello' } } }
const privateChannel = orbit.channel(channel, password);
channelModes = privateChannel.setMode({ mode: '+w', params: { ops: [orbit.user.id] } }); // { modes: { ... } }
channelModes = privateChannel.setMode({ mode: '-r' }); // { modes: { ... } }
channelModes = privateChannel.setMode({ mode: '-w' }); // { modes: {} }
/* Delete channel */
2016-02-12 09:27:57 +01:00
const result = db.delete(); // true | false
2015-12-27 17:27:17 +02:00
})();
2015-12-26 19:05:51 +02:00
```
2016-01-19 21:59:53 +08:00
### Development
#### Run Tests
2016-02-03 14:28:00 +07:00
**Note!** Requires Aerospike, see http://www.aerospike.com/docs/operations/install/
```
npm test
```
Keep tests running while development:
```
mocha -w
```
2016-01-19 21:59:53 +08:00
### TODO
- Tests for remove(), put() and get()
2016-02-02 22:59:58 +08:00
- pubsub communication (use redis to mock ipfs pubsub)
2016-01-19 21:59:53 +08:00
- Local caching of messages
- Possibility to fetch content separately from data structure
- Use HTTPS instead of HTTP (channel password are sent in plaintext atm)