UNPKG

@dreesq/serpent

Version:

An express wrapper for developing fast web applications

79 lines (60 loc) 1.84 kB
const mongoose = require('mongoose'); const {get: getValue, toModelName, load} = require('../utils'); const {APP_PATH, MODULE_PATH} = require('../constants'); let client = false; let db = {}; /** * Helpers * @param name * @param modelPath */ db.loadModel = (name, modelPath) => { name = toModelName(name.substring(0, name.lastIndexOf('.'))); let model = require(modelPath); if (typeof model === 'function') { model = model(mongoose.Schema); } db[toModelName(name)] = mongoose.model(name, model, name); }; /** * Initialize the database plugin * @param context */ exports.init = async context => { const {config: appConfig} = context; const {config, logger} = context.plugins; try { await mongoose.connect(config.get('plugins.db.server'), { useNewUrlParser: true, useUnifiedTopology: true }); mongoose.set('useCreateIndex', true); client = db.client = mongoose.connection; /** * Load application models */ let models = {}; await load(MODULE_PATH, 'models', (name, modelPath) => { models[name] = modelPath; }, false); let modelsPath = getValue(appConfig, 'autoload.models'); modelsPath = modelsPath === true ? 'models' : modelsPath; if (modelsPath) { await load(APP_PATH, modelsPath, (name, modelPath) => { models[name] = modelPath; }, false); } for (const name in models) { let path = models[name]; db.loadModel(name, path); } } catch(e) { logger.error(e instanceof Error ? e.stack : e); } mongoose.connection.on('error', logger.error); }; /** * Exported models and methods * @type {{client: boolean}} */ exports.methods = db;