From 37463bbdb684bad2ceeafcb3674196c58aa22946 Mon Sep 17 00:00:00 2001 From: Kia Rahimian Date: Sun, 12 Aug 2018 10:03:32 -0400 Subject: [PATCH] # This is a combination of 2 commits. # This is the 1st commit message: # This is a combination of 2 commits. # This is the 1st commit message: docs(README): add TOC & update API # This is the commit message #2: docs: clarify load & replicated events # This is the commit message #2: docs: update constructor params --- README.md | 186 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 143 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 2157fb0..7efe642 100644 --- a/README.md +++ b/README.md @@ -15,39 +15,104 @@ Base class for [orbit-db](https://github.com/orbitdb/orbit-db) data stores. You - Node.js >= 8.0.0 -### API +## Table of Contents -#### Public methods + -##### `load(amount)` +- [API](#api) + * [constructor(ipfs, peerId, address, options)](#constructoripfs-peerid-address-options) + * [Public methods](#public-methods) + + [load([amount])](#loadamount) + + [loadMoreFrom(amount, entries)](#loadmorefromamount-entries) + + [saveSnapshot()](#savesnapshot) + + [loadFromSnapshot()](#loadfromsnapshot) + + [close()](#close) + + [drop()](#drop) + + [sync(heads)](#syncheads) + * [Properties](#properties) + + [address](#address) + + [key](#key) + + [all](#all) + + [type](#type) + + [replicationStatus](#replicationstatus) + * [Events](#events) + * [Private methods](#private-methods) + + [_addOperation(data)](#_addoperationdata) + * [Creating Custom Data Stores](#creating-custom-data-stores) +- [Contributing](#contributing) +- [License](#license) -Load the database using locally persisted state. Can specify how many entries to load with `amount` argument. + -##### `saveSnapshot()` +## API -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. +### constructor(ipfs, identity, address, options) -##### `loadFromSnapshot(hash, onProgressCallback)` +**ipfs** can be an [IPFS](https://github.com/ipfs/js-ipfs) instance or an [IPFS-API](https://github.com/ipfs/js-ipfs) instance. **identity** is an instance of [Identity](https://github.com/orbitdb/orbit-db-identity-provider/). **address** is the OrbitDB address to be used for the store. -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. +`options` is an object with the following required properties: -##### `close()` +- `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). -Uninitialize the store. Emits `close` after the store has been uninitialized. +the following properties are optional: -##### `drop()` +- `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`). +- `accessController` (Object): An instance of AccessController with the following [interface](https://github.com/orbitdb/orbit-db-access-controllers/blob/master/src/access-controller-interface.js). See [orbit-db-access-controllers](https://github.com/orbitdb/orbit-db-access-controllers) for more information on how to create custom access controllers. 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. -Remove the database locally. This doesn't remove or delete the database from peers who have replicated the database. +### Public methods -##### `sync(heads)` +#### load([amount]) +> Load the database using locally persisted state. -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. Provide an `amount` argument to specify how many entries to load. -#### Properties +#### loadMoreFrom(amount, entries) +> TODO -##### `address` +```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. + +Returns a **promise** that resolves once complete. Emits `close` after the store has been uninitialized. -Get the address of this database. Returns an object `{ root: , path: }`. Convert to a string with `db.address.toString()`. +#### drop() +> Remove the database locally. + +Returns a **promise** that resolves once complete. This doesn't remove or delete the database from peers who have replicated the database. + +#### sync(heads) +> 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. + +### 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.toString()) @@ -63,17 +128,28 @@ console.log(db.identity.publicKey) // 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 -The type of datastore model of the current instance. +Returns a string of the type of datastore model of the current instance. ```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 +160,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. + Emitted before loading the database history. **address** is a string of the OrbitDB address being loaded. **heads** is an array of ipfs-log Entries from which the history is loaded from. **heads** is omitted when this event is emitted as a result of `loadFromSnapshot`. ```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.progress', (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, count) + + 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. **count** number of items replicated. **count** is omitted when this event is emitted as a result of `loadFromSnapshot`. + + ```javascript + db.events.on('replicated', (address, count) => ... ) + ``` + + - `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, length) => ... ) + 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 +253,9 @@ const Store = require('orbit-db-store'); const KeyValueIndex = require('./KeyValueIndex'); class KeyValueStore extends Store { - constructor(ipfs, identity, dbname, options) { + constructor(ipfs, identity, address, options) { Object.assign(options || {}, { Index: KeyValueIndex }); - super(ipfs, identity, dbname, options) + super(ipfs, identity, address, options) } get(key) {