I am trying to connect the multiple database with the API request. I tried to connect the DB using this code:
const connectionString = `mongodb+srv://${process.env.DB_HOST}/${company.dbName}`;
const client = new MongoClient(connectionString, {
autoReconnect: true,
reconnectTries: 60,
reconnectInterval: 10000,
useNewUrlParser: true,
auth: {
user: process.env.DB_USER,
password: process.env.DB_PASS
}
});
await client.connect();
await client.db(company.dbName);
I didn't achieve any solution with the mentioned code.
Looking for the good solution :)
Related
I use Sequelize in my nodejs.app to load and save data.
To manange Sequelize I use:
(Sample model)
const { Sequelize, DataTypes } = require("sequelize");
const sequelize = require("../config/mysql");
const login = sequelize.define(
"login_bot",
{
login: {
type: DataTypes.STRING,
allowNull: false,
},
// Model attributes are defined here
password: {
type: DataTypes.STRING,
allowNull: false,
},
team: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
}
);
module.exports = login;
The config for this is:
const Sequelize = require("sequelize");
const sequelize = new Sequelize("xxx", "xxx", "xxx", {
host: "xxx.bplaced.net",
dialect: "mariadb",
pool: {
max: 25,
min: 5,
idle: 20000,
evict: 15000,
acquire: 30000,
},
});
sequelize
.authenticate()
.then(() => {
console.log("Connection to database has been established successfully.");
})
.catch((err) => {
console.error("Unable to connect to database:", err);
});
module.exports = sequelize;
In my code I use: as sample
const logindata = await login.findAll({
where: { team: usersdata[0].team },
attributes: ["login", "password"],
raw: true,
});
All works fine. But after some time I get "too many connections" from the server. So I want to ask whether the workflow is wring and I miss to close the DBConnection some where?
I have in all 4 models. All models with "..config/mysql.js" as reference. Do Sequelize open for each model a connection and keeps this open? Or just one connection for the whole app?
In all I just want to have one connection my app is using all the time.
Or do I have to close Sequelize somewhere at each call? Hope to get some hints here because iam lost at this moment.
When your application needs to retrieve data from the database, it creates a database connection. Creating this connection involves some overhead of time and machine resources for both your application and the database. Many database libraries and ORM's will try to reuse connections when possible, so that they do not incur the overhead of establishing that DB connection over and over again. The pool is the collection of these saved, reusable connections that, in your case, Sequelize pulls from. Your configuration of
pool: {
max: 5,
min: 0,
idle: 10000
}
reflects that your pool should:
Never have more than five open connections (max: 5)
At a minimum, have zero open connections/maintain no minimum number of connections (min: 0)
Remove a connection from the pool after the connection has been idle (not been used) for 10 seconds (idle: 10000)
I'm experiencing below error after upgrading to Node.js v16.4.0 (from Node.js v12).
I'm using knex to connect to db (postgres) and using ORM - objection.js.
KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
Please help to resolve!
import Knex from "knex";
const getConnection = () => {
try {
return Knex({
client: 'pg',
connection: {
user: 'user',
password: 'password',
host: 'dbhost',
port: 5432,
database: 'dbname',
},
pool: {
min: 0,
max: 10,
idleTimeoutMillis: 10000,
acquireTimeoutMillis: 30000,
},
acquireConnectionTimeout: 2000,
});
}
catch (err) {
throw err;
}
}
const getTransaction = () => {
const con = getConnection();
const trx = con.transaction();
return trx;
}
Having had the same issue, I traced the problem to an old version of the "pg" package.
Doing
npm i pg#latest
fixed it
I am using couchDB and when ever i try to create views it is giving me this error
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'EUNAUTHORIZED',
body: {
error: 'unauthorized',
reason: 'You are not a db or server admin.'
}
}
I am using Node-Couchdb and i am passing the credentials like this
const NodeCouchDb = require('node-couchdb')
require("dotenv-flow").config();
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT
})
const couchAuth = new NodeCouchDb({
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD
}
})
module.exports = {
couch
}
Your code is creating two instances of NodeCouchDB, couch and couchAuth where
couch points to server specified by envars without credentials
couchAuth points to the default server (127.0.0.1:5984) with credentials specified by envars
You need to combine parameters, for example
const NodeCouchDb = require("node-couchdb");
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT,
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD,
},
});
module.exports = {
couch
};
I'm trying to use postgres (pg) inside electron.
When the window opens, the connection and query works normally, but when the electron window is reloaded, pg connect and queries does not return anything.
<script>
const { Client } = require('pg')
async function query() {
const client = new Client({
host: '192.168.99.100',
port: 5433,
user: 'user',
password: 'password',
database: 'database',
})
console.log(await client.connect())
const res = await client.query('SELECT * from users')
console.log(res)
await client.end()
}
query()
</script>
PS: If i try to use Knex for connect and query the database, it returns this error:
Uncaught (in promise) KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
at Client_PG.acquireConnection
const sql = require("mssql");
var sqlconfig = {
server:"192.168.200.5",
database:"DATA",
user: "labo",
password: "*********",
connectionTimeout: 30000,
port: 1433,
dialect: "mssql",
dialectOptions: {
"instanceName": "SQLEXPRESS"
},
};
new sql.ConnectionPool(sqlconfig).connect().then(pool => {
console.log(pool); return pool.query('select * from data where id = 2')
}).then(result=> {
console.dir(result)
}).catch(err => {
console.log(err);
})
Problem: Return Login failed for user 'Labo' ELOGIN when trying to login to Microsoft SQL Server using node.js.
What I have tried: Grab the example codes on npm to find the avoid extra problems. Error still persisted.
What I suspect: The username I inserted was "labo" but the one returned from err was 'Labo' (with capitalized 'L'). Is this a bug?
This is my code