Files
Void-Homelab/lib/db/repos/source_docs.js
root e558be49a9 feat(jobs): repo-level embed triggers (pages/refs/source_docs)
create/update on embeddable repos enqueue embed.text with a singleton
key that coalesces rapid edits. No-op when the queue is not running
(server tests construct createApp without booting pg-boss).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 03:48:03 +10:00

57 lines
1.9 KiB
JavaScript

import { pool } from '../pool.js';
import { recordAudit } from './audit_stub.js';
import { triggerEmbed } from '../../jobs/triggers.js';
const FIELDS = ['resource_id','name','upstream_url','version','format','sync_source','local_path','body_text','embedding','last_synced','metadata'];
export async function create(input, actor) {
const cols = [], vals = [], ph = [];
let i = 1;
for (const f of FIELDS) {
if (input[f] !== undefined) { cols.push(f); vals.push(input[f]); ph.push(`$${i++}`); }
}
const { rows: [r] } = await pool.query(
`INSERT INTO source_docs(${cols.join(',')}) VALUES(${ph.join(',')}) RETURNING *`,
vals
);
await recordAudit(actor, 'create', 'source_doc', r.id, null, r);
await triggerEmbed('source_doc', r.id);
return r;
}
export async function getById(id) {
const { rows: [r] } = await pool.query(`SELECT * FROM source_docs WHERE id=$1`, [id]);
return r;
}
export async function listByResource(resource_id) {
const { rows } = await pool.query(
`SELECT * FROM source_docs WHERE resource_id=$1 ORDER BY name`, [resource_id]
);
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 source_docs SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
vals
);
await recordAudit(actor, 'update', 'source_doc', id, before, r);
if (patch.embedding === undefined) await triggerEmbed('source_doc', id);
return r;
}
export async function del(id, actor) {
const before = await getById(id);
await pool.query(`DELETE FROM source_docs WHERE id=$1`, [id]);
await recordAudit(actor, 'delete', 'source_doc', id, before, null);
}