nodedb-json
Version:
A lightweight JSON-based database for Node.js with TypeScript support, indexing, and complex query capabilities
96 lines • 3.71 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonFileStorage = void 0;
exports.readJsonFile = readJsonFile;
const fs = __importStar(require("fs"));
const atomic_writer_1 = require("./atomic-writer");
const corrupted_file_error_1 = require("../errors/corrupted-file-error");
class JsonFileStorage {
constructor(filePath, options) {
this.filePath = filePath;
this.options = options;
}
read() {
(0, atomic_writer_1.ensureParentDirectory)(this.filePath);
(0, atomic_writer_1.cleanupTempFile)(this.options.tempPath);
if (!fs.existsSync(this.filePath)) {
if (this.options.createIfNotExists) {
this.write(JSON.stringify(this.options.defaultValue || {}, null, 2));
}
else {
throw new Error(`Database file does not exist: ${this.filePath}`);
}
}
return this.readWithBackup();
}
write(content, backupOnWrite = true) {
if (!this.options.atomicWrites) {
(0, atomic_writer_1.ensureParentDirectory)(this.filePath);
fs.writeFileSync(this.filePath, content, 'utf-8');
return;
}
(0, atomic_writer_1.writeFileAtomic)(this.filePath, content, {
backupOnWrite: backupOnWrite && this.options.backupOnWrite,
backupPath: this.options.backupPath,
tempPath: this.options.tempPath
});
}
readWithBackup() {
try {
return readJsonFile(this.filePath);
}
catch (error) {
if (!fs.existsSync(this.options.backupPath)) {
throw new corrupted_file_error_1.CorruptedFileError(this.filePath, error);
}
let backupData;
try {
backupData = readJsonFile(this.options.backupPath);
}
catch (backupError) {
throw new corrupted_file_error_1.CorruptedFileError(this.filePath, backupError);
}
this.write(JSON.stringify(backupData, null, 2), false);
return backupData;
}
}
}
exports.JsonFileStorage = JsonFileStorage;
function readJsonFile(filePath) {
const fileData = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(fileData);
}
//# sourceMappingURL=json-file-storage.js.map