Sails mongodb missing connection handling - node.js

Very simple and stupid question I came up with today when was playing around with sails and mongo db (adapter of the waterline odm). I just shut down the mongo while my sails server still has been running and saw nothing, neither in logs of sails or in the browser. How I could handle such situation?

If sails.js attempts a connection to the DB you will most certainly get a message in your console if the Mongo server is shut down, but if no request is made that requires a DB connection, then you will not see any error because no error as of yet exists.
It sounds like you might require a process that will monitor your DB and check to make sure it is still running? There are Sail.js options where you could create a CRON job to check the connection. Or you could use other application monitoring services like New Relic to monitor your DB.

Related

NodeJS start mongoDB server

I need to start the mongoDB server from my NodeJS application. I managed to do this before but for some reason I forgot how. I though I used a cild process but not sure anymore as I can't get anything to work at the moment.
How would I start the mongoDB server (command mongod) from withing my NodeJS app and execute some other code when the server had been started (guessing using a promise...)?
You can use child_process to run mongod from your application, but this may cause the MongoDB server to exit when your app exits. It's generally better to have the DB server running all the time.
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

use custom mongoDB server instead of mongolab

Today i started migrating from firebase to mongoDB,
I used this tutorial and its all up and running http://thejackalofjavascript.com/re-architecting-a-firebase-app-in-node/
When checking the code i see there is a mongolab link connected to the code,
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
My question is: can i easily setup my own local database to use instead of this? and what packages i would need to do this?
The main reason for switching to mongo instead of firebase is the pricing, please take this into account.
Yes, of course you can.
First, install mongodb locally, to do that, follow the instructions for the distribution you're working on.
Then, make sure your mongodb service is running.
After that, on your database connection script, change the connection parameters.
I guess that you have something like this on your .js file:
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
Just to make a connection test, try to change it to:
mongo://localhost/test
After a successful connection, you can start to manage your database as you want.
As additional information, you don't have to specify user, password and a different port than the mongo's default, because you're using your local configuration, if you want to do so, you have to configure your mongodb server to make it work that way.
I'm working with mongoose ORM to manage a local database, and here's my connection function working.
Mongoose connection to local database
Hope this helps you.

Parse Server "Unable to ensure uniqueness for usernames"

Me and my partner are having serious issues with MongoDB these days. Suddenly, our Parse Server mounted on MongoDB, in a VPS, started failing. Our app couldn't connect there, nor the Parse Dashboard. Something was failing.
In the parse logs, this is what appears:
{"level":"info","message":"Parse LiveQuery Server starts
running","timestamp":"2017-07-09T20:23:59.711Z"}
{"level":"info","message":"Parse LiveQuery Server starts
running","timestamp":"2017-07-09T20:25:47.884Z"} {"message":"Unable to
ensure uniqueness for usernames:
","name":"MongoError","stack":"MongoError: failed to connect to server
[localhost:27017] on first connect\n at null.
(/home/main/StepApp/node_modules/mongodb-core/lib/$ {"message":"Unable
to ensure uniqueness for user email addresses:
","name":"MongoError","stack":"MongoError: failed to connect to server
[localhost:27017] on first connect\n at null.
(/home/main/StepApp/node_modules/mongod$
We tried this whole bunch of things:
Installing a new MongoDB version
Import the old .db files
Restart our Node.js instancies via pm2
Adding more space to our VPS (it seems MongoDB was having some trouble with disk space, but this is already fixed).
Adding an admin user to our db, with all permissions, and then adding it to the mongodb connection string in our Node file.
After all of this, several server restarts, pm2 restarts, mongo restarts...
... nothing works.
When I restart the server, I receive that strange error string I copied before, though Mongo is working fine.
Seems to be more of a Parse problem... but, why?
Any help will he appreciated!
EDIT / UPDATE:
I've been checking if MongoDB is working, and it's service "mongod" is online. Tried to listen to a different port, changed it too in the index.js mongo connection string, but I keep getting the same error.
Strange thing is, that when I connect to my database with my newly created Mongo user, it authenticates correctly! (I could see it in the logs). So it seems to be a Parse error, somehow it doesn't get the Mongo response or something like that.
What do you think?

Is it good practice to connect to the db multiple times?

I am using MongoDB, so I am connecting trough MongoClient.connect
but I have to use that, for every route where I want to work with the database.
Tried to preload it to an object, but then the changes are not visible, till the server is restarted. Right now, it's working properly, I am only a bit worried about the performance.
Is there a better way to do that?
THe correct answer is "it depends".
For a simple desktop application where your NodeJS program is the only client: sure. A persistent connection is fine.
For an enterprise application with 100s or 1000s of concurrent users each connecting independently: no, you probably do NOT want to hold the connection open "forever".
One possible solution for the latter scenario is Connection Pooling.
You should only need to connect to your DB once - when the server starts. As long as the server is running, the connection should persist. There is no reason to connect multiple times.

how to access various mongo databases with node

I need some help with this question.
I try to build an node express REST api which has to deal with various mongoDB databases on the same mongoDB server.
What is the right approach to do this?
The sequence could be:
app starts
connect to the mongoDB Server
use the right express route
check which database is needed
finally query the correct data
connection to the database is still open
OR
use the right express route
connect to the mongoDB Server
check which database is needed
query the data
close the DB connection
AND how can I do this with mongo-native driver, not mongoose?
Thanks for any help.
In Mongo, database connections are persistent - that is you should leave a connection open and not close it until you want to close your server.
Assuming you're using the new Mongo driver (new as in, not 3 years old), it will handle reconnects and managing the connections for you - all you have to do is just connect to it once the server starts and close the connection when the server ends.
This is a property of the node driver - so it is equally true for the driver itself and wrappers/mappers like Mongoose.

Resources