2015-12-26 19:05:51 +02:00
|
|
|
# orbit-client
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
Key-Value Store and Event Store on IPFS.
|
2015-12-26 19:05:51 +02:00
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
***VERY MUCH WIP! WILL NOT WORK WHEN CLONED, orbit-server (not released yet) REQUIRED!***
|
2015-12-26 19:05:51 +02:00
|
|
|
|
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
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
_Channel maps to "table", "keyspace", "topic" or "feed" in similar systems_
|
2015-12-26 19:05:51 +02:00
|
|
|
|
|
|
|
## API
|
|
|
|
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-01-19 21:59:53 +08:00
|
|
|
.remove({ key: <key>, hash: <event's ipfs-hash> }) // Remove entry (use one option)
|
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
|
2015-12-26 21:11:43 +02:00
|
|
|
|
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)
|
2016-01-17 18:10:16 +08:00
|
|
|
|
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';
|
2015-12-27 17:27:17 +02:00
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
// Send an event
|
|
|
|
const head = orbit.channel(channelName).add('hello'); // <ipfs-hash>
|
2016-01-17 18:10:16 +08:00
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
// Delete an event
|
2016-01-17 18:10:16 +08:00
|
|
|
orbit.channel(channelName).remove(head);
|
2015-12-27 17:27:17 +02:00
|
|
|
|
|
|
|
// Iterator options
|
2016-01-19 21:59:53 +08:00
|
|
|
const options = { limit: -1 }; // fetch all messages
|
|
|
|
|
|
|
|
// Get events
|
2016-01-19 11:43:09 +08:00
|
|
|
const iter = orbit.channel(channelName).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
|
|
|
|
2016-01-19 21:59:53 +08:00
|
|
|
// KV-store
|
|
|
|
orbit.channel(channelName).put("key1", "hello world");
|
|
|
|
orbit.channel(channelName).get("key1"); // returns "hello world"
|
2016-01-18 18:41:00 +08:00
|
|
|
|
2016-01-19 21:59:53 +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-01-17 18:10:16 +08:00
|
|
|
channelModes = orbit.channel(channel).setMode({ mode: "+r", params: { password: password } }); // { modes: { r: { password: 'hello' } } }
|
2015-12-27 18:25:23 +02:00
|
|
|
channelModes = orbit.channel(channel, password).setMode({ mode: "+w", params: { ops: [orbit.user.id] } }); // { modes: { ... } }
|
|
|
|
channelModes = orbit.channel(channel, password).setMode({ mode: "-r" }); // { modes: { ... } }
|
|
|
|
channelModes = orbit.channel(channel, '').setMode({ mode: "-w" }); // { modes: {} }
|
|
|
|
|
|
|
|
// Delete channel
|
2016-01-19 11:43:09 +08:00
|
|
|
const result = orbit.channel(channelName, channelPwd).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
|
|
|
|
|
|
|
### TODO
|
|
|
|
- Tests for remove(), put() and get()
|
|
|
|
- Local caching of messages
|
|
|
|
- Possibility to fetch content separately from data structure
|
|
|
|
- Use HTTPS instead of HTTP (channel password are sent in plaintext atm)
|