Managing MongoDB databases connection Based on Users - node.js

Good Morning Everyone .
for my project I'm Using mongo Atlas DB + Nodejs + express js
Currently let say I have one customer and all his users(managed with local-passport-js) and data are managed inside one DB in my cluster.
I'm Interested managing my project a step forward and create multiple DB's one for each costumer .
every costumer will have a different DB managing his users and data.
this is my current connection to the Db:
const MongoUrl = `mongodb+srv://*myuser**:${process.env.password}#testdb.9unfjt9.mongodb.net/?retryWrites=true&w=majority`
mongoose.connect(MongoUrl,
{ useNewUrlParser: true, useUnifiedTopology: true})
.then(()=> {
console.log('DB Connection, Successful')
})
.catch(err => {
console.log('Error connecting DB')
console.log(err)
})
How can I Create "Global" User Collection and determine which DB the user Associate to and load it.
Thanks !

Related

How to get data from an API into a database with different schemas?

Suppose you have an MS SQL Server which has a Database A {id, name, age} and I need to take this into an REST API. Now I want to map the values into another Database B{student_id, student_name, student_age} in PostgreSQL . How do I do it?
Also assume I have made the API from Database A and now only mapping is required.
The API is as follows:
API goes like this!
I have read about spring boot one to one mappings but I have no idea how to do it.
Hello this is Gulshan Negi
Well, you can extract the data from the MS SQL Server database, transform it to match the schema of the PostgreSQL database, load it into the PostgreSQL database, and then set up a REST API to query the data and return the desired results in order to map values from an MS SQL Server database to a PostgreSQL database and expose it through a REST API. Before loading the data into PostgreSQL, it is essential to ensure that it has been accurately transformed and that any potential issues, such as naming conflicts or mismatches between data types, have been resolved.
Thanks
To map the values from MS SQL Server to PostgreSQL database using NodeJS, you can follow the following steps:
Connect to the MS SQL Server using the appropriate library for NodeJS
(for example, 'mssql' library).
Execute the SELECT statement on the 'Database A' and get the results.
Close the connection to the MS SQL Server.
Connect to the PostgreSQL database using the appropriate library for
NodeJS (for example, 'pg' library).
Use the results obtained from the MS SQL Server to construct INSERT
statements and execute them on 'Database B' in PostgreSQL using the
appropriate library for NodeJS.
Here is some example code to get you started:
const mssql = require('mssql');
const pg = require('pg');
// Connect to the MS SQL Server and execute the SELECT statement
const config = {
user: 'username',
password: 'password',
server: 'mssqlserver',
database: 'A'
};
mssql.connect(config, (err) => {
if (err) {
console.log(err);
return;
}
const request = new mssql.Request();
request.query('SELECT id, name, age FROM table', (err, result) => {
if (err) {
console.log(err);
return;
}
// Close the connection to the MS SQL Server
mssql.close();
// Connect to the PostgreSQL database and execute the INSERT statements
const pgConfig = {
user: 'username',
password: 'password',
host: 'postgresqlhost',
database: 'B',
port: '5432'
};
const pgClient = new pg.Client(pgConfig);
pgClient.connect();
result.recordset.forEach((row) => {
const query = {
text: 'INSERT INTO table(student_id, student_name, student_age) VALUES ($1, $2, $3)',
values: [row.id, row.name, row.age]
};
pgClient.query(query, (err, result) => {
if (err) {
console.log(err);
}
});
});
// Close the connection to the PostgreSQL database
pgClient.end();
});
});

Create new database with API call

I want to create a new database at the time of the API call. I am already connected to one main database.
I have tried using mongoose.connect() method. And it gives me a positive response. But when I checked in mongo console I did not find the newly created database.
Here is my code.
const connectionString = `mongodb://${process.env.DB_HOST}:${
process.env.DB_PORT
}/client_${Math.random()
.toString(36)
.substring(2, 8)}`;
mongoose.connect(
connectionString,
{
autoReconnect: true,
reconnectTries: 60,
reconnectInterval: 10000,
useNewUrlParser: true,
useCreateIndex: true
},
(error) => {
if (error) {
console.log(error);
} else {
console.log('Connected');
}
}
);
Hope you got my point. Looking for a solution.
As per mongodb documentation:
If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell. link
This is true for mongo-shell as well, if you do use mydb; to create one and immediately do show dbs; it won't show up unless you create some data for that mydb like createCollection(), insert() etc.
So your code is alright because connect() is as good as use db;. Unless you create some data for that db you won't see that in mongo console.

How to connect to mongoDB Atlas using mongoose

