UNPKG

node-json-db

Version:

Database using JSON file as storage for Node.JS

41 lines 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonAdapter = void 0; class JsonAdapter { adapter; humanReadable; dateRegex = new RegExp('^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}', 'm'); constructor(adapter, humanReadable = false) { this.adapter = adapter; this.humanReadable = humanReadable; } replacer(key, value) { return value; } reviver(key, value) { if (typeof value == "string" && this.dateRegex.exec(value) != null) { return new Date(value); } return value; } async readAsync() { const data = await this.adapter.readAsync(); if (data == null || data === '') { await this.writeAsync({}); return {}; } return JSON.parse(data, this.reviver.bind(this)); } writeAsync(data) { let stringify = ''; if (this.humanReadable) { stringify = JSON.stringify(data, this.replacer.bind(this), 4); } else { stringify = JSON.stringify(data, this.replacer.bind(this)); } return this.adapter.writeAsync(stringify); } } exports.JsonAdapter = JsonAdapter; //# sourceMappingURL=JsonAdapter.js.map