Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit 37463bb

Browse files
mistakiaaphelionz
authored andcommitted
# 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
1 parent 649ee15 commit 37463bb

File tree

1 file changed

+143
-43
lines changed

1 file changed

+143
-43
lines changed

README.md

Lines changed: 143 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,104 @@ Base class for [orbit-db](https://github.com/orbitdb/orbit-db) data stores. You
1515

1616
- Node.js >= 8.0.0
1717

18-
### API
18+
## Table of Contents
1919

20-
#### Public methods
20+
<!-- toc -->
2121

22-
##### `load(amount)`
22+
- [API](#api)
23+
* [constructor(ipfs, peerId, address, options)](#constructoripfs-peerid-address-options)
24+
* [Public methods](#public-methods)
25+
+ [load([amount])](#loadamount)
26+
+ [loadMoreFrom(amount, entries)](#loadmorefromamount-entries)
27+
+ [saveSnapshot()](#savesnapshot)
28+
+ [loadFromSnapshot()](#loadfromsnapshot)
29+
+ [close()](#close)
30+
+ [drop()](#drop)
31+
+ [sync(heads)](#syncheads)
32+
* [Properties](#properties)
33+
+ [address](#address)
34+
+ [key](#key)
35+
+ [all](#all)
36+
+ [type](#type)
37+
+ [replicationStatus](#replicationstatus)
38+
* [Events](#events)
39+
* [Private methods](#private-methods)
40+
+ [_addOperation(data)](#_addoperationdata)
41+
* [Creating Custom Data Stores](#creating-custom-data-stores)
42+
- [Contributing](#contributing)
43+
- [License](#license)
2344

24-
Load the database using locally persisted state. Can specify how many entries to load with `amount` argument.
45+
<!-- tocstop -->
2546

26-
##### `saveSnapshot()`
47+
## API
2748

28-
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.
49+
### constructor(ipfs, identity, address, options)
2950

30-
##### `loadFromSnapshot(hash, onProgressCallback)`
51+
**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.
3152

32-
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.
53+
`options` is an object with the following required properties:
3354

34-
##### `close()`
55+
- `cache`: A [Cache](https://github.com/orbitdb/orbit-db-cache) instance to use for storing heads and snapshots.
56+
- `Index` : By default it uses an instance of [Index](https://github.com/orbitdb/orbit-db-store/blob/master/src/Index.js).
3557

36-
Uninitialize the store. Emits `close` after the store has been uninitialized.
58+
the following properties are optional:
3759

38-
##### `drop()`
60+
- `maxHistory` (Integer): The number of entries to load (Default: `-1`).
61+
- `referenceCount` (Integer): The number of previous ipfs-log entries a new entry should reference (Default: `64`).
62+
- `replicationConcurrency` (Integer): The number of concurrent replication processes (Default: `128`).
63+
- `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.
64+
- `onClose` (Function): A function to be called with a string of the OrbitDB address of the database that is closing.
3965

40-
Remove the database locally. This doesn't remove or delete the database from peers who have replicated the database.
66+
### Public methods
4167

42-
##### `sync(heads)`
68+
#### load([amount])
69+
> Load the database using locally persisted state.
4370
44-
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.
71+
Returns a **Promise** that resolves once complete. Provide an `amount` argument to specify how many entries to load.
4572

46-
#### Properties
73+
#### loadMoreFrom(amount, entries)
74+
> TODO
4775
48-
##### `address`
76+
```javascript
77+
//TODO
78+
db.loadMoreFrom()
79+
```
80+
81+
#### saveSnapshot()
82+
> Save the current state of the database locally.
83+
84+
Returns a **Promise** that resolves to an array containing an object with the following properties:
85+
86+
- `path` of the snapshot file
87+
- `hash` representing the IPFS Multihash (as a Base58 encoded string) of the snapshot file
88+
- `size` of the snapshot file
89+
90+
#### loadFromSnapshot()
91+
> Load the state of the database from a snapshot.
92+
93+
Returns a **Promise** that resolves to a store instance once it has been loaded.
94+
95+
#### close()
96+
> Uninitialize the store.
97+
98+
Returns a **promise** that resolves once complete. Emits `close` after the store has been uninitialized.
4999

50-
Get the address of this database. Returns an object `{ root: <manifestHash>, path: <path> }`. Convert to a string with `db.address.toString()`.
100+
#### drop()
101+
> Remove the database locally.
102+
103+
Returns a **promise** that resolves once complete. This doesn't remove or delete the database from peers who have replicated the database.
104+
105+
#### sync(heads)
106+
> Sync this database with entries from **heads** where **heads** is an array of ipfs-log Entries.
107+
108+
Usually, you don't need to call this method manually as OrbitDB takes care of this for you.
109+
110+
### Properties
111+
112+
#### address
113+
> Get the address of this database.
114+
115+
Returns an object `{ root: <manifestHash>, path: <path> }`. Convert to a string with `db.address.toString()`.
51116

52117
```javascript
53118
console.log(db.address.toString())
@@ -63,17 +128,28 @@ console.log(db.identity.publicKey)
63128
// 042c07044e7ea51a489c02854db5e09f0191690dc59db0afd95328c9db614a2976e088cab7c86d7e48183191258fc59dc699653508ce25bf0369d67f33d5d77839
64129
```
65130

66-
##### `type`
131+
#### all
132+
> Get all of the entries in the store index
133+
134+
Returns an array of all store entries within the index.
135+
136+
```javascript
137+
db.all
138+
```
139+
140+
#### type
141+
> Get the store type
67142
68-
The type of datastore model of the current instance.
143+
Returns a string of the type of datastore model of the current instance.
69144

70145
```javascript
71146
console.log(db.type) // "eventlog"
72147
```
73148

74-
##### `replicationStatus`
149+
#### replicationStatus
150+
> Get database replication status information such as total number of entries and loading progress.
75151
76-
Get database replication status information such as total number of entries and loading progress.
152+
Returns an instance of [ReplicationInfo](https://github.com/orbitdb/orbit-db-store/blob/master/src/replication-info.js).
77153

78154
```javascript
79155
console.log(db.replicationStatus)
@@ -84,56 +160,80 @@ console.log(db.replicationStatus)
84160

85161
Store has an `events` ([EventEmitter](https://nodejs.org/api/events.html)) object that emits events that describe what's happening in the database.
86162

87-
- `load` - (dbname, hash)
163+
- `load` - (address, heads)
88164

89-
Emitted before loading the database history. *hash* is the hash from which the history is loaded.
165+
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`.
90166

91167
```javascript
92-
db.events.on('load', (id, hash) => ... )
168+
db.events.on('load', (address, heads) => ... )
93169
db.load()
94170
```
95171

96-
- `ready` - (dbname)
172+
- `ready` - (address, heads)
97173

98-
Emitted after fully loading the database history.
174+
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.
99175

100176
```javascript
101-
db.events.on('ready', (id) => ... )
177+
db.events.on('ready', (address, heads) => ... )
102178
db.load()
103179
```
104180

105-
- `load.progress` - (id, hash, entry, progress, total)
106-
107-
Emitted for each entry during load.
181+
- `load.progress` - (address, hash, entry, progress, total)
108182

109-
*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.
183+
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.
110184

111185
```javascript
112-
db.events.on('load.progress', (id, hash, entry, progress, total) => ... )
186+
db.events.on('load.progress', (address, hash, entry, progress, total) => ... )
113187
db.load()
114188
```
115189

116-
- `replicated` - (dbname)
190+
- `replicate` - (address, entry)
191+
192+
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.
193+
194+
```javascript
195+
db.events.on('replicate', (address, entry) => ... )
196+
```
197+
198+
- `replicate.progress` - (address, hash, entry, progress, total)
199+
200+
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.
201+
202+
```javascript
203+
db.events.on('replicate.progress', (address, hash, entry, progress, total) => ... )
204+
```
205+
206+
- `replicated` - (address, count)
207+
208+
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`.
209+
210+
```javascript
211+
db.events.on('replicated', (address, count) => ... )
212+
```
213+
214+
- `write` - (address, entry, heads)
117215

118-
Emitted after the database was synced with an update from a peer database.
216+
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.
119217

120218
```javascript
121-
db.events.on('replicated', (id, length) => ... )
219+
db.events.on('write', (address, entry, heads) => ... )
122220
```
123221

124-
- `write` - (id, hash, entry)
222+
- `closed` - (address)
125223

126-
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.
224+
Emitted once the database has finished closing. **address** is a string of the OrbitDB address that emitted the event.
127225

128226
```javascript
129-
db.events.on('write', (id, hash, entry) => ... )
227+
db.events.on('closed', (address) => ... )
228+
db.close()
130229
```
131230

132-
#### Private methods
231+
### Private methods
133232

134-
##### `_addOperation(data)`
233+
#### _addOperation(data)
234+
> Add an entry to the store.
135235

136-
Add an entry to the store. Takes `data` as a parameter which can be of any type.
236+
Returns a **Promise** that resolves to the IPFS Multihash of the added entry. Takes `data` as a parameter which can be of any type.
137237

138238
```javascript
139239
this._addOperation({
@@ -153,9 +253,9 @@ const Store = require('orbit-db-store');
153253
const KeyValueIndex = require('./KeyValueIndex');
154254
155255
class KeyValueStore extends Store {
156-
constructor(ipfs, identity, dbname, options) {
256+
constructor(ipfs, identity, address, options) {
157257
Object.assign(options || {}, { Index: KeyValueIndex });
158-
super(ipfs, identity, dbname, options)
258+
super(ipfs, identity, address, options)
159259
}
160260
161261
get(key) {

0 commit comments

Comments
 (0)