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
157 lines • 8.47 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 outers_1 = require("outers");
const responseBuilder_helper_1 = __importDefault(require("../../helper/responseBuilder.helper"));
const filesCounterInFolder_helper_1 = __importDefault(require("../../helper/filesCounterInFolder.helper"));
/**
* Controller class for managing collections in AxioDB.
*
* This class provides methods for creating, retrieving, and managing collections
* within the AxioDB instance. It acts as an interface between the API routes and
* the AxioDB instance.
*/
class CollectionController {
constructor(AxioDBInstance) {
this.AxioDBInstance = AxioDBInstance;
}
/**
* Creates a new collection in the specified database.
*
* @param request - The Fastify request object containing the collection details in the body
* @returns A ResponseBuilder object containing the status and message of the operation
*
* @throws Will return a conflict response if collection already exists
* @throws Will return a bad request response if name is missing, not a string, or empty
* @throws Will return an internal server error response if collection creation fails
*/
createCollection(request) {
return __awaiter(this, void 0, void 0, function* () {
// Extracting parameters from the request body
const { dbName, collectionName, crypto, key } = request.body;
// Validating extracted parameters
if (!dbName || typeof dbName !== "string") {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.BAD_REQUEST, "Invalid database name");
}
if (!collectionName || typeof collectionName !== "string") {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.BAD_REQUEST, "Invalid collection name");
}
const databaseInstance = yield this.AxioDBInstance.createDB(dbName);
const isCollectionExists = yield databaseInstance.isCollectionExists(collectionName);
if (isCollectionExists) {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.CONFLICT, "Collection already exists");
}
// Creating the collection
try {
yield databaseInstance.createCollection(collectionName, crypto, key);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.CREATED, "Collection created successfully", {
dbName,
collectionName,
});
}
catch (error) {
console.error("Error creating collection:", error);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.INTERNAL_SERVER_ERROR, "Failed to create collection");
}
});
}
/**
* Retrieves all collections for a specified database.
*
* @param request - The Fastify request object containing query parameters
* @returns A Promise resolving to a ResponseBuilder object with the response status and data
*
* @remarks
* This method expects a 'databaseName' query parameter in the request.
* It fetches all collections in the specified database and additionally computes
* the file count for each collection path.
*
* @throws Will return a BAD_REQUEST response if databaseName is not provided
* @throws Will return an INTERNAL_SERVER_ERROR response if collection retrieval fails
*/
getCollections(request) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
// extract databaseName from url query
const { databaseName } = request.query;
if (!databaseName) {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.BAD_REQUEST, "Database name is required");
}
try {
const collections = yield (yield this.AxioDBInstance.createDB(databaseName)).getCollectionInfo();
// Read all file count of each Collections
let FolderPaths = (_a = collections === null || collections === void 0 ? void 0 : collections.data) === null || _a === void 0 ? void 0 : _a.AllCollectionsPaths;
// Remove .meta extenstion paths
FolderPaths = FolderPaths.filter((path) => !path.endsWith(".meta"));
const mainData = collections === null || collections === void 0 ? void 0 : collections.data;
mainData.CollectionSizeMap = [];
yield Promise.all([
...FolderPaths.map((folderPath) => __awaiter(this, void 0, void 0, function* () {
const fileCount = yield (0, filesCounterInFolder_helper_1.default)(folderPath);
mainData.CollectionSizeMap.push({ folderPath, fileCount });
})),
]);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.OK, "Collections retrieved successfully", mainData);
}
catch (error) {
console.error("Error retrieving collections:", error);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.INTERNAL_SERVER_ERROR, "Failed to retrieve collections");
}
});
}
/**
* Deletes a collection from a specified database.
*
* @param request - The Fastify request object containing the database and collection names in the body.
* @returns A ResponseBuilder object with appropriate status code and message.
*
* @throws Returns a BAD_REQUEST response if the database name or collection name is invalid.
* @throws Returns a NOT_FOUND response if the collection does not exist.
* @throws Returns an INTERNAL_SERVER_ERROR response if the collection deletion fails.
*
* @example
* // Example request body:
* // {
* // "dbName": "myDatabase",
* // "collectionName": "myCollection"
* // }
*/
deleteCollection(request) {
return __awaiter(this, void 0, void 0, function* () {
const { dbName, collectionName } = request.query;
if (!dbName || typeof dbName !== "string") {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.BAD_REQUEST, "Invalid database name");
}
if (!collectionName || typeof collectionName !== "string") {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.BAD_REQUEST, "Invalid collection name");
}
const databaseInstance = yield this.AxioDBInstance.createDB(dbName);
const isCollectionExists = yield databaseInstance.isCollectionExists(collectionName);
if (!isCollectionExists) {
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.NOT_FOUND, "Collection not found");
}
try {
yield databaseInstance.deleteCollection(collectionName);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.OK, "Collection deleted successfully");
}
catch (error) {
console.error("Error deleting collection:", error);
return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.INTERNAL_SERVER_ERROR, "Failed to delete collection");
}
});
}
}
exports.default = CollectionController;
//# sourceMappingURL=Collection.controller.js.map