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
84 lines • 4.18 kB
JavaScript
;
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 });
/* eslint-disable @typescript-eslint/no-explicit-any */
const FileManager_1 = __importDefault(require("../../engine/Filesystem/FileManager"));
const outers_1 = require("outers");
const response_helper_1 = __importDefault(require("../../Helper/response.helper"));
const Keys_1 = require("../../config/Keys/Keys");
const Converter_helper_1 = __importDefault(require("../../Helper/Converter.helper"));
const PathSanitizer_helper_1 = __importDefault(require("../../Helper/PathSanitizer.helper"));
/**
* Class representing an insertion operation.
*/
class Insertion {
/**
* Creates an instance of Insertion.
* @param {string} collectionName - The name of the collection.
* @param {string | any} path - The data to be inserted.
*/
constructor(collectionName, path) {
this.collectionName = collectionName;
this.path = path;
this.Converter = new Converter_helper_1.default();
}
/**
* Saves the data to a file.
* @returns {Promise<any>} A promise that resolves with the response of the save operation.
*/
Save(data, ExistingdocumentId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const documentId = ExistingdocumentId === undefined
? yield this.generateUniqueDocumentId()
: ExistingdocumentId;
// Sanitize documentId to prevent directory traversal attacks
const sanitizedDocumentId = PathSanitizer_helper_1.default.sanitizePathComponent(documentId);
const filePath = PathSanitizer_helper_1.default.safePath(this.path, `${sanitizedDocumentId}${Keys_1.General.DBMS_File_EXT}`);
// Directly write data to file (no lock/unlock system)
const WriteResponse = yield new FileManager_1.default().WriteFile(filePath, this.Converter.ToString(data));
if (WriteResponse.status) {
return new response_helper_1.default().Success({
Message: "Data Inserted Successfully",
documentId: documentId,
});
}
return new response_helper_1.default().Error("Failed to save data");
}
catch (error) {
return new response_helper_1.default().Error(error);
}
});
}
/**
* Generates a unique document ID.
* @returns {Promise<string>} A promise that resolves with a unique document ID.
*/
generateUniqueDocumentId() {
return __awaiter(this, void 0, void 0, function* () {
let isExist = true;
let ID;
do {
ID = new outers_1.ClassBased.UniqueGenerator(15).RandomWord(true);
// Sanitize ID to ensure safe file path
const sanitizedID = PathSanitizer_helper_1.default.sanitizePathComponent(ID);
const response = yield new FileManager_1.default().FileExists(PathSanitizer_helper_1.default.safePath(this.path, `${sanitizedID}${Keys_1.General.DBMS_File_EXT}`));
isExist = response.status;
} while (isExist);
return ID;
});
}
}
exports.default = Insertion;
//# sourceMappingURL=Create.operation.js.map