//db.js file const mongoClient = require('mongodb').MongoClient; const mongoDbUrl = 'mongodb://127.0.0.1:27017/mydb'; let mongodb; function connect(callback){ mongoClient.connect(mongoDbUrl, (err, db) => { mongodb = db; callback(); }); } function get(){ return mongodb; } function close(){ mongodb.close(); } module.exports = { connect, get, close };
'use strict' // db is just an object shared between the migrations var myDB = require('../db/db.js'); module.exports.up = function (next) { myDB.connect(function (err, client) { if (err) { console.log(err); } else { // start the rest of your app here } }); console.log("MyDB=" + myDB.get()); next() } module.exports.down = function (next) { next() }
sp@sp-HP-ProBook-4530s:~/Documents/express$ migrate up : 1648736072728-posts.js MyDB=undefined migration : complete
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)