orbit-db-store
Base class for orbit-db data stores. You generally don't need to use this module if you want to use orbit-db
. This module contains shared methods between all data stores in orbit-db
and can be used as a base class for a new data model.
Used in
Requirements
API
Public methods
load()
Load the database using locally persisted state.
saveSnapshot()
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.
loadFromSnapshot(hash, onProgressCallback)
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.
close()
Uninitialize the store. Emits close
after the store has been uninitialized.
drop()
Remove the database locally. 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: <manifestHash>, path: <path> }
. Convert to a string with db.address.toString()
.
console.log(db.address)
key
Key pair used with this store to sign and access entries. This key is the peer/node/user key.
console.log(db.key.toPublic('hex'))
type
Remove all items from the local store. This doesn't remove or delete any entries in the distributed operations log.
console.log(db.type)
Events
Store has an events
(EventEmitter) object that emits events that describe what's happening in the database.
-
load
- (dbname, hash)
Emitted before loading the database history. hash is the hash from which the history is loaded from.
db.events.on('load', (id, hash) => ... )
db.load()
-
ready
- (dbname)
Emitted after fully loading the database history.
db.events.on('ready', (id) => ... )
db.load()
-
load.progress
- (id, hash, entry, progress, total)
Emitted for each entry during load.
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.
db.events.on('load', (id, hash, entry, progress, total) => ... )
db.load()
-
replicated
- (dbname)
Emitted after the database was synced with an update from a peer database.
db.events.on('replicated', (id) => ... )
-
write
- (id, hash, entry)
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.
db.events.on('write', (id, hash, entry) => ... )
Private methods
_addOperation(data)
Add an entry to the store. Takes data
as a parameter which can be of any type.
this._addOperation({
op: 'PUT',
key: 'greeting',
value: 'hello world!'
});
Creating Custom Data Stores
You can create a custom data stores that stores data in a way you need it to. To do this, you need to import orbit-db-store
to your custom store and extend your store ckass from orbit-db-store's Store
. Below is the orbit-db-kvstore
which is a custom data store for orbit-db
.
TODO: describe indices and how they work
const Store = require('orbit-db-store');
const KeyValueIndex = require('./KeyValueIndex');
class KeyValueStore extends Store {
constructor(ipfs, id, dbname, options) {
Object.assign(options || {}, { Index: KeyValueIndex });
super(ipfs, id, dbname, options)
}
get(key) {
return this._index.get(key);
}
set(key, data) {
this.put(key, data);
}
put(key, data) {
return this._addOperation({
op: 'PUT',
key: key,
value: data,
meta: {
ts: new Date().getTime()
}
});
}
del(key) {
return this._addOperation({
op: 'DEL',
key: key,
value: null,
meta: {
ts: new Date().getTime()
}
});
}
}
module.exports = KeyValueStore;
Contributing
See orbit-db's contributing guideline.
License
MIT ©️ 2016 Haadcode