umzug down method not running - node.js

I am trying to use umzug/sequelize but I can't run the down method at all. I am following this tutorial. I want to basically create migration files with up and down methods. The up method gets executed successfully but calling down does not all the umzug.down() method at all.
"use strict";
const Promise = require("bluebird");
const sqlite3 = require("sqlite3");
const path = require('path');
module.exports = {
up: function() {
return new Promise(function(resolve, reject) {
/* up is to commit migrations to the database */
let db = new sqlite3.Database('./database/db.db');
db.run(`PRAGMA foreign_keys = ON`);
db.serialize(function() {
db.run(`CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT
)`);
});
db.close();
});
},
down: function() {
return new Promise(function(resolve, reject) {
/* roll back database changes made by this migration */
console.log('in down')
let db = new sqlite3.Database("./database/db.db");
db.serialize(function() {
db.run(`DROP TABLE users`);
});
db.close();
});
}
};
My migrate file looks like this as well:
const path = require("path");
const Umzug = require("umzug");
let umzug = new Umzug({
logging: function() {
console.log.apply(null, arguments);
},
migrations: {
path: "./database/migrations",
pattern: /\.js$/
},
upName: "up",
downName: "down"
});
const cmd = process.argv[2].trim();
// this will run your migrations
if(cmd=='up')
{
umzug.up().then(console.log("Migrations committed"));
}
else if (cmd=='down'){
umzug.down().then(console.log("Migrations revereted"));
}
When I do node migrate.js up it works. but down never gets executed. Am I missing something?

At some point, you will need to call the resolve() or reject() on the Promise for both your up and down functions.

Related

Wait for the promise

I'm trying to use the code from this answer to extend one OSS application.
However app.js is sync and no matter what I do, I cant force it to wait for the promise to resolve.
app.js
var cosmos = require('./cosmos.js');
const key = cosmos.key(var1, var2, var3);
console.log(key); // << shows Promise { <pending> }
mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
});
cosmos.js
async function retriveKey(subId, resGrp, server) {
const { EnvironmentCredential } = require("#azure/identity");
const { CosmosDBManagementClient } = require("#azure/arm-cosmosdb");
const armClient = new CosmosDBManagementClient(
new EnvironmentCredential(), subId
);
const { primaryMasterKey } = await armClient.databaseAccounts.listKeys(
resGrp, server
);
return new Promise(resolve => {
setTimeout(() => resolve(primaryMasterKey), 1000);
});
}
exports.key = retriveKey
If i console.log() inside the async function it actually shows the key, however mongoose db connection doesn't wait for the promise to get resolved, it starts connecting straight away and fails with something like: password must be a string.
If i hardcode actual key instead of this promise - everything works fine.
EDIT:
halfway there:
// pull cosmos keys
async function retriveKey(subId, resGrp, server) {
const { EnvironmentCredential } = require("#azure/identity");
const { CosmosDBManagementClient } = require("#azure/arm-cosmosdb");
const armClient = new CosmosDBManagementClient(
new EnvironmentCredential(), subId
);
const { primaryMasterKey } = await armClient.databaseAccounts.listKeys(
resGrp, server
);
return primaryMasterKey // don't even need a promise here
}
exports.key = retriveKey
var mongooseConnected; // global variable
app.use(function (req, res, next) {
if (!moongooseConnected) {
moongooseConnected = cosmos.key(var1, var2, var3).then(function (key) {
mongoose.connect(`xxx`,
{
auth: {
username: config.database.name,
password: key
}
}
);
console.log(key); // works as expected
require('./models/user');
require('./models/audit');
require('./routes/user')(app);
require('./routes/audit')(app, io);
});
}
moongooseConnected.then(function () {
next();
});
});
the database connection gets established, console.log(key) shows proper key in the log, however no routes are present in the app.
if i move routes or models outside of this app.use(xyz) - i'm starting to see failures due to:
Connection 0 was disconnected when calling createCollection
or
MongooseError [MissingSchemaError]: Schema hasn't been registered for model "User".
which (i assume) means they require mongoose to be instantiated, but they are not waiting.
If you switch from CommonJS modules to ES modules, you can use await to wait for a promise to resolve:
import cosmos from './cosmos.js';
const key = await cosmos.key(var1, var2, var3);
console.log(key);
await mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
});
Alternatively, you can wait with the initialization of mongoose until the first request comes in, because express middleware is asynchronous:
var mongooseConnected; // global variable
function connectMongoose() {
if (!mongooseConnected)
mongooseConnected = cosmos.key(var1, var2, var3)
.then(key => mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
}));
return mongooseConnected;
}
module.exports = connectMongoose;
If the code above is needed elsewhere, it can be put in a separate module and imported wherever needed:
const connectMongoose = require("./connectMongoose");
app.use(function(req, res, next) {
connectMongoose().then(function() {
next();
});
});
require('./routes/user')(app);
require('./routes/audit')(app, io);
Note that if several parallel requests come in, only the first of these will let the global variable mongooseConnected equal a promise, and all these requests will wait for it to resolve before calling next().
Also note that additional routes of app must be registered after this app.use command, not inside it.
unless somebody comes up with a way to do this with less changes to the original code base, this is what I'm using:
cosmos.js
// pull cosmos keys
async function retriveKey(subId, resGrp, server) {
const { DefaultAzureCredential } = require("#azure/identity");
const { CosmosDBManagementClient } = require("#azure/arm-cosmosdb");
const armClient = new CosmosDBManagementClient(
new DefaultAzureCredential(), subId
);
const { primaryMasterKey } = await armClient.databaseAccounts.listKeys(
resGrp, server
);
return primaryMasterKey
}
exports.key = retriveKey
app.js
// pull cosmos keys
var cosmos = require('./cosmos');
let key = cosmos.key(var1, var2, var3)
mongoose.connect(
`xxx`,
{
auth: {
username: config.database.name,
password: key
}
}
).catch(
err => {
console.log("dOrty h4ck");
key.then(k => mongoose.connect(
`xxx`,
{
auth: {
username: config.database.name,
password: k
}
}
)
);
}
)
basically, like Heiko mentioned, mongoose.connect() is actually async, but somehow blocking (??). so while first mongoose.connect() always fails - it gives enough time for the code to retrieve the key, then I catch the error and connect again. no other changes to the original code base are needed.

