Zoom image will be displayed
GenosDB is an embedded graph database built for web environments, featuring native P2P support, local persistence, and real-time reactive querying.
- Directed Graph: Nodes (data) and directed edges (relationships).
- Flexible Nodes: Each node has an id, a serializable value object, and an HLC timestamp.
- Full CRUD:
- put(data, id?): Inserts or updates a node.
- get(id): Retrieves a node by its ID.
- remove(id): Deletes a node and its links.
- link(sourceId, targetId): Creates a directed edge.
- clear(): Wipes all local data.
- map(…args):
- Supports MongoDB-style query, sorting, pagination, and real-time mode.
- Returns { results, unsubscribe }.
- Callback receives events with { id, value, action, edges, timestamp }.
- Event types: ‘initial’, ‘added’, ‘updated’, ‘removed’.
- Hybrid Logical Clocks (HLC):
- Combines physical time + logical counter for causal ordering.
- Last-Write-Wins (LWW):
- Conflict resolution favors the newest HLC.
- Prevents abuse via overly-future timestamps.
- Clock Sync: Local HLC is updated on remote operations to maintain causality.
- P2P via WebRTC:
- Direct client-to-client sync using room.
- Events: onPeerJoin, onPeerLeave, onPeerStream.
- Cross-tab Sync:
- Uses BroadcastChannel for intra-browser consistency.
- Automatically recovers from network loss or tab inactivity.
- Primary: OPFS (Origin Private File System):
- Uses createSyncAccessHandle if available (sync mode).
- Falls back to createWritable or IndexedDB as needed.
- Dedicated Web Worker:
- Offloads all file I/O.
- File-level locking and write queuing prevent race conditions.
- MessagePack: Compact binary serialization.
- Pako (Deflate): Compresses data for optimized storage and transfer.
- Optional encryption via password in constructor.
- Optional room protection with password (Trystero-based).
- Extensible Security Manager (experimental): Validates remote operations for custom authorization (e.g. RBAC).
- Browser-native: No server required.
- Promise-based API: Modern async flow.
- Network & Visibility Aware: Reacts to tab visibility and connectivity.
GenosDB is ideal for decentralized, real-time web apps requiring local-first data, peer-to-peer sync, and reactive querying without server dependencies.
⸻
⚙️ Technical Features of GenosDB (GDB)
GenosDB (GDB) is a minimalist, graph-oriented database with P2P support and real-time querying. Ideal for decentralized, collaborative applications requiring intensive local and remote sync.
📦 Installation and Import
NPM:
npm install genosdbimport { GDB } from "genosdb"CDN:
<script type="module">import { GDB } from "https://cdn.jsdelivr.net/npm/genosdb/+esm"
</script>
🧱 Instantiation
const db = new GDB("my-db") // no passwordconst secureDb = new GDB("secure-db", { password: "secret" }) // optional encryption
🔧 Core Methods
put(data, id?) → Inserts or updates a node.
const id = await db.put({ type: "User", name: "Ana" })await db.put({ name: "Ana B" }, id)
get(id) → Retrieves a node by ID.
const { result } = await db.get(id)link(sourceId, targetId) → Creates directed relationships.
await db.link(sourceId, targetId)remove(id) → Deletes nodes and references.
await db.remove(id)map(options?, callback?) → Real-time dynamic querying.
const { unsubscribe } = await db.map(({ id, value, action }) => {if (action === "added") console.log("New node:", value)
})
clear() → Deletes all data.
await db.clear()🔗 P2P Functions (Room)
Listen to remote actions:
const [send, recv] = room.makeAction("cursormove")recv(([x, y], peerId) => moveCursor(peerId, x, y))
Network events:
db.room.onPeerJoin(id => console.log(id + " joined"))db.room.onPeerLeave(id => console.log(id + " left"))
db.room.onPeerStream((stream, id) => assignStream(id, stream))
⸻
GENOSDB
This article is part of the official documentation of GenosDB (GDB), a distributed, modular P2P graph database developed by Esteban Fuster Pozzi (estebanrfp).
🔍 Learn more: https://github.com/estebanrfp/gdb/wiki/GDB-API-Reference
📖 Full documentation: https://github.com/estebanrfp/gdb/wiki
📦 Install GenosDB via npm: https://www.npmjs.com/package/genosdb