When user edit the Yjs doc, I stored a snapshot every 500 versions. This is how I store the snapshot:
async storeSnapshot(syncFileAttr: SyncFileAttr, doc: Y.Doc) {
if (typeof window !== "undefined" || !this.pool) {
return;
}
try {
const latestSnapshot = await getFileLatestSnapshot(syncFileAttr.docName);
const latestClock = await getCurrentUpdateClock(syncFileAttr.docName);
if (!latestSnapshot || (latestClock - latestSnapshot.clock > 500)) {
const snapshot = Y.snapshot(doc);
const encoded = Y.encodeSnapshot(snapshot);
const client = await this.pool.connect();
try {
await client.query('BEGIN');
const key = `snapshot_${syncFileAttr.docName}_${Date.now()}`;
await client.query(
`INSERT INTO tex_sync_history
(key, value, version, content_type, doc_name, clock, source, project_id, created_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW())`,
[
key,
encoded,
'1.0', // version
'snapshot',
syncFileAttr.docName,
latestClock,
'system',
syncFileAttr.projectId
]
);
await client.query('COMMIT');
} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
}
} catch (error) {
logger.error('Failed to store snapshot:', error);
throw error;
}
}
I want to show the version list from the latest snapshot (latest snapshot + recent update). If I use the full version, this should be too many versions, or missing the history version after Yjs merge the legacy version. I am trying to restore the document from snapshot like this(when get the base document, I can apply the next 1-10 update to get the recent doc versions, user can check the history version and roll back to history):
let currentDoc = new Y.Doc({ gc: false });
let newDoc = new Y.Doc({ gc: false });
if (latestSnapshot.value) {
try {
const snapshot = Y.decodeSnapshot(latestSnapshot.value);
let newDoc = Y.createDocFromSnapshot(currentDoc, snapshot);
} catch (error) {
logger.error(`Failed to apply snapshot for file ${fileId}:`, error);
return [];
}
}
shows error:
│ [2025-06-02T06:51:04.109] [ERROR] default - Failed to apply snapshot for file 27346efefc134eb4bb479cc61eb594dd: TypeError: Cannot read properties of undefined (rea │
│ at findIndexSS (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2910:22) │
│ at file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2783:31 │
│ at transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:3452:14) │
│ at Doc.transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:545:12) │
│ at Module.createDocFromSnapshot (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2766:13) │
│ at calcFileVersion (file:///home/node/app/dist/service/version_service.js:111:32) │
│ at process.processTicksAndRejections (node:internal/process/task_queues:95:5) │
│ at async file:///home/node/app/dist/controllers/doc/doc_controller.js:21:20
I have make a minimal example:
const testSnapshot = () => {
const ydoc = new Y.Doc({ gc: false });
ydoc.getText().insert(0, "world!");
const snapshot = Y.snapshot(ydoc);
ydoc.getText().insert(0, "hello ");
const restored = Y.createDocFromSnapshot(ydoc, snapshot);
console.log(restored.getText().toString());
};
testSnapshot();
it look like the document should be the original document. but I want to restore the specify version from snapshot and I could not get the original document from context. is it possible to restore the specify version from snapshot. it is hard to make a minimal reproduce example, the workflow like this: user edit doc->generate snapshot and store to database every 500 versions. get the latest snapshot and update, calculate the history version from base snapshot.