I'm using express-session along with passport and MongoStore to store my sessions. They're obviously being stored in the MongoDB, how can I access them just to take a look at? There's no business value in this it's just from a theoretical/technical perspective that I'm interested, thanks!
Run mongo shell with command:
mongo
Create a new connection:
conn = new Mongo()
Access your DB:
db = conn.getDB('myDB')
This command displays the sessions (inside the inprog array, look at the objects with client):
db.currentOP(true)
Related
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?
I am learning NodeJS by building a JWT server. Basically I want to authorize users against credentials in a PostgreSQL database. I am considering node-postgres, passport, pg to connect with PostgreSQL but I have not found anyway to store my connection values encrypted. Ideally I would store them in a properties file so I can change them per environment.
Most examples I see do something like:
var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword#localhost:5432/YourDatabase";
Can someone help show me how to encrypt and use my credentials so I don't have to hard code the plain values in my source?
There seem to exist npm packages for this already. See https://www.npmjs.com/package/secure-conf. Seems to fulfill your needs.
Please note, that you should also secure your Connection to the DB using SSL. See SSL for PostgreSQL connection nodejs for a Solution.
This should help.
if you use sequelize to connect postgres
const sequelize = new Sequelize("DB", usrname, password, {
host: "/var/run/postgresql",
dialect: "postgres",
});
NB: get the host string from your pgsl db might be different //
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);
});
I read that in a nodejs application, if we use a mongoose object using
var obj = require('mongoose');
then, wherever we create a new mongoose object (say, in another file), then the same cached copy is used throughout. In that case, how can we connect to multiple database instances of mongoDB in the same nodejs app ?
You can use mongoose.createConnection to connect to a separate database, and then using the connection object that generates, you can define models from that database using connection.model.
I am building a nodejs app using expressjs framework. I would like to ask how do we connect to mongodb using monk? i found this code online however it seems that we do not need to specify username and password. why is that so?
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');
Appreciate any advice.
There are two methods of passing username/password:
// first:
var db = monk('USERNAME:PASSWORD#localhost:27017/nodetest1');
// second:
var db = monk('localhost:27017/nodetest1', {
username : 'USERNAME',
password : 'PASSWORD'
});
It's not very well documented, but since monk uses mongoskin, you can look here for more information.
I want to add an alternative to the answers above as neither worked for me.
So the way I was able to connect to a mongod --auth instance was:
1.Make sure you create a user while logged into mongodb as a user with appropriate permissions to create users on databases (like a user with the role userAdminAnyDatabase). If you don't have a user with this role restart mongo with just mongod without --auth and login as your superuser (if you have one) or with a simple mongo command and make this user with the appropriate privileges.
I highly recommend reading up on mongodb roles here: http://docs.mongodb.org/manual/reference/built-in-roles/
2.Once you are logged into your database instance with the appropriate user who is supposed to be creating other users, type use db_name where db_name is the name of your database that you want to make the user for.
3.Once inside this database use the db.createUser command like so: db.createUser({user:"your_username", pwd:"your_password", roles:["your_role"]})
It's documented here: http://docs.mongodb.org/manual/reference/method/db.createUser/
4.Then just use db = monk('USERNAME:PASSWORD#localhost:27017/DATABASE_NAME') in your code.
After you've done this you should be able to log into a running mongod --auth instance with a user for the particular database you created that user for.
For anyone curious I'm developing a little web app in Koa. Using monk and co-monk.
In apps.js:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/ta');
module.exports = db;
In /lib/mylib.js:
var db = require('../apps.js')
Though i would recommend making a separate file called db.js and in that file export the connection ,that is more cleaner.