80 lines
2.5 KiB
JavaScript
80 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.KyselyCrawlSessionRepository = void 0;
|
|
const CrawlSession_1 = require("../../domain/entities/CrawlSession");
|
|
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
|
class KyselyCrawlSessionRepository {
|
|
constructor(db) {
|
|
this.db = db;
|
|
}
|
|
async save(session) {
|
|
const row = {
|
|
id: session.id.toString(),
|
|
url: session.url,
|
|
status: session.status,
|
|
seed: session.seed,
|
|
max_states: session.maxStates,
|
|
states_visited: session.statesVisited,
|
|
anomalies_found: 0,
|
|
started_at: Date.now(),
|
|
finished_at: null,
|
|
config_json: JSON.stringify(session.config),
|
|
};
|
|
await this.db
|
|
.insertInto('sessions')
|
|
.values(row)
|
|
.execute();
|
|
}
|
|
async findById(id) {
|
|
const row = await this.db
|
|
.selectFrom('sessions')
|
|
.selectAll()
|
|
.where('id', '=', id.toString())
|
|
.executeTakeFirst();
|
|
if (!row)
|
|
return null;
|
|
return this.toDomain(row);
|
|
}
|
|
async findAll() {
|
|
const rows = await this.db
|
|
.selectFrom('sessions')
|
|
.selectAll()
|
|
.orderBy('started_at', 'desc')
|
|
.execute();
|
|
return rows.map((row) => this.toDomain(row));
|
|
}
|
|
async update(session) {
|
|
const isTerminal = session.status === 'completed' || session.status === 'failed' || session.status === 'stopped';
|
|
await this.db
|
|
.updateTable('sessions')
|
|
.set({
|
|
status: session.status,
|
|
states_visited: session.statesVisited,
|
|
finished_at: isTerminal ? Date.now() : null,
|
|
config_json: JSON.stringify(session.config),
|
|
})
|
|
.where('id', '=', session.id.toString())
|
|
.execute();
|
|
}
|
|
toDomain(row) {
|
|
const props = {
|
|
url: row.url,
|
|
status: row.status,
|
|
seed: row.seed,
|
|
maxStates: row.max_states,
|
|
statesVisited: row.states_visited,
|
|
config: this.parseJson(row.config_json),
|
|
};
|
|
return CrawlSession_1.CrawlSession.reconstitute(props, UniqueId_1.UniqueId.from(row.id));
|
|
}
|
|
parseJson(json) {
|
|
try {
|
|
return JSON.parse(json);
|
|
}
|
|
catch {
|
|
return {};
|
|
}
|
|
}
|
|
}
|
|
exports.KyselyCrawlSessionRepository = KyselyCrawlSessionRepository;
|