Parameter obj to Document() must be an object when trying to convert array to mongoose document with redis

I have using redis to cache my queries. Its working fine with object but not when i get array. It gives me an error **"Parameter "obj" to Document() must be an object, got kids", **. It also happens with count query. Here is my code :
const mongoose = require("mongoose");
const redis = require("redis");
const util = require("util");
const client = redis.createClient(process.env.REDIS_URL);
client.hget = util.promisify(client.hget);
const exec = mongoose.Query.prototype.exec;
mongoose.Query.prototype.cache = async function (options = {}) {
this.useCache = true;
this.hashKey = JSON.stringify(options.key || "");
this.time = JSON.stringify(options.time || 36000);
return this;
};
mongoose.Query.prototype.exec = async function () {
if (!this.useCache) {
return exec.apply(this, arguments);
}
const key = JSON.stringify(
Object.assign({}, this.getQuery(), {
collection: this.mongooseCollection.name,
})
);
// client.flushdb(function (err, succeeded) {
// console.log(succeeded); // will be true if successfull
// });
const cacheValue = await client.hget(this.hashKey, key);
if (cacheValue) {
const doc = JSON.parse(cacheValue);
/*
this.model refers to the Class of the corresponding Mongoose Model of the query being executed, example: User,Blog
this function must return a Promise of Mongoose model objects due to the nature of the mongoose model object having other
functions attached once is created ( validate,set,get etc)
*/
console.log("Response from Redis");
console.log(doc);
console.log(Array.isArray(doc));
return Array.isArray(doc)
? doc.map((d) => new this.model(d))
: new this.model(doc);
}
//await the results of the query once executed, with any arguments that were passed on.
const result = await exec.apply(this, arguments);
client.hset(this.hashKey, key, JSON.stringify(result));
client.expire(this.hashKey, this.time);
console.log("Response from MongoDB");
return result;
};
module.exports = {
clearHash(hashKey) {
client.del(JSON.stringify(hashKey));
},
};
Data in redis - [ 'kids', 'men', 'women' ]
Query - const collectionType = await Product.find() .distinct("collectionType") .cache({ key: "COLLECTION_TYPE" });
can i anyone please tell me what i am doing wrong?
I have solved by directly returning the doc and its working fine. Not sure if it is the right way if i directly do return doc then sending data from redis only

Using 2 different mongodb databases in a single nodejs project with Mongoose

