Registry / database / y-mongodb

y-mongodb

JSON →
library0.1.11jsnpmunverified

A MongoDB database adapter for Yjs, providing persistent storage for collaborative editing applications using y-websocket. This package is a fork of Y-leveldb and is not officially maintained by the Yjs team. Current stable version is 0.1.11, with infrequent releases. It requires MongoDB as a backend and offers a simple API for storing and retrieving Yjs documents. The package ships TypeScript types and is designed to be used in Node.js environments.

npm install y-mongodb
INSTALL
IMPORT
SIG · Y-MONGODB
Y
y-mongodb
databasejavascriptv0.1.11
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongodbPersistence
import { MongodbPersistence } from 'y-mongodb'
const MongodbPersistence = require('y-mongodb')
The package is primarily used with CommonJS in the examples, but TypeScript types support ESM imports.
MongodbPersistence
const { MongodbPersistence } = require('y-mongodb')
const MongodbPersistence = require('y-mongodb').MongodbPersistence
CommonJS destructured import is the documented pattern.
default
import yMongodb from 'y-mongodb'
import { default as yMongodb } from 'y-mongodb'
No default export exists; only named export MongodbPersistence.

Shows setting up a y-websocket server with MongoDB persistence using y-mongodb.

import http from 'http'; import WebSocket from 'ws'; import * as Y from 'yjs'; import { MongodbPersistence } from 'y-mongodb'; import * as utils from 'y-websocket/bin/utils.js'; const mongoUri = process.env.MONGODB_URI ?? 'mongodb://localhost:27017'; const collection = 'yjs-transactions'; const persistence = new MongodbPersistence(mongoUri, collection); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('okay'); }); const wss = new WebSocket.Server({ noServer: true }); wss.on('connection', utils.setupWSConnection); server.on('upgrade', (request, socket, head) => { wss.handleUpgrade(request, socket, head, (ws) => { wss.emit('connection', ws, request); }); }); utils.setPersistence({ bindState: async (docName, ydoc) => { if (!ydoc) return; const persistedYdoc = await persistence.getYDoc(docName); const newUpdates = Y.encodeStateAsUpdate(ydoc); await persistence.storeUpdate(docName, newUpdates); Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc)); ydoc.on('update', update => { persistence.storeUpdate(docName, update); }); }, writeState: async (docName, ydoc) => { await persistence.storeUpdate(docName, Y.encodeStateAsUpdate(ydoc)); return new Promise(resolve => resolve()); } }); const port = process.env.PORT ?? 8080; server.listen(port); console.log(`Listening on port ${port}`);
Debug
Known issues
gotchaMongoDB connection string must be provided via environment variable MONGODB_URI or passed directly.
fix
Set process.env.MONGODB_URI or pass uri as first argument to new MongodbPersistence(uri, collection).
affects: >=0.0.0
gotchaThe package is a fork of Y-leveldb and not officially maintained; expect limited support and infrequent updates.
fix
Consider using official Yjs persistence adapters like y-leveldb or y-websql for production use.
affects: >=0.0.0
gotchawriteState in persistence callback must resolve after document is fully saved; the example shows immediate resolve which may cause data loss.
fix
Await persistence.storeUpdate in writeState before resolving.
affects: >=0.0.0
gotchaThe package uses CommonJS require in examples; TypeScript imports may not work without esModuleInterop.
fix
Enable esModuleInterop or use * as syntax for default exports.
affects: >=0.0.0
Errors
Common errors & fixes
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
Missing MONGODB_URI environment variable or undefined passed to constructor.
fix
Set MONGODB_URI environment variable or pass a valid MongoDB connection string to new MongodbPersistence(uri, collection).
TypeError: persistedYdoc is not a Y.Doc
getYDoc returned undefined or null because document not found in database.
fix
Check that document exists before applying update. Use Y.utils.applyUpdate only on valid Y.Doc objects.
ReferenceError: MongodbPersistence is not defined
Forgetting to import or require the class correctly.
fix
Ensure import { MongodbPersistence } from 'y-mongodb' or const { MongodbPersistence } = require('y-mongodb').
Upgrade
Version history
0.1.11latest on npm
Audit
Dependencies
yjsrequiredPeer dependency; y-mongodb is a persistence adapter for Yjs documents.
Agent activity
26 hits · last 30 days
node
20
Meta
1
OpenAI (training)
1
Resources
y-mongodb — npm install y-mongodb · libregistry