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

Commit 2fb5087

Browse files
committed
docs: update & fix README
1 parent 39d83b3 commit 2fb5087

File tree

4 files changed

+548
-51
lines changed

4 files changed

+548
-51
lines changed

README.md

Lines changed: 148 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,144 @@ 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, peerId, 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. **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.
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).
57+
- `keystore`: A [Keystore](https://github.com/orbitdb/orbit-db-keystore) instance to use for key management.
3558

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

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

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

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

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

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

52119
```javascript
53120
console.log(db.address)
54121
// /orbitdb/Qmd8TmZrWASypEp4Er9tgWP4kCNQnW4ncSnvjvyHQ3EVSU/databaseName
55122
```
56123

57-
##### `key`
124+
#### key
125+
> Key pair used with this store to sign and access entries.
58126
59-
Key pair used with this store to sign and access entries. This key is the peer/node/user key.
127+
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.
60128

61129
```javascript
62130
console.log(db.key.toPublic('hex'))
63131
// 042c07044e7ea51a489c02854db5e09f0191690dc59db0afd95328c9db614a2976e088cab7c86d7e48183191258fc59dc699653508ce25bf0369d67f33d5d77839
64132
```
65133

66-
##### `type`
134+
#### all
135+
> Get all of the entries in the store index
136+
137+
Returns an array of all store entries within the index.
138+
139+
```javascript
140+
db.all
141+
```
142+
143+
#### type
144+
> Get the store type
67145
68-
Remove all items from the local store. This doesn't remove or delete any entries in the distributed operations log.
146+
Returns a string of the store type.
69147

70148
```javascript
71149
console.log(db.type) // "eventlog"
72150
```
73151

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

78157
```javascript
79158
console.log(db.replicationStatus)
@@ -84,56 +163,80 @@ console.log(db.replicationStatus)
84163

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

87-
- `load` - (dbname, hash)
166+
- `load` - (address, heads)
88167

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

91170
```javascript
92-
db.events.on('load', (id, hash) => ... )
171+
db.events.on('load', (address, heads) => ... )
93172
db.load()
94173
```
95174

96-
- `ready` - (dbname)
175+
- `ready` - (address, heads)
97176

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

100179
```javascript
101-
db.events.on('ready', (id) => ... )
180+
db.events.on('ready', (address, heads) => ... )
102181
db.load()
103182
```
104183

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

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.
186+
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.
110187

111188
```javascript
112-
db.events.on('load', (id, hash, entry, progress, total) => ... )
189+
db.events.on('load.progress', (address, hash, entry, progress, total) => ... )
113190
db.load()
114191
```
115192

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

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

120221
```javascript
121-
db.events.on('replicated', (id) => ... )
222+
db.events.on('write', (address, entry, heads) => ... )
122223
```
123224

124-
- `write` - (id, hash, entry)
225+
- `closed` - (address)
125226

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

128229
```javascript
129-
db.events.on('write', (id, hash, entry) => ... )
230+
db.events.on('closed', (address) => ... )
231+
db.close()
130232
```
131233

132-
#### Private methods
234+
### Private methods
133235

134-
##### `_addOperation(data)`
236+
#### _addOperation(data)
237+
> Add an entry to the store.
135238

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

138241
```javascript
139242
this._addOperation({
@@ -153,9 +256,9 @@ const Store = require('orbit-db-store');
153256
const KeyValueIndex = require('./KeyValueIndex');
154257
155258
class KeyValueStore extends Store {
156-
constructor(ipfs, id, dbname, options) {
259+
constructor(ipfs, peerId, address, options) {
157260
Object.assign(options || {}, { Index: KeyValueIndex });
158-
super(ipfs, id, dbname, options)
261+
super(ipfs, peerId, address, options)
159262
}
160263
161264
get(key) {

0 commit comments

Comments
 (0)