all repos — snow-editor @ 65993a713586363acceab129af67161a5fbe71cc

small and cozy markdown, and orgmode editor

backend/src/versionUtils.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
import { MAX_VERSIONS_PER_DOCUMENT } from './messages.js';
import { newId } from './utils.js';

export function saveDocumentVersion(db, doc, createdAt) {
  const versionId = newId();
  db.prepare(
    `INSERT INTO document_versions (id, document_id, title, mode, content, created_at)
     VALUES (?, ?, ?, ?, ?, ?)`,
  ).run(versionId, doc.id, doc.title, doc.mode, doc.content, createdAt);

  const excess = db
    .prepare(
      `SELECT id FROM document_versions
       WHERE document_id = ?
       ORDER BY created_at DESC
       LIMIT -1 OFFSET ?`,
    )
    .all(doc.id, MAX_VERSIONS_PER_DOCUMENT);

  if (excess.length > 0) {
    const placeholders = excess.map(() => '?').join(',');
    db.prepare(`DELETE FROM document_versions WHERE id IN (${placeholders})`).run(
      ...excess.map((row) => row.id),
    );
  }
}