https://auth0.com/blog/developing-well-organized-apis-with-nodejs-joi-and-mongo/Am referring this tutorial for creating an API in a well organized manner using Restify & Mongoose. Btw, I come from a PHP background.My doubt is, while creating the database connection, the author has initialized the Users model. But what if I want to use other models inside the Services and Controllers? Do I have to include them one by one just beneath that line?'use strict'; const serviceLocator = require('../lib/service_locator'); const logger = serviceLocator.get('logger'); class Database { constructor(port, host, name) { this.mongoose = serviceLocator.get('mongoose'); this._connect(port, host, name); } _connect(port, host, name) { this.mongoose.Promise = global.Promise; this.mongoose.connect(`mongodb://${host}:${port}/${name}`); const {connection} = this.mongoose; connection.on('connected', () => logger.info('Database Connection was Successful') ); connection.on('error', (err) => logger.info('Database Connection Failed' + err) ); connection.on('disconnected', () => logger.info('Database Connection Disconnected') ); process.on('SIGINT', () => { connection.close(); logger.info( 'Database Connection closed due to NodeJs process termination' ); process.exit(0); }); // initialize Model require('../models/Users'); // <--------------------- this line } } Or I can include at the top of the Service / Controller page file? Which is the best practice to follow?Because in this tutorial there's only one model overall. But in real world application, there would be more than one models. So are they supposed to be initialize all together in a single place?
Submitted July 20, 2019 at 06:14AM by vpp00708
No comments:
Post a Comment