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}`);
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.
fixSet 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.
fixCheck 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.
fixEnsure import { MongodbPersistence } from 'y-mongodb' or const { MongodbPersistence } = require('y-mongodb'). Audit
Dependencies
yjsrequiredPeer dependency; y-mongodb is a persistence adapter for Yjs documents.