CODE · 0.6 KB
server/job-store.js
Workspace snapshot · 09/04 14:52
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import path from "node:path";
export class JobStore {
constructor(rootDir) {
this.rootDir = rootDir;
}
file(id) {
return path.join(this.rootDir, `${id}.json`);
}
async save(job) {
await mkdir(this.rootDir, { recursive: true });
const target = this.file(job.id);
const temporary = `${target}.tmp`;
await writeFile(temporary, JSON.stringify(job, null, 2), "utf8");
await rename(temporary, target);
return job;
}
async get(id) {
return JSON.parse(await readFile(this.file(id), "utf8"));
}
}