fase(19): scheduling module refactor
This commit is contained in:
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.KyselyScheduleRepository = void 0;
|
||||
const Schedule_1 = require("../../domain/entities/Schedule");
|
||||
class KyselyScheduleRepository {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
async save(schedule) {
|
||||
await this.db
|
||||
.insertInto('schedules')
|
||||
.values({
|
||||
id: schedule.id.toString(),
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
created_at: schedule.createdAt,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
async findById(id) {
|
||||
const row = await this.db
|
||||
.selectFrom('schedules')
|
||||
.selectAll()
|
||||
.where('id', '=', id.toString())
|
||||
.executeTakeFirst();
|
||||
if (!row)
|
||||
return null;
|
||||
return this.toEntity(row);
|
||||
}
|
||||
async findAll(enabledOnly = false) {
|
||||
let query = this.db.selectFrom('schedules').selectAll().orderBy('created_at', 'desc');
|
||||
if (enabledOnly) {
|
||||
query = query.where('enabled', '=', 1);
|
||||
}
|
||||
const rows = await query.execute();
|
||||
return rows.map((r) => this.toEntity(r));
|
||||
}
|
||||
async update(schedule) {
|
||||
await this.db
|
||||
.updateTable('schedules')
|
||||
.set({
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
})
|
||||
.where('id', '=', schedule.id.toString())
|
||||
.execute();
|
||||
}
|
||||
async delete(id) {
|
||||
await this.db.deleteFrom('schedules').where('id', '=', id.toString()).execute();
|
||||
}
|
||||
toEntity(row) {
|
||||
return Schedule_1.Schedule.reconstitute(row.id, {
|
||||
name: row.name,
|
||||
url: row.url,
|
||||
cronExpression: row.cron_expression,
|
||||
config: JSON.parse(row.config_json),
|
||||
enabled: row.enabled === 1,
|
||||
lastRunAt: row.last_run_at,
|
||||
nextRunAt: row.next_run_at,
|
||||
createdAt: row.created_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.KyselyScheduleRepository = KyselyScheduleRepository;
|
||||
Reference in New Issue
Block a user