feat(repos): pages with auto-revisions, refs with upsertByExternal

This commit is contained in:
root
2026-05-31 02:17:01 +10:00
parent 652f7c3894
commit c891c495bb
4 changed files with 244 additions and 0 deletions

94
lib/db/repos/pages.js Normal file
View File

@@ -0,0 +1,94 @@
import { pool } from '../pool.js';
import { recordAudit } from './audit_stub.js';
async function snapshot(client, page_id, body_md, edited_by) {
await client.query(
`INSERT INTO page_revisions(page_id, body_md, edited_by)
VALUES($1,$2,$3)`,
[page_id, body_md, edited_by || null]
);
}
export async function create({ space_id, slug, title, body_md = '', parent_id }, actor) {
const client = await pool.connect();
try {
await client.query('BEGIN');
const { rows: [r] } = await client.query(
`INSERT INTO pages(space_id, slug, title, body_md, parent_id)
VALUES($1,$2,$3,$4,$5) RETURNING *`,
[space_id, slug, title, body_md, parent_id || null]
);
await snapshot(client, r.id, body_md, actor?.kind);
await client.query('COMMIT');
await recordAudit(actor, 'create', 'page', r.id, null, r);
return r;
} catch (e) {
await client.query('ROLLBACK'); throw e;
} finally {
client.release();
}
}
export async function getById(id) {
const { rows: [r] } = await pool.query(`SELECT * FROM pages WHERE id=$1`, [id]);
return r;
}
export async function getBySlug(space_id, slug) {
const { rows: [r] } = await pool.query(
`SELECT * FROM pages WHERE space_id=$1 AND slug=$2`, [space_id, slug]
);
return r;
}
export async function listBySpace(space_id) {
const { rows } = await pool.query(
`SELECT id, space_id, slug, title, parent_id, updated_at
FROM pages WHERE space_id=$1 ORDER BY title`, [space_id]
);
return rows;
}
export async function listRevisions(page_id) {
const { rows } = await pool.query(
`SELECT * FROM page_revisions WHERE page_id=$1 ORDER BY created_at DESC`,
[page_id]
);
return rows;
}
export async function update(id, patch, actor) {
const before = await getById(id);
const client = await pool.connect();
try {
await client.query('BEGIN');
const fields = ['slug','title','body_md','body_html','parent_id','embedding'];
const sets = [], vals = [];
let i = 1;
for (const f of fields) {
if (patch[f] !== undefined) { sets.push(`${f}=$${i++}`); vals.push(patch[f]); }
}
sets.push(`updated_at=now()`);
vals.push(id);
const { rows: [r] } = await client.query(
`UPDATE pages SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
vals
);
if (patch.body_md !== undefined && patch.body_md !== before.body_md) {
await snapshot(client, id, patch.body_md, actor?.kind);
}
await client.query('COMMIT');
await recordAudit(actor, 'update', 'page', id, before, r);
return r;
} catch (e) {
await client.query('ROLLBACK'); throw e;
} finally {
client.release();
}
}
export async function del(id, actor) {
const before = await getById(id);
await pool.query(`DELETE FROM pages WHERE id=$1`, [id]);
await recordAudit(actor, 'delete', 'page', id, before, null);
}

82
lib/db/repos/refs.js Normal file
View File

@@ -0,0 +1,82 @@
import { pool } from '../pool.js';
import { recordAudit } from './audit_stub.js';
const FIELDS = [
'space_id','kind','source_url','title','description','summary',
'body_text','blob_path','thumbnail','metadata','embedding','status',
'source_kind','external_id','captured_at'
];
export async function create(input, actor) {
const cols = [], vals = [], placeholders = [];
let i = 1;
for (const f of FIELDS) {
if (input[f] !== undefined) {
cols.push(f); vals.push(input[f]); placeholders.push(`$${i++}`);
}
}
const { rows: [r] } = await pool.query(
`INSERT INTO refs(${cols.join(',')})
VALUES(${placeholders.join(',')}) RETURNING *`,
vals
);
await recordAudit(actor, 'create', 'ref', r.id, null, r);
return r;
}
export async function getById(id) {
const { rows: [r] } = await pool.query(`SELECT * FROM refs WHERE id=$1`, [id]);
return r;
}
export async function upsertByExternal(input, actor) {
const { source_kind, external_id } = input;
if (!source_kind || !external_id) {
throw new Error('upsertByExternal requires source_kind + external_id');
}
const { rows: [existing] } = await pool.query(
`SELECT * FROM refs WHERE source_kind=$1 AND external_id=$2`,
[source_kind, external_id]
);
if (existing) {
return update(existing.id, input, actor);
}
return create(input, actor);
}
export async function list({ space_id, kind, limit = 100, offset = 0 } = {}) {
const where = [], vals = [];
let i = 1;
if (space_id) { where.push(`space_id=$${i++}`); vals.push(space_id); }
if (kind) { where.push(`kind=$${i++}`); vals.push(kind); }
const w = where.length ? `WHERE ${where.join(' AND ')}` : '';
vals.push(limit, offset);
const { rows } = await pool.query(
`SELECT * FROM refs ${w} ORDER BY captured_at DESC LIMIT $${i++} OFFSET $${i}`,
vals
);
return rows;
}
export async function update(id, patch, actor) {
const before = await getById(id);
const sets = [], vals = [];
let i = 1;
for (const f of FIELDS) {
if (patch[f] !== undefined) { sets.push(`${f}=$${i++}`); vals.push(patch[f]); }
}
sets.push(`updated_at=now()`);
vals.push(id);
const { rows: [r] } = await pool.query(
`UPDATE refs SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
vals
);
await recordAudit(actor, 'update', 'ref', id, before, r);
return r;
}
export async function del(id, actor) {
const before = await getById(id);
await pool.query(`DELETE FROM refs WHERE id=$1`, [id]);
await recordAudit(actor, 'delete', 'ref', id, before, null);
}