axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
130 lines • 6.07 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const FileManager_1 = __importDefault(require("../../engine/Filesystem/FileManager"));
const FolderManager_1 = __importDefault(require("../../engine/Filesystem/FolderManager"));
const Converter_helper_1 = __importDefault(require("../../Helper/Converter.helper"));
const response_helper_1 = __importDefault(require("../../Helper/response.helper"));
class TransactionRegistry {
constructor(collectionPath) {
this.collectionPath = collectionPath;
this.transactionDir = `${collectionPath}/.transactions`;
this.registryPath = `${this.transactionDir}/txn-meta.json`;
this.FileManager = new FileManager_1.default();
this.FolderManager = new FolderManager_1.default();
this.Converter = new Converter_helper_1.default();
this.ResponseHelper = new response_helper_1.default();
}
registerTransaction(metadata) {
return __awaiter(this, void 0, void 0, function* () {
try {
const dirExists = yield this.FolderManager.DirectoryExists(this.transactionDir);
if (!dirExists.status) {
yield this.FolderManager.CreateDirectory(this.transactionDir);
}
const transactions = yield this.getAllTransactions();
transactions.push(metadata);
const writeResult = yield this.FileManager.WriteFile(this.registryPath, this.Converter.ToString(transactions));
if (writeResult.status) {
return this.ResponseHelper.Success({
message: "Transaction registered",
transactionId: metadata.transactionId,
});
}
return this.ResponseHelper.Error("Failed to register transaction");
}
catch (error) {
return this.ResponseHelper.Error(error);
}
});
}
updateTransactionStatus(txnId, status) {
return __awaiter(this, void 0, void 0, function* () {
try {
const transactions = yield this.getAllTransactions();
const txnIndex = transactions.findIndex((t) => t.transactionId === txnId);
if (txnIndex === -1) {
return this.ResponseHelper.Error("Transaction not found");
}
transactions[txnIndex].status = status;
const writeResult = yield this.FileManager.WriteFile(this.registryPath, this.Converter.ToString(transactions));
if (writeResult.status) {
return this.ResponseHelper.Success({
message: "Transaction status updated",
transactionId: txnId,
status,
});
}
return this.ResponseHelper.Error("Failed to update transaction status");
}
catch (error) {
return this.ResponseHelper.Error(error);
}
});
}
getActiveTransactions() {
return __awaiter(this, void 0, void 0, function* () {
try {
const transactions = yield this.getAllTransactions();
return transactions.filter((t) => t.status === 'ACTIVE' || t.status === 'PREPARING' || t.status === 'COMMITTED');
}
catch (_a) {
return [];
}
});
}
removeTransaction(txnId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const transactions = yield this.getAllTransactions();
const filteredTransactions = transactions.filter((t) => t.transactionId !== txnId);
const writeResult = yield this.FileManager.WriteFile(this.registryPath, this.Converter.ToString(filteredTransactions));
if (writeResult.status) {
return this.ResponseHelper.Success({
message: "Transaction removed",
transactionId: txnId,
});
}
return this.ResponseHelper.Error("Failed to remove transaction");
}
catch (error) {
return this.ResponseHelper.Error(error);
}
});
}
getAllTransactions() {
return __awaiter(this, void 0, void 0, function* () {
try {
const fileExists = yield this.FileManager.FileExists(this.registryPath);
if (!fileExists.status) {
return [];
}
const readResult = yield this.FileManager.ReadFile(this.registryPath);
if (!readResult.status) {
return [];
}
const content = readResult.data;
if (!content || content.trim().length === 0) {
return [];
}
return this.Converter.ToObject(content);
}
catch (_a) {
return [];
}
});
}
}
exports.default = TransactionRegistry;
//# sourceMappingURL=TransactionRegistry.service.js.map