ryuudb
Version:
A lightweight, customizable JSON/YAML database for Node.js by Jr Busaco
25 lines (21 loc) • 609 B
JavaScript
import { promises as fs } from 'fs';
import { resolve } from 'path';
import yaml from 'js-yaml';
export class FileAdapter {
constructor(filePath, format = 'json') {
this.filePath = resolve(filePath);
this.format = format;
}
async read() {
try {
const raw = await fs.readFile(this.filePath, 'utf8');
return this.format === 'yaml' ? yaml.load(raw) : JSON.parse(raw);
} catch {
return {};
}
}
async write(data) {
const content = this.format === 'yaml' ? yaml.dump(data) : JSON.stringify(data, null, 2);
await fs.writeFile(this.filePath, content);
}
}