43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
// Generated, do not edit. sha256:051b73ea4c90cd1dfd7110d9d56e91cb527361b375f6b09d6509f8db81b2835e
|
|||
|
|
import * as db from '../db.js';
|
||
|
|
import { CONFIG } from '../../shared/config.js';
|
||
|
|
import { deleteValuation } from './valuations.js';
|
||
|
|
import { deleteProposal } from './proposals.js';
|
||
|
|
import { unlinkTxEvent } from './tx-links.js';
|
||
|
|
|
||
|
|
export function tableColumns(name) {
|
||
|
|
if (!CONFIG.DB_TABLES.includes(name)) throw new Error(`Unknown table: ${name}`);
|
||
|
|
return db.all(`PRAGMA table_info(${name})`).map(c => c.name);
|
||
|
|
}
|
||
|
|
export function listTableRaw(name) {
|
||
|
|
if (!CONFIG.DB_TABLES.includes(name)) throw new Error(`Unknown table: ${name}`);
|
||
|
|
return db.all(`SELECT * FROM ${name} ORDER BY rowid DESC`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const TABLE_ROW_DELETERS = {
|
||
|
|
valuations: (id) => deleteValuation(id),
|
||
|
|
reconcile_proposals: (id) => deleteProposal(id),
|
||
|
|
tx_event_link: (id) => {
|
||
|
|
const r = db.one(`SELECT walletTransactionId, eventId FROM WalletTransactionEventLink WHERE id=?`, [id]);
|
||
|
|
if (!r) throw new Error(`Link ${id} does not exist.`);
|
||
|
|
return unlinkTxEvent(r.walletTransactionId, r.eventId);
|
||
|
|
},
|
||
|
|
};
|
||
|
|
export async function deleteTableRow(name, id) {
|
||
|
|
if (!CONFIG.DB_DELETABLE_TABLES.includes(name)) throw new Error(`Table ${name} is not deletable.`);
|
||
|
|
const custom = TABLE_ROW_DELETERS[name];
|
||
|
|
if (custom) { await custom(id); return; }
|
||
|
|
db.run(`DELETE FROM ${name} WHERE id=?`, [id]);
|
||
|
|
await db.persist();
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function clearTable(name) {
|
||
|
|
if (!CONFIG.DB_DELETABLE_TABLES.includes(name)) throw new Error(`Table ${name} cannot be cleared.`);
|
||
|
|
const custom = TABLE_ROW_DELETERS[name];
|
||
|
|
if (custom) {
|
||
|
|
for (const r of db.all(`SELECT id FROM ${name}`)) await custom(r.id);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
db.run(`DELETE FROM ${name}`);
|
||
|
|
await db.persist();
|
||
|
|
}
|