I am creating a blog website where you can create your own blog and update it at anytime. However, I want to add a log-in system so that users can have their own separate blog page. My problem is that I have yet to find a way to use both the login database and the blog database in my website. Right now I am using mongoose to connect to mongodb atlas, is there a way to use something like mongoose.connect for multiple databases?
You just have two different mongoose schemas, one for the blogs and the other for the log in. They will be separated in the database, you don't need to have two separate ones.
Here is an example using mongodb package.
I believe you can use the same logic with Mongoose.
import { Db, MongoClient, MongoError } from "mongodb";
const mongoURIs: MongoDBsInterface = {
global: {
uri: process.env.MONGODB_URI_DEFAULT,
db: process.env.MONGODB_DB_NAME,
},
secondary: {
uri: process.env.MONGODB_URI_DEFAULT,
db: "secondaryDatabaseInTheSameCluster",
},
secondary: {
uri: "secondClusterURI",
db: "anotherDatabase",
},
};
// Connection parameters
const param = {
numberOfRetries: 20,
auto_reconnect: true,
useNewUrlParser: true,
useUnifiedTopology: true,
};
// Function to start multiple databases
export const startDatabases = (): Promise<MongoDBsDetailInterface> => {
return new Promise((resolve) => {
const promises = [];
logger.info("### Connect to MongoDBs");
// Loop into each mongoURIs
for (const lang in mongoURIs) {
if (mongoURIs[lang]) {
promises.push(connectToOneDB(lang, mongoURIs[lang]));
}
}
Promise.all(promises).then((res: MongoDBDetailInterface[]) => {
const dbs: MongoDBsDetailInterface = {};
// tslint:disable-next-line: prefer-for-of
for (let i: number = 0; i < res.length; i++) {
dbs[res[i].lang] = res[i].db;
}
resolve(dbs);
});
});
};
export const connectToOneDB = (
lang: string,
dbValue: MongoDBInterface
): Promise<MongoDBDetailInterface> => {
return new Promise(async (resolve) => {
logger.info(` - Connect to ${lang} in db ${dbValue.db} (${dbValue.uri})`);
// Connect to the db
const client = new MongoClient(dbValue.uri, param);
let db = null;
try {
await client.connect();
db = await client.db(dbValue.db);
} catch (e) {
logger.error(e.message);
client.close();
// you should change that to handle it for your usage
process.exit(1);
}
logger.info(` - Connected to ${lang}`);
return resolve({ lang, db });
});
};
And then you can use
const dbs = await startDatabases();
And put it in your global context.
You then just need to select the collection you want
dbs.global.collection("...")

Best practice running queries in Node.js with MongoDB driver 3.6?

The official documentation of the Node.js Driver version 3.6 contains the following example for the .find() method:
const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb+srv://<user>:<password>#<cluster-url>?w=majority";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const database = client.db("sample_mflix");
const collection = database.collection("movies");
// query for movies that have a runtime less than 15 minutes
const query = { runtime: { $lt: 15 } };
const options = {
// sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
const cursor = collection.find(query, options);
// print a message if no documents were found
if ((await cursor.count()) === 0) {
console.log("No documents found!");
}
await cursor.forEach(console.dir);
} finally {
await client.close();
}
}
To me this somewhat implies that I would have to create a new connection for each DB request I make.
Is this correct? If not, then what is the best practise to keep the connection alive for various routes?
You can use mongoose to set a connection with your database.
mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
then you need to define your models which you will use to communicate with your DB in your routes.
const MyModel = mongoose.model('Test', new Schema({ name: String }));
MyModel.findOne(function(error, result) { /* ... */ });
https://mongoosejs.com/docs/connections.html
It's 2022 and I stumbled upon your post because I've been running into the same issue. All the tutorials and guides I've found so far have setups that require reconnecting in order to do anything with the Database.
I found one solution from someone on github, that creates a class to create, save and check if a client connection exist. So, it only recreates a client connection if it doesn't already exist.
const MongoClient = require('mongodb').MongoClient
class MDB {
static async getClient() {
if (this.client) {
return this.client
}
this.client = await MongoClient.connect(this.url);
return this.client
}
}
MDB.url='<your_connection_url>'
app.get('/yourroute', async (req, res) => {
try {
const client = await MDB.getClient()
const db = client.db('your_db')
const collection = db.collection('your_collection');
const results = await collection.find({}).toArray();
res.json(results)
} catch (error) {
console.log('error:', error);
}
})

How to properly use Mongoose models with Node.js?

