/** The caller must authorize documentId before calling this function. */ export async function publishIfCurrent( pool, documentId, sourceVersion, output, ) { const result = await pool.query( `UPDATE contracts SET content = $1, version = version + 1 WHERE id = $2 AND version = $3 RETURNING version::text AS version`, [Buffer.from(output), documentId, sourceVersion], ); if (result.rowCount !== 1) { throw new Error("The document changed or was removed. Review it again."); } return result.rows[0].version; }