83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
"use strict";
|
|
/**
|
|
* ScheduleRepository — CRUD for schedules table.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ScheduleRepository = void 0;
|
|
function rowToRecord(row) {
|
|
return {
|
|
id: row.id,
|
|
name: row.name,
|
|
url: row.url,
|
|
configJson: row.config_json,
|
|
cronExpression: row.cron_expression,
|
|
enabled: row.enabled === 1,
|
|
lastRunAt: row.last_run_at,
|
|
nextRunAt: row.next_run_at,
|
|
createdAt: row.created_at,
|
|
};
|
|
}
|
|
class ScheduleRepository {
|
|
constructor(db) {
|
|
this.db = db;
|
|
}
|
|
create(params) {
|
|
this.db
|
|
.prepare(`INSERT INTO schedules (id, name, url, config_json, cron_expression, enabled, next_run_at, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
.run(params.id, params.name, params.url, params.configJson, params.cronExpression, params.enabled !== false ? 1 : 0, params.nextRunAt ?? null, Date.now());
|
|
}
|
|
findById(id) {
|
|
const row = this.db
|
|
.prepare('SELECT * FROM schedules WHERE id = ?')
|
|
.get(id);
|
|
return row ? rowToRecord(row) : undefined;
|
|
}
|
|
findAll(enabledOnly = false) {
|
|
const rows = enabledOnly
|
|
? this.db.prepare('SELECT * FROM schedules WHERE enabled = 1 ORDER BY created_at DESC').all()
|
|
: this.db.prepare('SELECT * FROM schedules ORDER BY created_at DESC').all();
|
|
return rows.map(rowToRecord);
|
|
}
|
|
update(id, fields) {
|
|
const sets = [];
|
|
const values = [];
|
|
if (fields.name !== undefined) {
|
|
sets.push('name = ?');
|
|
values.push(fields.name);
|
|
}
|
|
if (fields.url !== undefined) {
|
|
sets.push('url = ?');
|
|
values.push(fields.url);
|
|
}
|
|
if (fields.configJson !== undefined) {
|
|
sets.push('config_json = ?');
|
|
values.push(fields.configJson);
|
|
}
|
|
if (fields.cronExpression !== undefined) {
|
|
sets.push('cron_expression = ?');
|
|
values.push(fields.cronExpression);
|
|
}
|
|
if (fields.enabled !== undefined) {
|
|
sets.push('enabled = ?');
|
|
values.push(fields.enabled ? 1 : 0);
|
|
}
|
|
if (fields.lastRunAt !== undefined) {
|
|
sets.push('last_run_at = ?');
|
|
values.push(fields.lastRunAt);
|
|
}
|
|
if (fields.nextRunAt !== undefined) {
|
|
sets.push('next_run_at = ?');
|
|
values.push(fields.nextRunAt);
|
|
}
|
|
if (sets.length === 0)
|
|
return;
|
|
values.push(id);
|
|
this.db.prepare(`UPDATE schedules SET ${sets.join(', ')} WHERE id = ?`).run(...values);
|
|
}
|
|
delete(id) {
|
|
this.db.prepare('DELETE FROM schedules WHERE id = ?').run(id);
|
|
}
|
|
}
|
|
exports.ScheduleRepository = ScheduleRepository;
|