I'm trying to use mongoose to control my db logic and transactions. I already got Schema definitions and I'm exporting the models.
Howver when i try to use a model, it will fail witl a message like:
return mongoose.model('Report', reportSchema);
} has no method 'find'...
This is my Model export:
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var reportSchema = mongoose.Schema({
category: ObjectId,
authorName: String,
authorEmail: String,
text: String,
address: String,
coordinates: {
type: "Point",
coordinates: [Number,Number]
},
date: {
type: Date,
default: new Date()
},
comments: Array
});
return mongoose.model('Report', reportSchema);
});
And this is how my controller functions are coded using mongoose inside:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report');
var Category = require('../models/Category');
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getReport (request,response,next) {
var id = request.params.id;
Report.findById({_id: id}, function (err,report) {
callback(err,report);
});
}
function _createReport (request,response) {
var newReport = new Report();
newReport.text = request.body.text;
newReport.category = request.body.category;
newReport.author = request.session.userId;
newReport.save(function (err,savedReport) {
callback(err,savedReport._id,response);
});
}
function _updateReport (request,response) {
var id = request.params.id;
var update = request.body.report;
Report.findByIdAndUpdate(id, update, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _deleteReport (request,response) {
var id = request.params.id;
Report.findByIdAndRemove(id, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports){
callback(err,reports,response);
});
}
function _getCategories (request,response) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getCategoryReports (argument) {
var _id = mongoose.Types.ObjectId(request.params.id);
Report.find({category:id},{category:false},function (err, foundReports) {
callback(err,foundReports,response);
});
}
function _createCategory (request,response) {
var newCategory = new Category();
newCategory.name = request.body.name;
newCategory.save(function (err,savedCategory) {
callback(err,savedCategory._id,response);
});
}
function _updateCategory (request,response) {
var id = request.params.id;
var update = request.body.category;
Category.findByIdAndUpdate(id, update, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function _deleteCategory (request,response) {
var id = request.params.id;
Category.findByIdAndRemove(id, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function callback (err,object,response) {
if (err)
response.status(500).send(JSON.stringify(err));
response.send(JSON.stringify(object));
}
var apiController = {
getReports: _getReports,
getReport: _getReport,
createReport: _createReport,
updateReport: _updateReport,
deleteReport: _deleteReport,
getCategories: _getCategories,
getCategoryReports: _getCategoryReports,
createCategory: _createCategory,
updateCategory: _updateCategory
}
return apiController;
})();
Before this, a mongoose connection is ensured:
var connectToMongoose = function (mongoose,app) {
var connect = function () {
var options = { server: { socketOptions: { keepAlive: 1 } } };
mongoose.connect( 'mongodb://localhost/data4', options);
}
mongoose.connection.on('connected', function () {
console.log('Connected to db');
app.listen(32884, function() {
console.log("Listening at \"data4 port\" #:32884");
});
})
mongoose.connection.on('error', function (err) {
console.log(err);
});
mongoose.connection.on('disconnected', function () {
console.log('Disconnected from db, attempting to connect again...');
app.close();
connect();
});
connect();
};
module.exports = connectToMongoose;
Which is invoked by require('./db/mongoose-connect.js')(mongoose,app);
What am I doing wrong?
There are a couple issues here that I caught off the bat.
First off, I don't see a mongoose.connect() line that explicitly connects your mongoose ODM to a mongo server+database. An example would be:
var mongoose = require( 'mongoose' ),
Schema = mongo.Schema,
ObjectId = mongo.Schema.ObjectId;
mongoose.connect( 'mongodb://localhost/db_name' );
Your schema export looks fine. But you're using an anonymous function as your export. Since you're doing that, your require statement needs to change a little:
var Report = require('../models/Report')();
var Category = require('../models/Category')();
Notice the () at the end of the require statements. You need to execute the function that you're defining as your model file's module.export.
EDIT: I see that you added your mongoose connect code. At this point, executing the module.exports function that you assign in the model file should allow your mongoose models to function as intended.
When you export a function;
// file: A.js
module.exports = function () {
//some logic
};
And you want to use it on another file, when you require the A file, you are importing a function and in order to use that function, you need to to invoke it.
// file: B.js
var A = require('./A.js');
A();
so your model is exporting a function
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// ..
// some code
// ..
return mongoose.model('Report', reportSchema);
});
and when you are importing your model from your controller, you need to execute your imported function so that your Report variable contains the model created:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report') ();
I have created a gist of how you could write your code using modules without using IIFE.
https://gist.github.com/wilsonbalderrama/d5484f3f530899f101dc
actually if you download all those files on a folder and run:
$ sudo npm install
$ mocha
You could see that all the tests created for the controller are passing.
In addition you don't need to use IIFE in Node.JS since when you are creating a module because you already have an isolated scope in Node.JS using modules.
// IIFE
module.exports = (function() {
var apiController = {
getReport: function () {}
}
return apiController;
})();
In Node.JS you can export a object,
// GOOD WAY
module.exports = {
getReport: function () {}
};

Resources