@naturalcycles/db-lib
Version:
Lowest Common Denominator API to supported Databases
43 lines (42 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalFilePersistencePlugin = void 0;
const node_stream_1 = require("node:stream");
const js_lib_1 = require("@naturalcycles/js-lib");
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
/**
* Persists in local filesystem as ndjson.
*/
class LocalFilePersistencePlugin {
constructor(cfg = {}) {
this.cfg = {
storagePath: './tmp/localdb',
gzip: true,
...cfg,
};
}
async ping() { }
async getTables() {
return (await nodejs_lib_1.fs2.readdirAsync(this.cfg.storagePath))
.filter(f => f.includes('.ndjson'))
.map(f => f.split('.ndjson')[0]);
}
async loadFile(table) {
await nodejs_lib_1.fs2.ensureDirAsync(this.cfg.storagePath);
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
if (!(await nodejs_lib_1.fs2.pathExistsAsync(filePath)))
return [];
return await nodejs_lib_1.fs2.createReadStreamAsNDJSON(filePath).toArray();
}
async saveFiles(ops) {
await (0, js_lib_1.pMap)(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 32 });
}
async saveFile(table, rows) {
await nodejs_lib_1.fs2.ensureDirAsync(this.cfg.storagePath);
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
await (0, nodejs_lib_1._pipeline)([node_stream_1.Readable.from(rows), ...nodejs_lib_1.fs2.createWriteStreamAsNDJSON(filePath)]);
}
}
exports.LocalFilePersistencePlugin = LocalFilePersistencePlugin;