UNPKG

fenixenbot.js

Version:

Libreria Creacion De Bots De Discord

47 lines (38 loc) 1.36 kB
const { mongoose } = require('../libs/MongoConnect'); function MSchema(config) { const { nameTable, items, options = {} } = config; if (!nameTable) throw new Error("nameTable es obligatorio"); if (!Array.isArray(items)) throw new Error("items debe ser un array"); const typeMap = { STRING: String, TEXT: String, INTEGER: Number, FLOAT: Number, DOUBLE: Number, NUMBER: Number, DECIMAL: Number, BOOLEAN: Boolean, DATE: Date, DATEONLY: Date, JSON: Object, JSONB: Object, ARRAY: Array, UUID: String, ENUM: String, OBJECTID: mongoose.Schema.Types.ObjectId, }; const schemaDefinition = {}; items.forEach(itemConfig => { const { name, type, ...fieldOptions } = itemConfig; if (!name || !type) throw new Error("name y type son obligatorios"); const mongooseType = typeMap[type.toUpperCase()]; if (!mongooseType) throw new Error(`Tipo inválido: ${type}`); schemaDefinition[name] = { type: mongooseType, ...fieldOptions }; }); const mongooseSchema = new mongoose.Schema(schemaDefinition, options); return mongoose.model(nameTable, mongooseSchema); } module.exports = MSchema;