bapig
Version:
Welcome to BAPIG, a powerful backend solution designed to streamline your development process. With over 40 built-in functionalities, BAPIG simplifies CRUD operations, facilitates authentication and encryption, handles communication tasks, and much more.
58 lines • 2.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllSchema = void 0;
// Import necessary modules
var fs_1 = __importDefault(require("fs"));
var helpers_1 = require("../helpers");
/**
* Function to retrieve all table schemas.
* @returns {controllerResponse} Returns success and message containing schemas if successful; otherwise, returns an error message.
*/
function getAllSchema() {
try {
// Check if the models directory exists
var schemaDirectory = fs_1.default.existsSync(helpers_1.modelsDirectory) ? helpers_1.modelsDirectory : undefined;
if (schemaDirectory) {
// Read all files in the schema directory
var schemaFiles = fs_1.default.readdirSync(schemaDirectory);
if (schemaFiles.length > 0) {
var schemas = [];
// Iterate through each schema file
for (var _i = 0, schemaFiles_1 = schemaFiles; _i < schemaFiles_1.length; _i++) {
var schemaFile = schemaFiles_1[_i];
// Extract the file name without the extension
var fileName = schemaFile.substring(0, schemaFile.indexOf('.'));
// Load the schema from the file
var schema = require("".concat(schemaDirectory, "/").concat(schemaFile));
var name_1 = fileName;
// Check if the schema is an ES module
if (schema.__esModule) {
// Use the default export if available; otherwise, use the named export
schemas.push({ model: schema.default || schema[fileName], name: name_1 });
}
else {
// Use the default export if available; otherwise, use the schema itself
schemas.push(schema.default ? { model: schema.default, name: name_1 } : { model: schema, name: name_1 });
}
}
// Check if any schemas were found
if (schemas.length > 0) {
return { success: true, message: schemas };
}
return { success: false, message: 'No schema has been found' };
}
return { success: false, message: 'No schema file has been found' };
}
// Return an error if the models directory does not exist
return { success: false, message: 'Models directory does not exist' };
}
catch (error) {
// Return an error if an exception occurs
return { success: false, message: error.message };
}
}
exports.getAllSchema = getAllSchema;
//# sourceMappingURL=all.js.map