use custom mongoDB server instead of mongolab - node.js

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.

Related

Mongo connecting to localhost although the conn string is right accross the app

So I have an Node API hosted on AWS' Beanstalk and it's connected to a DocumentDB database hosted within the same VPC, unfortunately I can't share too much info on the project since I've inherited it, but currently we have a terrible logic of creating a new db for each tenant in the system so when they sign in we create a new mongoose connection (with the proper string). Upon that there is also a global connection to a central db which is supposed to be established upon the initial start of the API - this works as expected. What doesn't work for some reason is that whenever I make a request that's supposed to go through for a certain tenant I get the following error on AWS Beanstalk logs:
ERROR IMAGE
Unfortunately, I can't fully disclose the connection strings, but I can tell you that there are no references to "locahost" within the project (noted in the picture):
PROJECT SEARCH RESULT
Has anyone encountered similar issues ?

How to configure Mongoose with an already existing Mongo connection

I have an app that is already working with the native Node Mongo driver (v3.0).
I'm now trying to slowly implement Mongoose in order to make the app easier to maintain. I would like to do this in a gradual way so I rewrote all the user related operations with Mongoose and the rest like it was before. I noticed that my app now creates two connections to my Mongo db. This is clearly because Mongoose knows nothing about my existing connection.
I would like to handle connecting and disconnecting to Mongo myself and give Mongoose a reference to the already existing connection but I can't find anything like this in the docs.
Is this even possible or will I need two different connections until my app is fully rewritten to use Mongoose exclusively?
EDIT: My app is being run as an AWS Lambda function which has to connect and disconnect to mongo on every request so having two concurrent connections per request is effectively halving my mongo db available connections. That’s why I’m concerned about having an extra connection.
Turns out the answer to this is to do it the other way around. Just connect to Mongoose and then grab the connection.
let mongoConnection = mongoose.connection.client

How to solve the error SequelizeHostNotFoundError?

Problem essence
Writing the API server in NodeJS (Express) using the PostgreSQL database (connecting to it remotely through the service ElephantSQL and work through Sequelize). Today appeared an error "SequelizeHostNotFoundError". It occurs even in the checked endpoints.
Error text
(it remains only a screenshot of the error)
My attempts to solve the problem
Tried to perform a GET request to my API not via Postman and via the browser (did not help).
Tried to create a new DB on the same service ElephantSQL (didn't help, but migrations to create new tables and relationships somehow executed and endpoints still not working).
Tried to connect to the database directly via IDE DataGrip (connection test is successful and the database is loaded with all the tables).
What could be the problem ? On stackoverflow some wrote that the problem may occur due to the lack of a paid subscription to Google Cloude Functions, but I do not seem to use it. There is an option to connect to PostgreSQL locally, but I want to understand.

mongoose create DB not working

Neither mongoose.connect('mongodb://localhost/sync-db'); nor mongoose.createConnection('mongodb://localhost/sync-db'); is creating the sync-db on local host.
Though both connection callbacks are successfully connected. But when I tried to check both robo-mongo and mongod client console using the command show dbs, the sync-db is neither to be seen on robo-mongo nor on mongod client
WTF?
Since no one is answering this question, I will post my own answer based on what #Odonno said on the comment, which is correct.
The codes above use to create database is not wrong, in the background the database has already been created, but for some reason (I hope someone will explain this) you wont see it until you added some data to it.

PouchDb on PAAS (Heroku, Bluemix, etc)

I've gotten some great feedback from Stackoverflow and wanted to check on one more idea.
Currently I've got a webapp that runs nodejs on a PAAS (Heroku and trying out bluemix). The server is being configured to talk to a Couchdb (hosted on cloudant). There are two types of data saved to the db, first, user data (each user will have it's own database), and second, app data itself (metrics, user account info (auth/admin stuff).
After some great feedback from here, the idea is that after the user logs in, they will sync there local (browser) pouchdb instance with Cloudant (probably proxied through my server as was recommended here).
Now the question is, for the app/admin data, maybe I run a couchdb instance on my server so i'm not making repeated network calls for things like user logins, metrics data, etc. The data would not be very big, and is already separated from the user data calls. The point is to have a faster/local instance for authentication mainly, changes/updates get synced outside of user requests.
The backend is in express web framework and it looks like my options are pouchdb.... to sync to the Cloudant instance?
If I want local db access (backed a Couchdb instance), on a node/express server running on a PAAS, is that the recommended setup?
Thanks vm for any feedback,
Paul
Not sure if you found a solution, but this is what I would try.
Because heroku clears any temp data, you wouldn't be able to run a default express-pouch database, you will need to change pouch db from using file system to using LevelDOWN adapter.(Link to Pouchdb adapters: https://pouchdb.com/adapters.html)
Some of these adapters would include:
https://github.com/watson/mongodown
https://github.com/kesla/mysqldown
https://github.com/hmalphettes/redisdown
You can easily get heroku mondo, mysql, or redis addon, and connect that to you express-pouchdb backend.
This way you will be able to keep your data.

Resources