mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
OrbitDB API - v1.0
OrbitDB is a serverless, distributed, peer-to-peer database. OrbitDB uses IPFS as its data storage and Libp2p Pubsub to automatically sync databases with peers. It's an eventually consistent database that uses Merkle-CRDTs for conflict-free database writes and merges making OrbitDB an excellent choice for p2p and decentralized apps, blockchain applications and local first web applications.
To install OrbitDB:
npm install @orbitdb/core
IPFS is also required:
npm install ipfs-core
Instantiate OrbitDB and create a database:
import { create } from 'ipfs-core'
import { createOrbitDB } from '@orbitdb/core'
const ipfs = await create() // IPFS is required for storage and network communication
const orbitdb = await createOrbitDB({ ipfs })
const mydb = await orbitdb.open('mydb')
console.log(mydb.address) // /orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13
await mydb.add("hello world!")
Open and replicate an existing database:
// In another process
import { create } from 'ipfs-core'
import { createOrbitDB } from '@orbitdb/core'
const ipfs = await create()
const orbitdb = await createOrbitDB({ ipfs })
const theirdb = await orbitdb.open('/orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13')
for await (let record of theirdb.iterator()) {
console.log(record)
}
See the OrbitDB module for more information about how to open databases.