Correct Use of Mongoskin - node.js

I usually work with mongoskin because I like to be close to the database. Usually, I do a setup with a file like db.coffee, that contains just this:
mongo = require 'mongoskin'
# either local
module.exports = mongo.db 'mongodb://localhost/database'
# or remote
module.exports = mongo.db 'mongodb://<user>:<pass>#<host>:<port>/<db>?auto_reconnect=true'
Then I use it in my other sources:
db = require 'db'
users = db.collection 'users'
# Now use the collection in handlers and middleware
This seems to work perfectly fine when I am using a local mongo server, I've had an uptime for months and it never turned out to be a problem.
However, when I am using the remote second, I get problem if the server runs longer than just a few minutes - the connection to the mongodb seems lost, despite auto_reconnect. I guess this is because the localhost connection is never closed automatically.
However this led me to thinking if I am maybe using mongoskin in a wrong way, or if there's simply a bug with the auto_reconnect?

ensure mongoskin is using the 1.0.0 or higher driver

Related

Switch DB dynamically for NodeJS web application

I am trying to implement a feature which a user can decide on login to which DB to connect. As it is a web-app, running on a server which all the clients approach, how can I implement this feature without changing every client DB?
At our company we are using mongoose as the MongoDB API.
I read all the docs, and didn't notice any functionality for using multiple connections to different DB's on different hosts within the same App at once - without damaging other's client work.
The most valuable thing I have accomplished is to open few connections based on multiple mongoose instances, based on this post:
Mongoose and multiple database in single node.js project
I have created few files for example:
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://10.20.100.71:27017/DB_NAME');
module.exports = exports = mongoose;
And then I required them:
let stageAccess = require('./databsesConnections/stageAccess');
let prodAccess = require('./databsesConnections/prodAccess');
I debugged the files and checked the connections are establishing.
Further more I checked in the mongoose docs and concluded that I can choose which connection is the default connection, as the docs state:
"Mongoose creates a default connection when you call mongoose.connect(). You can access the default connection using mongoose.connection."
So I tried:
mongoose.connection = mongoose.connections[1];
And it works fine.
So the actual question is, what will happen if client 1 approach the app, select to connect dbNum1 and starts to work,
then client 2 approach the app and select to connect to dbNum2?

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).
However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.
My setup:
Creating a function app using all defaults through the Azure portal.
Creating a package.json with mongodb version 3.x.
Running npm install through the Kudu shell
Include the require statement in my code.
Make a request to the function
This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).
I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.
After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.
However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!
I don't have a full code example right now, as I'm away from my work computer, but the code is something like
const mongo = require('mongodb');
module.exports = function(context) {
context.res = {
body: 'Hello world'
};
context.done();
}
Without the require statement, the code runs fine, returning the response to the browser.
It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.
Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work
var mongodb = require('mongodb').MongoClient;
you have it as:
const mongodb = require('mongodb');
Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.
Here is the tutorial I found: MongoDB app using Node.js

node express postgres - why and how to connect to database using pg module?

I am super new to node express and postgres and wondering the following:
const pg=require('pg').native
const client=new pg.Clirnt('postgres ...')
what is const?
pg is used to create a client to connect to the Postgres database-correct?
If so
var db = new Sequelize('postgres://localhost:5432/mydb')
would work too or would I just have created a database without connecting it?
Why exactly do I need to connect at all-to do what?
Thanks a lot!
const is constant in javascript which was introduced in ES6 specification.
node-postgres is a client for PostgreSQL.
Sequelize is using node-postgres for working with PostgreSQL database, so yes, in the nutshell, it will act like node-postgres.
Imagine the warehouse, that's your database, where you have different shelves, that's your tables, to take or put different items into warehouse you need workers that will do your instructions like - INSERT someitem INTO items_shelf;. So worker is the client like Sequelize or node-postgres. The important part, warehouse should be open, otherwise, workers couldn't access to it, so your database should be turned on.
Hope I'm explained understandable enough.
what is const?
TLDR; variables that can't be re-assigned. scoped the same way as var. Part of es6.
pg is used to create a client to connect to the postgres database?
yes, note you need to do npm install --save pg as well as npm install --save sequelize. the save flag adds the packages to the package.json file for your convenience.
would I just have created a database without connecting it?
That bit of code should instantiate a connector - you haven't modified the database, and you also don't really know if the connection works yet.
why exactly do I need to connect at all?
The pg library looks to use a connection pool; this means you set it up once, and then you use it repeatedly as desired and it handles the connections for you. You connect now so you can run queries against the database later.
This snippet of code connects to a postgres instance running locally on my machine, and tests that it can connect - per the docs
const Sequelize = require('sequelize');
var sequelize = new Sequelize('postgres://localhost:5432/postgres');
sequelize.authenticate().then(() => {
console.log('yay');
}).catch((e) => {
console.log('nooo', e);
});

Mongoose calls hangs

I haven't worked on my PC for few days.
Suddenly all the calls to mongo via mongoose hangs up, the callbacks are not called.
I checked that my call to .connect works, and that the connection state is 1 (connected).
I also made sure mongo service is running on localhost and the appropriate port 27017, and I can use the mongo console and query the db manually.
I also scanned the Internet for solutions but all I found was 'check that you're actually connected', and I verified that already.
Mongoose version 2.15.0, mongo version 2.4.9 and node js version is 4.4.2.
I fixed it.
Problem was duplicate references to the mongoose module.
I had a mongoose reference locally (which was connected), but my schema was present higher in the node_modules hierarchy, and it have used another mongoose instance which had no connection.
Once I removed the duplicate mongoose modules (npm uninstall mongoose one of them) it worked.
Above solutions didn't work for me so I fixed with following solution.
I had same issue where my db calls used to hang with no invocation to my callbacks or the promise resolution.
The problem was I used "createConnection()" to establish the connection with the db. But it didn't work perfectly.
Instead using "connect()" and "connection" imports worked.
Here is the sample code. Hope this helps.
import { connect, connection } from "mongoose";
const mongoUri = `mongodb://${my_mongo_host_&_port}`;
connect(mongoUri, {}); //to connect to my standalone db
//"connection" to listen to events
connection.on("connected", () => {
console.log("MongoDB connection established!", mongoUri);
});
I am working with "mongoose": "^6.6.1".

Is mongoose.Types.ObjectId() an absolutely Node.js client-side operation?

I'm using the Mongoose next to the Node.js. The question is:
Does the following command for creation of the ObjectID value make a call to the server?
mongoose.Types.ObjectId()
I've checked my local MongoDB server log, and it doesn't show anything like a call to the single local MongoDB node/server for requesting a new ObjectID value. However, I'm not sure if the default server log is about all the operations(trivial and essential ops), or not!
NOTE: trivial ops here means the non-manipulative data operations!
A very simple script suggests that it does not make a call to the server:
Installing mongoose by running npm install mongoose
Then make a 2-liner index.js:
var mongoose = require('mongoose');
console.log(new mongoose.Types.ObjectId);
Because we haven't even connected yet.

Resources