mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
Pre release (#85)
* docs: Update README to match new version. * docs: Update events example to use new API. * docs: Correctly print out db query results. * test: Remove concurrent. * test: Remove unimplemented and 3rd party AC tests. * test: Remove unimplemented and 3rd party identity tests. * docs: Move jsdoc config to conf directory. * Point package.json main at index.js to access all exported functions. * docs: Vetted AC docs; these examples should work if implemented in code. Explicitly show orbit-db function imports. * docs: Fix incorrectly declared write objects. * docs: Improved canAppend documentation. Better JS syntax highlighting. * docs: wss and define filters for localhost separately. * docs: Simplified webSockets implementation with filters. * docs: Return manifest json only (no hash). JS highlighting. * docs: Remove operations documentation. * docs: Update heading levels. * docs: Differentiate between db types which expose put/add function. * docs: Correctly import IPFS and pass config. * docs: A simple method for full db replication. * docs: Link to existing examples of db implementation. * docs: Update heading. * docs: JS code formatting. import statements. * docs: Expand on the concepts of identities and identity management. * docs: Describe head sync-ing and full replication. * docs: Comprehensive explanation of setting up a db and sync-ing/replicating data across peers. Examples can be run in node.js. * docs: Syntax highlighting. Correct code implementation for custom/3rd party storage implementations. * docs: Getting started cleanup. * docs: Manifest as an IPLD data strcture.
This commit is contained in:
@@ -3,36 +3,111 @@
|
||||
Below is a simple replication example. Both peers run within the same Node daemon.
|
||||
|
||||
```
|
||||
const waitFor = async (valueA, toBeValueB, pollInterval = 100) => {
|
||||
return new Promise((resolve) => {
|
||||
const interval = setInterval(async () => {
|
||||
if (await valueA() === await toBeValueB()) {
|
||||
clearInterval(interval)
|
||||
resolve()
|
||||
}
|
||||
}, pollInterval)
|
||||
})
|
||||
import { OrbitDB } from 'orbit-db'
|
||||
import { create } from 'ipfs-core'
|
||||
|
||||
// The config will set up a TCP connection when dialling other node.js peers.
|
||||
// You can find out more about peer connectivity at https://connectivity.libp2p.io/.
|
||||
const config1 = {
|
||||
Addresses: {
|
||||
API: '/ip4/127.0.0.1/tcp/0',
|
||||
Swarm: ['/ip4/0.0.0.0/tcp/0'],
|
||||
Gateway: '/ip4/0.0.0.0/tcp/0'
|
||||
},
|
||||
Bootstrap: [],
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: true,
|
||||
Interval: 0
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let connected1 = false
|
||||
let connected2 = false
|
||||
|
||||
const onConnected1 = async (peerId, heads) => {
|
||||
connected1 = true
|
||||
const config2 = {
|
||||
Addresses: {
|
||||
API: '/ip4/127.0.0.1/tcp/0',
|
||||
Swarm: ['/ip4/0.0.0.0/tcp/0'],
|
||||
Gateway: '/ip4/0.0.0.0/tcp/0'
|
||||
},
|
||||
Bootstrap: [],
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: true,
|
||||
Interval: 0
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const onConnected2 = async (peerId, heads) => {
|
||||
connected2 = true
|
||||
}
|
||||
const ipfs1 = await create({ config: config1, repo: './ipfs/1' })
|
||||
const ipfs2 = await create({ config: config2, repo: './ipfs/2' })
|
||||
|
||||
db1.events.on('join', onConnected1)
|
||||
db2.events.on('join', onConnected2)
|
||||
// The decentralized nature if IPFS can make it slow for peers to find one
|
||||
// another. You can speed up a connection between two peers by "dialling-in"
|
||||
// to one peer from another.
|
||||
// const ipfs1PeerId = await ipfs1.id()
|
||||
// await ipfs2.swarm.connect(ipfs1PeerId.id)
|
||||
|
||||
await db1.put({ _id: 1, msg: 'record 1 on db 1' })
|
||||
await db2.put({ _id: 2, msg: 'record 2 on db 2' })
|
||||
await db1.put({ _id: 3, msg: 'record 3 on db 1' })
|
||||
await db2.put({ _id: 4, msg: 'record 4 on db 2' })
|
||||
const orbitdb1 = await OrbitDB({ ipfs: ipfs1, id: 'userA', directory: './orbitdb/1' })
|
||||
const orbitdb2 = await OrbitDB({ ipfs: ipfs2, id: 'userB', directory: './orbitdb/2' })
|
||||
|
||||
await waitFor(() => connected1, () => true)
|
||||
await waitFor(() => connected2, () => true)
|
||||
// This opens a new db. Default db type will be 'events'.
|
||||
const db1 = await orbitdb1.open('my-db')
|
||||
|
||||
// We connect to the first db using its address. This initiates a
|
||||
// synchronization of the heads between db1 and db2.
|
||||
const db2 = await orbitdb2.open(db1.address)
|
||||
|
||||
// We write some data to db1. This will not be replicated on db2 until we
|
||||
// explicitly request these records using db2's iterator or all() convenience
|
||||
// function.
|
||||
await db1.add('hello world 1')
|
||||
await db1.add('hello world 2')
|
||||
await db1.add('hello world 3')
|
||||
await db1.add('hello world 4')
|
||||
|
||||
let db2Updated = false
|
||||
|
||||
// Listen for the connection of ipfs1 to ipfs2.
|
||||
// If we want to listen for connections from ipfs2 to ipfs1, add a "join"
|
||||
// listener to db1.
|
||||
db2.events.on('join', async (peerId, heads) => {
|
||||
// The peerId of the ipfs1 node.
|
||||
console.log(peerId, (await ipfs1.id()).id)
|
||||
})
|
||||
|
||||
// Listen for any updates to db2. This is especially useful when listening for
|
||||
// new heads that are available on db1.
|
||||
// If we want to listen for new data on db2, add an "update" listener to db1.
|
||||
db2.events.on('update', async (entry) => {
|
||||
// Full replication is achieved by explicitly retrieving all records from db1.
|
||||
console.log(await db2.all())
|
||||
db2Updated = true
|
||||
})
|
||||
|
||||
// wait for db2 to complete updating.
|
||||
await new Promise((resolve, reject) => {
|
||||
setInterval(() => {
|
||||
if (db2Updated) {
|
||||
resolve()
|
||||
}
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
// Close db1 and its underlying ipfs peer.
|
||||
await db1.close()
|
||||
await orbitdb1.stop()
|
||||
await ipfs1.stop()
|
||||
|
||||
// Close db2 and its underlying ipfs peer.
|
||||
await db2.close()
|
||||
await orbitdb2.stop()
|
||||
await ipfs2.stop()
|
||||
```
|
||||
|
||||
Refer to the API for more information about [OrbitDB's synchronization protocol](https://orbitdb.org/api/module-Sync.html).
|
||||
|
||||
Reference in New Issue
Block a user