1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Properly passing objects between my applications models/modules

Discussion in 'JavaScript' started by loki781, Aug 29, 2019.

  1. #1
    I have several models/modules such as User, Wallet, both these modules that are called from the express application such as User.login(req,res);
    right now in each User.js and Wallet.js I am including database.js which looks like this:

    var mysql = require('mysql2/promise')
    var pool = mysql.createPool({
        connectionLimit: 10,
        host: 'localhost',
        user: 'x',
        password: 'x',
        database: 'x'
    pool.getConnection((err, connection) => {
        if (err) {
            if (err.code === 'PROTOCOL_CONNECTION_LOST') {
                console.error('Database connection was closed.')
            if (err.code === 'ER_CON_COUNT_ERROR') {
                console.error('Database has too many connections.')
            if (err.code === 'ECONNREFUSED') {
                console.error('Database connection was refused.')
        if (connection) connection.release()
    module.exports = pool
    Code (markup):
    then in User.login() I can use pool.query/pool.execute.

    My question is there a better way of including this instead of require('./database.js) in both User.js and Wallet.js? Ideally calling database.js in my app.js main application file and having pool be visible to modules I include like User.js and Wallet.js
    loki781, Aug 29, 2019 IP