2018-07-12 15:07:30 -04:00
# OrbitDB API Documentation
2017-03-31 10:54:56 +02:00
Read the ** [GETTING STARTED ](https://github.com/orbitdb/orbit-db/blob/master/GUIDE.md )** guide for a more in-depth tutorial and to understand how OrbitDB works.
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
## Table of Contents
<!-- toc -->
- [Constructor ](#constructor )
* [constructor(ipfs, [directory], [options])](#constructoripfs -directory-options)
- [Public Instance Methods ](#public-instance-methods )
* [orbitdb.create(name, type, [options])](#orbitdbcreatename -type-options)
* [orbitdb.open(address, [options])](#orbitdbopenaddress -options)
* [orbitdb.disconnect() ](#orbitdbdisconnect )
* [orbitdb.stop() ](#orbitdbstop )
* [orbitdb.keyvalue(name|address) ](#orbitdbkeyvaluenameaddress )
+ [put(key, value) ](#putkey-value )
+ [set(key, value) ](#setkey-value )
+ [get(key) ](#getkey )
* [orbitdb.kvstore(name|address) ](#orbitdbkvstorenameaddress )
* [orbitdb.log(name|address) ](#orbitdblognameaddress )
+ [add(event) ](#addevent )
+ [get(hash) ](#gethash )
+ [iterator([options])](#iteratoroptions )
* [orbitdb.eventlog(name|address) ](#orbitdbeventlognameaddress )
* [orbitdb.feed(name|address) ](#orbitdbfeednameaddress )
+ [add(data) ](#adddata )
+ [get(hash) ](#gethash-1 )
+ [remove(hash) ](#removehash )
+ [iterator([options])](#iteratoroptions -1)
* [orbitdb.docs(name|address, options) ](#orbitdbdocsnameaddress-options )
+ [put(doc) ](#putdoc )
+ [get(key) ](#getkey-1 )
+ [query(mapper) ](#querymapper )
+ [del(key) ](#delkey )
* [orbitdb.docstore(name|address, options) ](#orbitdbdocstorenameaddress-options )
* [orbitdb.counter(name|address) ](#orbitdbcounternameaddress )
+ [value ](#value )
+ [inc([value])](#incvalue )
- [Static Properties ](#static-properties )
* [OrbitDB.databaseTypes ](#orbitdbdatabasetypes )
- [Static Methods ](#static-methods )
* [OrbitDB.isValidType(type) ](#orbitdbisvalidtypetype )
* [OrbitDB.addDatabaseType(type, store) ](#orbitdbadddatabasetypetype-store )
* [OrbitDB.getDatabaseTypes() ](#orbitdbgetdatabasetypes )
* [OrbitDB.isValidAddress(address) ](#orbitdbisvalidaddressaddress )
* [OrbitDB.parseAddress(address) ](#orbitdbparseaddressaddress )
- [Store API ](#store-api )
* [store.load([amount])](#storeloadamount )
* [store.close() ](#storeclose )
* [store.drop() ](#storedrop )
* [store.key ](#storekey )
* [store.type ](#storetype )
- [Store Events ](#store-events )
* [`replicated` ](#replicated )
* [`replicate` ](#replicate )
+ [`replicate.progress` ](#replicateprogress )
* [`load` ](#load )
+ [`load.progress` ](#loadprogress )
* [`ready` ](#ready )
* [`write` ](#write )
* [`closed` ](#closed )
<!-- tocstop -->
## Constructor
2018-07-12 15:07:30 -04:00
### constructor(ipfs, [directory], [options])
2016-10-05 10:33:45 +02:00
```javascript
const orbitdb = new OrbitDB(ipfs)
```
2018-07-12 15:07:30 -04:00
Creates and returns an instance of OrbitDB. Use the optional `directory` argument to specify a path to be used for the database files (Default: `'./orbitdb'` ). In addition, you can use the optional `options` argument for further configuration. It is an object with any of these properties:
2016-10-05 10:33:45 +02:00
2018-07-12 15:07:30 -04:00
- `peerId` (string): By default it uses the base58 string of the ipfs peer id.
2016-10-05 10:33:45 +02:00
2018-07-12 15:07:30 -04:00
- `keystore` (Keystore Instance) : By default creates an instance of [Keystore ](https://github.com/orbitdb/orbit-db-keystore ). A custom keystore instance can be used, see [this ](https://github.com/orbitdb/orbit-db/blob/master/test/utils/custom-test-keystore.js ) for an example.
2017-03-30 18:43:47 +02:00
2018-08-12 11:26:28 -04:00
After creating an `OrbitDB` instance , you can access the different data stores. Creating a database instance, eg. with `orbitdb.keyvalue(...)` , returns a *Promise* that resolves to a [database instance ](#store-api ). See the [Store ](#store-api ) section for details of common methods and properties.
2017-03-30 18:43:47 +02:00
2018-07-12 16:14:55 -04:00
*For further details, see usage for [kvstore ](https://github.com/orbitdb/orbit-db-kvstore#usage ), [eventlog ](https://github.com/orbitdb/orbit-db-eventstore#usage ), [feed ](https://github.com/orbitdb/orbit-db-feedstore#usage ), [docstore ](https://github.com/orbitdb/orbit-db-docstore#usage ) and [counter ](https://github.com/orbitdb/orbit-db-counterstore#usage ).*
2017-03-31 10:54:56 +02:00
```javascript
2018-07-12 13:08:41 -04:00
const db = await orbitdb.keyvalue('profile')
2017-03-31 10:54:56 +02:00
```
2017-03-30 18:43:47 +02:00
2018-07-12 15:07:30 -04:00
## Public Instance Methods
2018-08-08 18:07:51 -04:00
2018-07-13 15:21:21 -04:00
### orbitdb.create(name, type, [options])
> Creates and opens an OrbitDB database.
2018-08-12 11:26:28 -04:00
Returns a `Promise` that resolves to [a database instance ](#store-api ). `name` (string) should be the database name, not an OrbitDB address (i.e. `user.posts` ). `type` is a supported database type (i.e. `eventlog` or [an added custom type ](https://github.com/orbitdb/orbit-db#custom-store-types )). `options` is an object with any of the following properties:
2018-07-13 15:21:21 -04:00
- `directory` (string): The directory where data will be stored (Default: uses directory option passed to OrbitDB constructor or `./orbitdb` if none was provided).
2018-09-20 15:30:44 -04:00
- `write` (array): An array of hex encoded public keys which are used to set write access to the database. `["*"]` can be passed in to give write access to everyone. See the [GETTING STARTED ](https://github.com/orbitdb/orbit-db/blob/master/GUIDE.md ) guide for more info. (Default: uses the OrbitDB instance key `orbitdb.key` , which would give write access only to yourself)
2018-07-13 15:21:21 -04:00
- `overwrite` (boolean): Overwrite an existing database (Default: `false` )
2018-08-16 08:44:30 -04:00
- `replicate` (boolean): Replicate the database with peers, requires IPFS PubSub. (Default: `true` )
2018-07-13 15:21:21 -04:00
```javascript
const db = await orbitdb.create('user.posts', 'eventlog', {
write: [
// Give access to ourselves
orbitdb.key.getPublic('hex'),
// Give access to the second peer
'042c07044e7ea51a489c02854db5e09f0191690dc59db0afd95328c9db614a2976e088cab7c86d7e48183191258fc59dc699653508ce25bf0369d67f33d5d77839'
]
})
// db created & opened
```
### orbitdb.open(address, [options])
> Opens an OrbitDB database.
2018-08-12 11:26:28 -04:00
Returns a `Promise` that resolves to [a database instance ](#store-api ). `address` (string) should be a valid OrbitDB address. If a database name is provided instead, it will check `options.create` to determine if it should create the database. `options` is an object with any of the following properties:
2018-07-13 15:21:21 -04:00
- `localOnly` (boolean): If set to `true` , will throw an error if the database can't be found locally. (Default: `false` )
- `directory` (string): The directory where data will be stored (Default: uses directory option passed to OrbitDB constructor or `./orbitdb` if none was provided).
2018-07-13 15:44:09 -04:00
- `create` (boolean): Whether or not to create the database if a valid OrbitDB address is not provided. (Default: `false` )
2018-07-13 15:21:21 -04:00
- `type` (string): A supported database type (i.e. `eventlog` or [an added custom type ](https://github.com/orbitdb/orbit-db#custom-store-types )). Required if create is set to `true` . Otherwise it's used to validate the manifest.
- `overwrite` (boolean): Overwrite an existing database (Default: `false` )
2018-08-16 08:44:30 -04:00
- `replicate` (boolean): Replicate the database with peers, requires IPFS PubSub. (Default: `true` )
2018-07-13 15:44:09 -04:00
```javascript
const db = await orbitdb.open('/orbitdb/Qmd8TmZrWASypEp4Er9tgWP4kCNQnW4ncSnvjvyHQ3EVSU/first-database')
```
2018-07-21 09:11:03 -04:00
Convienance methods are available when opening/creating any of the default OrbitDB database types: [feed ](#orbitdbfeednameaddress ), [docs ](#orbitdbdocsnameaddress-options ), [log ](#orbitdblognameaddress ), [keyvalue ](#orbitdbkeyvaluenameaddress ), [counter ](#orbitdbcounternameaddress )
You can use: `orbitdb.feed(address, options)`
Instead of: `orbitdb.open(address, { type: 'feed', ...options })`
2018-07-13 15:30:09 -04:00
### orbitdb.disconnect()
2018-07-21 09:11:03 -04:00
>Close databases, connections, pubsub and reset orbitdb state.
2018-07-13 15:21:21 -04:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves once complete.
2018-07-13 15:21:21 -04:00
2018-07-13 15:30:09 -04:00
```javascript
await orbitdb.disconnect()
```
### orbitdb.stop()
Alias for `orbitdb.disconnect()`
```javascript
await orbitdb.stop()
```
2018-07-13 15:21:21 -04:00
2018-07-12 15:07:30 -04:00
### orbitdb.keyvalue(name|address)
2018-07-13 15:21:21 -04:00
> Creates and opens a keyvalue database
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a [`KeyValueStore` instance ](https://github.com/orbitdb/orbit-db-kvstore ).
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
```javascript
const db = await orbitdb.keyvalue('application.settings')
// Or:
const db = await orbitdb.keyvalue(anotherkvdb.address)
```
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Module: [orbit-db-kvstore ](https://github.com/orbitdb/orbit-db-kvstore )
2018-08-12 11:26:28 -04:00
**See the [Store ](#store-api ) section for details of common methods and properties.**
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
#### put(key, value)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a `String` that is the multihash of the entry.
2017-03-31 10:54:56 +02:00
```javascript
await db.put('hello', { name: 'World' })
```
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
#### set(key, value)
2018-07-21 09:11:03 -04:00
Alias for `.put()`
2016-10-05 10:33:45 +02:00
```javascript
2017-03-31 10:54:56 +02:00
await db.set('hello', { name: 'Friend' })
```
2017-12-02 06:33:30 +01:00
2017-03-31 10:54:56 +02:00
#### get(key)
2018-07-21 09:11:03 -04:00
Returns an `Object` with the contents of the entry.
2017-03-31 10:54:56 +02:00
```javascript
const value = db.get('hello')
// { name: 'Friend' }
2016-10-05 10:33:45 +02:00
```
2018-07-21 09:21:03 -04:00
### orbitdb.kvstore(name|address)
Alias for [`orbitdb.keyvalue()` ](#orbitdbkeyvaluenameaddress )
2018-07-12 15:07:30 -04:00
### orbitdb.log(name|address)
2018-07-13 15:21:21 -04:00
> Creates and opens an eventlog database
2017-09-17 16:02:07 +05:30
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a [`EventStore` instance ](https://github.com/orbitdb/orbit-db-eventstore ).
2017-09-17 16:02:07 +05:30
2017-03-31 10:54:56 +02:00
```javascript
const db = await orbitdb.eventlog('site.visitors')
// Or:
const db = await orbitdb.eventlog(anotherlogdb.address)
```
2017-09-17 16:02:07 +05:30
2018-07-21 09:11:03 -04:00
Module: [orbit-db-eventstore ](https://github.com/orbitdb/orbit-db-eventstore )
2018-08-12 11:26:28 -04:00
**See the [Store ](#store-api ) section for details of common methods and properties.**
2017-09-17 16:02:07 +05:30
2017-03-31 10:54:56 +02:00
#### add(event)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2017-03-31 10:54:56 +02:00
```javascript
const hash = await db.add({ name: 'User1' })
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### get(hash)
2018-07-21 09:11:03 -04:00
Returns an `Object` with the contents of the entry.
2017-03-31 10:54:56 +02:00
```javascript
const event = db.get(hash)
.map((e) => e.payload.value)
// { name: 'User1' }
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### iterator([options])
2017-09-17 16:02:07 +05:30
2018-07-21 09:11:03 -04:00
Returns an `Array` of `Objects` based on the `options` .
2017-03-31 10:54:56 +02:00
**options** : It is an object which supports the following properties
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`gt - (string)` Greater than, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`gte - (string)` Greater than or equal to, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`lt - (string)` Less than, takes an item's `hash` .
2017-03-31 10:54:56 +02:00
2018-04-14 10:16:00 +01:00
`lte - (string)` Less than or equal to, takes an item's `hash` value.
2017-03-30 18:43:47 +02:00
2018-04-14 10:16:00 +01:00
`limit - (integer)` Limiting the entries of result, defaults to `1` , and `-1` means all items (no limit).
2017-03-30 18:43:47 +02:00
2017-03-31 10:54:56 +02:00
`reverse - (boolean)` If set to true will result in reversing the result.
2017-03-30 18:43:47 +02:00
2018-04-14 10:16:00 +01:00
If `hash` not found when passing `gt` , `gte` , `lt` , or `lte` , the iterator will return all items (respecting `limit` and `reverse` ).
2017-03-31 10:54:56 +02:00
```javascript
const all = db.iterator({ limit: -1 })
.collect()
.map((e) => e.payload.value)
// [{ name: 'User1' }]
```
2016-10-05 10:33:45 +02:00
2018-07-21 09:21:03 -04:00
### orbitdb.eventlog(name|address)
Alias for [`orbitdb.log()` ](#orbitdblognameaddress )
2018-07-12 15:07:30 -04:00
### orbitdb.feed(name|address)
2018-07-13 15:21:21 -04:00
> Creates and opens a feed database
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a [`FeedStore` instance ](https://github.com/orbitdb/orbit-db-feedstore ).
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
```javascript
const db = await orbitdb.feed('orbit-db.issues')
// Or:
const db = await orbitdb.feed(anotherfeeddb.address)
```
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Module: [orbit-db-feedstore ](https://github.com/orbitdb/orbit-db-feedstore )
2018-08-12 11:26:28 -04:00
See the [Store ](#store-api ) section for details of common methods and properties.
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
#### add(data)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2016-10-05 10:33:45 +02:00
```javascript
2017-03-31 10:54:56 +02:00
const hash = await db.add({ name: 'User1' })
2016-10-05 10:33:45 +02:00
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### get(hash)
2018-07-21 09:11:03 -04:00
Returns an `Object` with the contents of the entry.
2017-03-31 10:54:56 +02:00
```javascript
const event = db.get(hash)
.map((e) => e.payload.value)
// { name: 'User1' }
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### remove(hash)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2017-03-31 10:54:56 +02:00
```javascript
const hash = await db.remove(hash)
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### iterator([options])
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Returns an `Array` of `Objects` based on the `options` .
2017-03-31 10:54:56 +02:00
**options** : It is an object which supports the following properties
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`gt - (string)` Greater than, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`gte - (string)` Greater than or equal to, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`lt - (string)` Less than, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`lte - (string)` Less than or equal to, takes an item's `hash` .
2017-09-17 16:02:07 +05:30
2018-04-14 10:16:00 +01:00
`limit - (integer)` Limiting the entries of result, defaults to `1` , and `-1` means all items (no limit).
2017-09-17 16:02:07 +05:30
2017-03-31 10:54:56 +02:00
`reverse - (boolean)` If set to true will result in reversing the result.
2016-10-05 10:33:45 +02:00
2018-04-14 10:16:00 +01:00
If `hash` not found when passing `gt` , `gte` , `lt` , or `lte` , the iterator will return all items (respecting `limit` and `reverse` ).
2017-03-31 10:54:56 +02:00
```javascript
const all = db.iterator({ limit: -1 })
.collect()
.map((e) => e.payload.value)
// [{ name: 'User1' }]
```
2017-03-30 18:43:47 +02:00
2018-07-12 15:07:30 -04:00
### orbitdb.docs(name|address, options)
2018-07-13 15:21:21 -04:00
> Creates and opens a docstore database
2017-03-30 18:43:47 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a [`DocumentStore` instance ](https://github.com/orbitdb/orbit-db-docstore ).
2017-03-30 18:43:47 +02:00
2017-03-31 10:54:56 +02:00
```javascript
const db = await orbitdb.docs('orbit.users.shamb0t.profile')
// Or:
const db = await orbitdb.docs(anotherdocdb.address)
```
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
By default, documents are indexed by field `_id` . You can also specify the field to index by:
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
```javascript
const db = await orbitdb.docs('orbit.users.shamb0t.profile', { indexBy: 'name' })
```
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Module: [orbit-db-docstore ](https://github.com/orbitdb/orbit-db-docstore )
2018-08-12 11:26:28 -04:00
**See the [Store ](#store-api ) section for details of common methods and properties.**
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
#### put(doc)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2017-03-31 10:54:56 +02:00
```javascript
const hash = await db.put({ _id: 'QmAwesomeIpfsHash', name: 'shamb0t', followers: 500 })
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### get(key)
2018-07-21 09:11:03 -04:00
Returns an `Array` with a single `Object` if key exists.
2017-03-31 10:54:56 +02:00
```javascript
const profile = db.get('shamb0t')
// [{ _id: 'shamb0t', name: 'shamb0t', followers: 500 }]
```
2018-07-12 13:08:41 -04:00
2017-03-31 10:54:56 +02:00
#### query(mapper)
2018-07-21 09:11:03 -04:00
Returns an `Array` of `Objects` based on the `mapper` .
2017-03-31 10:54:56 +02:00
```javascript
const all = db.query((doc) => doc.followers >= 500)
// [{ _id: 'shamb0t', name: 'shamb0t', followers: 500 }]
```
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
#### del(key)
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2016-10-10 16:09:00 -04:00
```javascript
2017-03-31 10:54:56 +02:00
const hash = await db.del('shamb0t')
2016-10-10 16:09:00 -04:00
```
2018-07-12 13:08:41 -04:00
2018-07-21 09:21:03 -04:00
### orbitdb.docstore(name|address, options)
Alias for [`orbitdb.docs()` ](#orbitdbdocsnameaddress-options )
2018-07-12 15:07:30 -04:00
### orbitdb.counter(name|address)
2018-07-13 15:21:21 -04:00
> Creates and opens a counter database
2017-03-31 10:54:56 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to a [`DocumentStore` instance ](https://github.com/orbitdb/orbit-db-docstore ).
2017-03-31 10:54:56 +02:00
```javascript
const counter = await orbitdb.counter('song_123.play_count')
// Or:
const counter = await orbitdb.counter(anothercounterdb.address)
```
2016-10-10 16:09:00 -04:00
2018-07-21 09:11:03 -04:00
Module: [orbit-db-counterstore ](https://github.com/orbitdb/orbit-db-counterstore ).
2018-08-12 11:26:28 -04:00
**See the [Store ](#store-api ) section for details of common methods and properties.**
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
#### value
2018-07-21 09:11:03 -04:00
Returns a `Number` .
2016-10-10 16:09:00 -04:00
```javascript
2017-03-31 10:54:56 +02:00
counter.value // 0
2016-10-10 16:09:00 -04:00
```
2017-03-31 10:54:56 +02:00
#### inc([value])
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves to the multihash of the entry as a `String` .
2017-03-31 10:54:56 +02:00
```javascript
await counter.inc()
counter.value // 1
await counter.inc(7)
counter.value // 8
await counter.inc(-2)
counter.value // 8
```
2018-07-12 13:08:41 -04:00
2018-07-12 15:07:30 -04:00
## Static Properties
### OrbitDB.databaseTypes
2018-07-21 09:11:03 -04:00
Returns supported database types (i.e. store types) as an `Array` of `Strings`
2018-07-12 13:08:41 -04:00
```js
OrbitDB.databaseTypes
// [ 'counter', 'eventlog', 'feed', 'docstore', 'keyvalue']
```
2018-07-12 15:07:30 -04:00
## Static Methods
2018-07-12 13:08:41 -04:00
2018-07-12 15:07:30 -04:00
### OrbitDB.isValidType(type)
2018-07-21 09:11:03 -04:00
Returns `true` if the provided `String` is a supported database type
2018-07-12 13:08:41 -04:00
```js
OrbitDB.isValidType('docstore')
// true
```
2018-07-12 15:07:30 -04:00
### OrbitDB.addDatabaseType(type, store)
2018-07-12 13:08:41 -04:00
Adds a custom database type & store to OrbitDB
```js
const CustomStore = require('./CustomStore')
OrbitDB.addDatabaseType(CustomStore.type, CustomStore)
```
2018-07-12 15:07:30 -04:00
### OrbitDB.getDatabaseTypes()
2018-07-21 09:11:03 -04:00
Returns an `Object` mapping database types to Store Classes
2018-07-12 13:08:41 -04:00
```js
OrbitDB.getDatabaseTypes()
// { counter: [Function: CounterStore],
// eventlog: [Function: EventStore],
// feed: [Function: FeedStore],
// docstore: [Function: DocumentStore],
// keyvalue: [Function: KeyValueStore] }
```
2018-07-12 15:07:30 -04:00
### OrbitDB.isValidAddress(address)
2018-07-21 09:11:03 -04:00
Returns `true` if the provided `String` is a valid OrbitDB address
2018-07-12 13:08:41 -04:00
```js
OrbitDB.isValidAddress('/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC/first-database')
// true
```
2018-07-12 15:07:30 -04:00
### OrbitDB.parseAddress(address)
2018-07-21 09:11:03 -04:00
Returns an instance of OrbitDBAddress if the provided `String` is a valid orbitdb address
2018-07-12 13:08:41 -04:00
```js
OrbitDB.parseAddress('/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC/first-database')
// OrbitDBAddress {
// root: 'Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC',
// path: 'first-database' }
```
2017-03-30 18:43:47 +02:00
2018-08-08 18:07:51 -04:00
## Store API
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
Every database (store) has the following methods available in addition to their specific methods.
2016-10-10 16:09:00 -04:00
2018-08-08 18:07:51 -04:00
### store.load([amount])
2016-10-10 16:09:00 -04:00
2018-07-18 13:42:15 -04:00
Load the locally persisted database state to memory. Use the optional `amount` argument to limit the number of entries loaded into memory, starting from the head(s) (Default: `-1` will load all entries)
2016-10-05 10:57:49 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves once complete
2017-03-31 10:54:56 +02:00
With events:
```javascript
db.events.on('ready', () => {
/* database is now ready to be queried */
})
db.load()
```
2016-10-10 16:09:00 -04:00
2017-03-31 10:54:56 +02:00
Async:
```javascript
await db.load()
/* database is now ready to be queried */
```
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### store.close()
2018-07-21 09:11:03 -04:00
> Close the database.
Returns a `Promise` that resolves once complete
2017-03-30 18:43:47 +02:00
2017-03-31 10:54:56 +02:00
Async:
```javascript
await db.close()
```
2017-03-30 18:43:47 +02:00
2018-08-08 18:07:51 -04:00
### store.drop()
2018-07-21 09:11:03 -04:00
> Remove the database locally. This does not delete any data from peers.
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Returns a `Promise` that resolves once complete
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
```javascript
await db.drop()
```
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### store.key
2016-10-05 10:57:49 +02:00
2018-07-21 09:11:03 -04:00
Returns an instance of [`KeyPair` ](https://github.com/indutny/elliptic/blob/master/lib/elliptic/ec/key.js#L8 ). The keypair is used to sign the database entries. See the [GUIDE ](https://github.com/orbitdb/orbit-db/blob/master/GUIDE.md#keys ) for more information on how OrbitDB uses the keypair.
2017-11-29 21:23:08 +01:00
```
const key = db.key
console.log(key)
2018-07-12 13:08:41 -04:00
// < Key priv: db8ef129f3d26ac5d7c17b402027488a8f4b2e7fa855c27d680b714cf9c1f87e
// pub: < EC Point x: f0e33d60f9824ce10b2c8983d3da0311933e82cf5ec9374cd82c0af699cbde5b
2017-11-29 21:23:08 +01:00
// y: ce206bfccf889465c6g6f9a7fdf452f9c3e1204a6f1b4582ec427ec12b116de9> >
```
2018-07-21 09:11:03 -04:00
The public key can be retrieved with:
2017-11-29 21:23:08 +01:00
```
console.log(db.key.getPublic('hex'))
// 04d009bd530f2fa0cda29202e1b15e97247893cb1e88601968abfe787f7ea03828fdb7624a618fd67c4c437ad7f48e670cc5a6ea2340b896e42b0c8a3e4d54aebe
```
2018-08-12 11:26:28 -04:00
The key can also be accessed from the OrbitDB instance: `orbitdb.key.getPublic('hex')` .
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### store.type
2016-10-05 10:33:45 +02:00
2018-07-21 09:11:03 -04:00
Returns the type of the database as a `String` .
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
## Store Events
2016-10-05 10:33:45 +02:00
2017-03-31 10:54:56 +02:00
Each database in `orbit-db` contains an `events` ([EventEmitter ](https://nodejs.org/api/events.html )) object that emits events that describe what's happening in the database. Events can be listened to with:
```javascript
db.events.on(name, callback)
```
2017-03-30 18:43:47 +02:00
2018-08-08 18:07:51 -04:00
### `replicated`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('replicated', (address) => ... )
```
2016-10-05 10:33:45 +02:00
2018-07-17 16:55:07 -04:00
Emitted when the database has synced with another peer. This is usually a good place to re-query the database for updated results, eg. if a value of a key was changed or if there are new events in an event log.
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### `replicate`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('replicate', (address) => ... )
```
2016-10-05 10:33:45 +02:00
2018-07-12 16:14:55 -04:00
Emitted before replicating a part of the database with a peer.
2016-10-05 10:33:45 +02:00
2018-07-12 16:23:08 -04:00
#### `replicate.progress`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('replicate.progress', (address, hash, entry, progress, have) => ... )
```
2016-10-05 10:33:45 +02:00
2018-07-12 16:14:55 -04:00
Emitted while replicating a database. *address* is id of the database that emitted the event. *hash* is the multihash of the entry that was just loaded. *entry* is the database operation entry. *progress* is the current progress. *have* is a map of database pieces we have.
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### `load`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('load', (dbname) => ... )
```
2016-10-05 10:33:45 +02:00
2018-07-12 16:14:55 -04:00
Emitted before loading the database.
2017-03-30 18:43:47 +02:00
2018-07-12 16:23:08 -04:00
#### `load.progress`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('load.progress', (address, hash, entry, progress, total) => ... )
```
2017-03-30 18:43:47 +02:00
2018-07-12 16:14:55 -04:00
Emitted while loading the local database, once for each entry. *dbname* is the name of the database that emitted the event. *hash* is the multihash of the entry that was just loaded. *entry* is the database operation entry. *progress* is a sequential number starting from 0 upon calling `load()` .
2017-03-30 18:43:47 +02:00
2018-08-08 18:07:51 -04:00
### `ready`
2018-07-12 16:14:55 -04:00
```javascript
2018-07-25 21:03:59 -04:00
db.events.on('ready', (dbname, heads) => ... )
2018-07-12 16:14:55 -04:00
```
2016-10-05 10:33:45 +02:00
2018-07-12 16:14:55 -04:00
Emitted after fully loading the local database.
2016-10-05 10:33:45 +02:00
2018-08-08 18:07:51 -04:00
### `write`
2018-07-12 16:14:55 -04:00
```javascript
db.events.on('write', (dbname, hash, entry) => ... )
```
2016-10-05 10:33:45 +02:00
2018-07-12 16:14:55 -04:00
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 added database op.
2018-07-25 21:08:31 -04:00
2018-08-08 18:07:51 -04:00
### `closed`
2018-07-25 21:08:31 -04:00
Emitted once the database has finished closing.
```javascript
db.events.on('closed', (dbname) => ... )
```