From 1bcc8a10c58261d3cbf1715810d547d1250290fc Mon Sep 17 00:00:00 2001 From: Kia Rahimian Date: Sun, 12 Aug 2018 10:03:32 -0400 Subject: [PATCH] docs: update & fix README --- README.md | 162 +++++++++++++++++++++++++++++++++++++-------------- src/Store.js | 8 +-- 2 files changed, 120 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index efffee7..7d38793 100644 --- a/README.md +++ b/README.md @@ -15,65 +15,113 @@ Base class for [orbit-db](https://github.com/orbitdb/orbit-db) data stores. You - Node.js >= 8.0.0 -### API +### constructor(ipfs, peerId, address, options) -#### Public methods +**ipfs** can be an [IPFS](https://github.com/ipfs/js-ipfs) instance or an [IPFS-API](https://github.com/ipfs/js-ipfs) instance. **peerId** is a string identifying the peer, usually the base58 string of the [PeerId](https://github.com/libp2p/js-peer-id#tob58string) of the IPFS instance. **address** is the OrbitDB address to be used for the store. -##### `load(amount)` +`options` is an object with the following required properties: -Load the database using locally persisted state. Can specify how many entries to load with `amount` argument. +- `cache`: A [Cache](https://github.com/orbitdb/orbit-db-cache) instance to use for storing heads and snapshots. +- `Index` : By default it uses an instance of [Index](https://github.com/orbitdb/orbit-db-store/blob/master/src/Index.js). +- `keystore`: A [Keystore](https://github.com/orbitdb/orbit-db-keystore) instance to use for key management. -##### `saveSnapshot()` +the following properties are optional: -Save the current state of the database locally. Returns a *Promise* that resolves to a IPFS Multihash as a Base58 encoded string. The the database can be loaded using this hash. +- `maxHistory` (Integer): The number of entries to load (Default: `-1`). +- `referenceCount` (Integer): The number of previous ipfs-log entries a new entry should reference (Default: `64`). +- `replicationConcurrency` (Integer): The number of concurrent replication processes (Default: `128`). +- `key`: A [KeyPair](https://github.com/indutny/elliptic/blob/master/lib/elliptic/ec/key.js#L8) instance. By default the provided keystore is used to find an existing KeyPair for the given `peerId`, otherwise a new KeyPair will be created using the given `peerId`. +- `accessController` (Object): By default only the owner will have write access. +- `onClose` (Function): A function to be called with a string of the OrbitDB address of the database that is closing. -##### `loadFromSnapshot(hash, onProgressCallback)` +### Public methods -Load the state of the database from a snapshot. *hash* is the IPFS Multihash of the snapshot data. Returns a *Promise* that resolves when the database has been loaded. +#### load([amount]) +> Load the database using locally persisted state. -##### `close()` +Returns a **Promise** that resolves once complete. Provide an `amount` argument to specify how many entries to load. -Uninitialize the store. Emits `close` after the store has been uninitialized. +#### loadMoreFrom(amount, entries) +> TODO -##### `drop()` +```javascript +//TODO +db.loadMoreFrom() +``` + +#### saveSnapshot() +> Save the current state of the database locally. + +Returns a **Promise** that resolves to an array containing an object with the following properties: + +- `path` of the snapshot file +- `hash` representing the IPFS Multihash (as a Base58 encoded string) of the snapshot file +- `size` of the snapshot file + +#### loadFromSnapshot() +> Load the state of the database from a snapshot. + +Returns a **Promise** that resolves to a store instance once it has been loaded. + +#### close() +> Uninitialize the store. -Remove the database locally. This doesn't remove or delete the database from peers who have replicated the database. +Returns a **promise** that resolves once complete. Emits `close` after the store has been uninitialized. -##### `sync(heads)` +#### drop() +> Remove the database locally. -Sync this database with entries from *heads* where *heads* is an array of ipfs-log Entries. Usually, you don't need to call this method manually as OrbitDB takes care of this for you. +Returns a **promise** that resolves once complete. This doesn't remove or delete the database from peers who have replicated the database. -#### Properties +#### sync(heads) +> Sync this database with entries from **heads** where **heads** is an array of ipfs-log Entries. -##### `address` +Usually, you don't need to call this method manually as OrbitDB takes care of this for you. -Get the address of this database. Returns an object `{ root: , path: }`. Convert to a string with `db.address.toString()`. +### Properties + +#### address +> Get the address of this database. + +Returns an object `{ root: , path: }`. Convert to a string with `db.address.toString()`. ```javascript console.log(db.address) // /orbitdb/Qmd8TmZrWASypEp4Er9tgWP4kCNQnW4ncSnvjvyHQ3EVSU/databaseName ``` -##### `key` +#### key +> Key pair used with this store to sign and access entries. -Key pair used with this store to sign and access entries. This key is the peer/node/user key. +Returns an instance of [KeyPair](https://github.com/indutny/elliptic/blob/master/lib/elliptic/ec/key.js#L8). This key is the peer/node/user key. ```javascript console.log(db.key.toPublic('hex')) // 042c07044e7ea51a489c02854db5e09f0191690dc59db0afd95328c9db614a2976e088cab7c86d7e48183191258fc59dc699653508ce25bf0369d67f33d5d77839 ``` -##### `type` +#### all +> Get all of the entries in the store index + +Returns an array of all store entries within the index. + +```javascript +db.all +``` + +#### type +> Get the store type -Remove all items from the local store. This doesn't remove or delete any entries in the distributed operations log. +Returns a string of the store type. ```javascript console.log(db.type) // "eventlog" ``` -##### `replicationStatus` +#### replicationStatus +> Get database replication status information such as total number of entries and loading progress. -Get database replication status information such as total number of entries and loading progress. +Returns an instance of [ReplicationInfo](https://github.com/orbitdb/orbit-db-store/blob/master/src/replication-info.js). ```javascript console.log(db.replicationStatus) @@ -84,56 +132,80 @@ console.log(db.replicationStatus) Store has an `events` ([EventEmitter](https://nodejs.org/api/events.html)) object that emits events that describe what's happening in the database. - - `load` - (dbname, hash) + - `load` - (address, heads) - Emitted before loading the database history. *hash* is the hash from which the history is loaded from. + Emitted before loading the database history. **address** is a string of the OrbitDB address that emitted the event. **heads** is an array of ipfs-log Entries. ```javascript - db.events.on('load', (id, hash) => ... ) + db.events.on('load', (address, heads) => ... ) db.load() ``` - - `ready` - (dbname) + - `ready` - (address, heads) - Emitted after fully loading the database history. + Emitted after fully loading the database history. **address** is a string of the OrbitDB address that emitted the event. **heads** is an array of ipfs-log Entries. ```javascript - db.events.on('ready', (id) => ... ) + db.events.on('ready', (address, heads) => ... ) db.load() ``` - - `load.progress` - (id, hash, entry, progress, total) - - Emitted for each entry during load. + - `load.progress` - (address, hash, entry, progress, total) - *Progress* is the current load count. *Total* is the maximum load count (ie. length of the full database). These are useful eg. for displaying a load progress percentage. + Emitted for each entry during load. **address** is a string of the OrbitDB address that emitted the event. **hash** is the multihash of the entry that was just loaded. **entry** is the ipfs-log Entry that was loaded. **Progress** is the current load count. **Total** is the maximum load count (ie. length of the full database). These are useful eg. for displaying a load progress percentage. ```javascript - db.events.on('load', (id, hash, entry, progress, total) => ... ) + db.events.on('load.progress', (address, hash, entry, progress, total) => ... ) db.load() ``` - - `replicated` - (dbname) + - `replicate` - (address, entry) + + Emitted before replicating a part of the database. **address** is a string of the OrbitDB address that emitted the event. **entry** is the ipfs-log Entry that is being processed. + + ```javascript + db.events.on('replicate', (address, entry) => ... ) + ``` + + - `replicate.progress` - (address, hash, entry, progress, total) + + Emitted while replicating a database. **address** is a string of the OrbitDB address of the database that emitted the event. **hash** is the multihash of the entry that was just replicated. **entry** is the ipfs-log Entry that was replicated. **progress** is an integer representing the current progress. **total** is an integer representing the remaining operations. + + ```javascript + db.events.on('replicate.progress', (address, hash, entry, progress, total) => ... ) + ``` + + - `replicated` - (address, logCount) + + Emitted after the database was synced with an update from a peer database. **address** is a string of the OrbitDB address that emitted the event. **logCount** ... + + ```javascript + db.events.on('replicated', (address, logCount) => ... ) + ``` + + - `write` - (address, entry, heads) - Emitted after the database was synced with an update from a peer database. + Emitted after an entry was added locally to the database. **address** is a string of the OrbitDB address that emitted the event. **entry** is the Entry that was added. **heads** is an array of ipfs-log Entries. ```javascript - db.events.on('replicated', (id) => ... ) + db.events.on('write', (address, entry, heads) => ... ) ``` - - `write` - (id, hash, entry) + - `closed` - (address) - Emitted after an entry was added locally to the database. *hash* is the IPFS hash of the latest state of the database. *entry* is the Entry that was added. + Emitted once the database has finished closing. **address** is a string of the OrbitDB address that emitted the event. ```javascript - db.events.on('write', (id, hash, entry) => ... ) + db.events.on('closed', (address) => ... ) + db.close() ``` -#### Private methods +### Private methods -##### `_addOperation(data)` +#### _addOperation(data) +> Add an entry to the store. -Add an entry to the store. Takes `data` as a parameter which can be of any type. +Returns a **Promise** that resolves to the IPFS Multihash of the added entry. Takes `data` as a parameter which can be of any type. ```javascript this._addOperation({ @@ -153,9 +225,9 @@ const Store = require('orbit-db-store'); const KeyValueIndex = require('./KeyValueIndex'); class KeyValueStore extends Store { - constructor(ipfs, id, dbname, options) { + constructor(ipfs, peerId, address, options) { Object.assign(options || {}, { Index: KeyValueIndex }); - super(ipfs, id, dbname, options) + super(ipfs, peerId, address, options) } get(key) { diff --git a/src/Store.js b/src/Store.js index c205ea2..16b301a 100644 --- a/src/Store.js +++ b/src/Store.js @@ -15,8 +15,6 @@ Logger.setLogLevel('ERROR') const DefaultOptions = { Index: Index, maxHistory: -1, - path: './orbitdb', - replicate: true, referenceCount: 64, replicationConcurrency: 128, } @@ -309,8 +307,8 @@ class Store { return snapshot } - async loadFromSnapshot (onProgressCallback) { - this.events.emit('load', this.address.toString()) + async loadFromSnapshot () { + this.events.emit('load', this.address.toString()) //TODO: inconsistent params, line 207 const maxClock = (res, val) => Math.max(res, val.clock.time) @@ -400,7 +398,7 @@ class Store { const log = await Log.fromJSON(this._ipfs, snapshotData, -1, this._key, this.access.write, 1000, onProgress) await this._oplog.join(log) await this._updateIndex() - this.events.emit('replicated', this.address.toString()) + this.events.emit('replicated', this.address.toString()) //TODO: inconsistent params, line 116 } this.events.emit('ready', this.address.toString(), this._oplog.heads) } else {