summaryrefslogtreecommitdiff
path: root/nodejs/database/agents.js
blob: 8b7203befa913f8c6a8730300c767f35c4f0c0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import db from './db.js';

const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`);
const getAgentStatement = db.prepare(`SELECT data FROM agents;`);
const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`);

export function addAgent(agent) {
	return addAgentStatement.run(JSON.stringify(agent)).lastInsertRowid;
}

export function getAgent() {
	const data = getAgentStatement.get();
	if (data === undefined) {
		return null;
	}
	return JSON.parse(data.data);
}

export function setAgent(agent) {
	return setAgentStatement.run(JSON.stringify(agent)).changes;
}