I'm trying to connect to my cluster on mongoDB Atlas via Mongoose.connect(), but every time i try to connect i get an exception "MongoError: authentication fail"
I know MongoDB Atlas is new mongo as a service could it be not supported by mongoose yet?.
The answer in this related post is correct. You should:
not mix options with connection string (if done so)
make sure your IP you are running on is whitelisted and your network allows connections to Atlas
make sure the user has sufficient permissions
use the connection string as is provided by atlas and just provide it to
mongoose.connect(uri);
MongoError: authentication fails - It means your name or password or dbname is not correct -
uri sample -
const uri =
"mongodb+srv://<username>:<password>#firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
Suppose username is - najim & password is 1234 & dbname is pets (Note - default dbname is test but you can write whatever you want) then my uri will be with above credentails -
const mongoAtlasUri =
"mongodb+srv://najim:1234#firstcluster.4rc4s.mongodb.net/pets?retryWrites=true&w=majority";
to connect with moongoose
try {
// Connect to the MongoDB cluster
mongoose.connect(
mongoAtlasUri,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log(" Mongoose is connected")
);
} catch (e) {
console.log("could not connect");
}
const mongoAtlasUri =
"mongodb+srv://<username>:<password>#firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
try {
// Connect to the MongoDB cluster
mongoose.connect(
mongoAtlasUri,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log(" Mongoose is connected"),
);
} catch (e) {
console.log("could not connect");
}
const dbConnection = mongoose.connection;
dbConnection.on("error", (err) => console.log(`Connection error ${err}`));
dbConnection.once("open", () => console.log("Connected to DB!"));
try {
mongoose.connect( uri, {useNewUrlParser: true, useUnifiedTopology: true}, () =>
console.log("connected"));
}catch (error) {
console.log("could not connect");
}
this works fine , try it
"mongodb+srv://:#cluster0.vvkuk.mongodb.net/"
also in the atlas, in security go to network access, there will be small buttons edit and delete click on edit, and in the edit, there will to two options the first option is ADD CURRENT IP ADRESS & the second option will be ALLOW ACCESS FROM ANYWHERE
go for the first option & then click confirm

DB Connection close issue in Node.js using trireme-jdbc

I have developed and deployed a Node.js application in apigee edge, which performs few CRUD operations. To establish db connection I have used trireme-jdbc module of node.js. I am able to perform all CRUD operations well through trireme-jdbc but I have problem with DB session close using db.close() function in trireme-jdbc. When I use db.close() it do not close currently active/open session from the pool. I want to know is there any other process or way to close db connection perfectly. Also I want to close all active connections from pool.
Any help will be appreciated. Below is the sample code to establish connection, run a select query and used db.close() to close session. My database is Openedge/Progress.
var jdbc = require('trireme-jdbc');
var db = new jdbc.Database({
url : connectionURL,
properties: {
user: 'XXXXXX',
password: 'XXXXXX',
},
minConnections:1,
maxConnections:2,
idleTimeout:10
});
db.execute('select * from users ', function(err, result, rows) {
console.log(err);
console.log(result);
rows.forEach(function(row) {
console.log(row);
});
db.close(); // Used to close DB session/connection.
});

How to reconnect mongoose to another remote server?

I am totally new to node.js and mongoose, how to reconnect mongoose to another remote server ?
At the beginning of the file I have
var mongoose = require('mongoose')
and connected to localhost,
later in code I have
var uristring ='mongodb://remote_server/db';
var mongoOptions = { db: { safe: true } };
// Connect to Database
mongoose.createConnection(uristring, mongoOptions, function (err, res) {
if (err) {
console.log ('ERROR connecting to: remote' + uristring + '. ' + err);
} else {
console.log ('Successfully connected to: remote' + uristring);
}
});
and I always get Successfully connected to: remote but when I bellow that print look for document by id I get always from local database(I have schema imported like require Person = mongoose.model('Person');).
How to reconnect to remote if I already have connection to local.
There are two ways of initializate a connection in mongoose:
Using the default connection "object"
Creating a connection from the dust
Here you are creating a connection, but using a model from another. Models are tied to databases (normal databases, replica sets or clusters) so you're not accesing to the correct host.
You must use the default connection (using mongoose.connect instead of mongoose.createConnection) or create a model in that new connection you are using. In your example:
var uristring ='mongodb://remote_server/db';
var mongoOptions = { db: { safe: true } };
// Connect to Database
var newConnection = mongoose.createConnection(uristring, mongoOptions);
newConnection.model(/*whatever*/);
mongoose.model wires to mongoose.connection. That is not the new connection you have created.

Resources