@naturalcycles/db-lib
Version:
Lowest Common Denominator API to supported Databases
40 lines (39 loc) • 1.42 kB
JavaScript
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
/**
* Persists in local filesystem as ndjson.
*/
export class LocalFilePersistencePlugin {
constructor(cfg = {}) {
this.cfg = {
storagePath: './tmp/localdb',
zst: true,
...cfg,
};
}
cfg;
async ping() { }
async getTables() {
return (await fs2.readdirAsync(this.cfg.storagePath))
.filter(f => f.includes('.ndjson'))
.map(f => f.split('.ndjson')[0]);
}
async loadFile(table) {
await fs2.ensureDirAsync(this.cfg.storagePath);
const ext = `ndjson${this.cfg.zst ? '.zst' : ''}`;
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
if (!(await fs2.pathExistsAsync(filePath)))
return [];
return await Pipeline.fromNDJsonFile(filePath).toArray();
}
async saveFiles(ops) {
await pMap(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 32 });
}
async saveFile(table, rows) {
await fs2.ensureDirAsync(this.cfg.storagePath);
const ext = `ndjson${this.cfg.zst ? '.zst' : ''}`;
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
await Pipeline.fromArray(rows).toNDJsonFile(filePath);
}
}