Skip to content

Commit 4b785e0

Browse files
authored
(docs) Wait for ipfs ready event for orbitdb-archive#10
1 parent b1f9114 commit 4b785e0

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@ npm install orbit-db-docstore
1818
const IPFS = require('ipfs')
1919
const OrbitDB = require('orbit-db')
2020

21-
const ipfs = new IPFS()
22-
const orbitdb = new OrbitDB(ipfs)
23-
const docstore = orbitdb.docstore('db name')
24-
25-
docstore.put({ _id: 'hello world', doc: 'all the things' })
26-
.then(() => docstore.put({ _id: 'sup world', doc: 'other things' }))
27-
.then(() => docstore.get('hello'))
28-
.then((value) => console.log(value))
29-
// [{ _id: 'hello world', doc: 'all the things'}]
21+
const ipfs = new IPFS({ repo: 'orbitdb/ipfs' });
22+
ipfs.on('ready', () => {
23+
const orbitdb = new OrbitDB(ipfs);
24+
const docstore = orbitdb.docstore('dbName');
25+
26+
docstore.then(() => {
27+
docstore.put({ _id: 'hello world', doc: 'all the things' })
28+
.then(() => docstore.put({ _id: 'sup world', doc: 'other things' }))
29+
.then(() => docstore.get('hello'))
30+
.then((value) => console.log(value))
31+
// [{ _id: 'hello world', doc: 'all the things'}]
32+
})
33+
});
34+
35+
// or
36+
const docstore = await orbitdb.docstore('dbName');
37+
});
3038

3139
```
3240

3341
You can specify the field to index by in the options:
3442

3543
```javascript
36-
const docstore = orbitdb.docstore('db name', { indexBy: 'doc' })
44+
const docstore = await orbitdb.docstore('dbName', { indexBy: 'doc' })
3745

3846
docstore.put({ _id: 'hello world', doc: 'some things' })
3947
.then(() => docstore.put({ _id: 'hello universe', doc: 'all the things' }))
@@ -46,7 +54,7 @@ docstore.put({ _id: 'hello world', doc: 'some things' })
4654
You can also use a mapper to query the documents
4755

4856
```javascript
49-
const docstore = orbitdb.docstore('db name')
57+
const docstore = await orbitdb.docstore('dbName')
5058

5159
docstore.put({ _id: 'hello world', doc: 'some things', views: 10 })
5260
.then(() => docstore.put({ _id: 'hello universe', doc: 'all the things', views: 100 }))

0 commit comments

Comments
 (0)