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)
Related
Since I have given the pool parameters as mentioned in the below code snippet, Is necessary to close the connection after each lambda invocation?
This aws lambda function is supposed to be triggered every minute, is the pool parameter only enough to close the connection?
export const databaseProviders = [
{
provide: 'vendorDB',
useFactory: async (awsSecrets: AwsSecretsService) => {
const { host, port, username, password, database } =
await awsSecrets.getVendorDbConfig();
const sequelize = new Sequelize({
dialect: 'postgres',
host,
port,
username,
password,
database,
logging: true,
pool: {
max: 1,
min: 0,
idle: 1000,
},
});
sequelize.addModels([VendorEntity]);
// await sequelize.sync();
return sequelize;
},
inject: [AwsSecretsService],
},
I'm trying to connect my electron app to a database, but I keep getting the same error
ETIMEOUT
or that the user don't have access
But if I try with my local database works fine, any ideas of why is this happening?
var mysql = require('mysql');
// Add the credentials to access your database
var connection = mysql.createConnection({
host : 'xxx.xxx.xxx',
user : 'xxxxx',
password : 'xxxxxxx',
database : 'xxxxx',
pool: {
max: 1000,
min: 0,
idleTimeoutMillis: 30000000
},
options: {
"enableArithAbort": false,
"encrypt": false
},
debug: true
});
// connect to mysql
connection.connect(function(err) {
if(err){
console.log(err.code);
console.log(err.fatal);
} else {
console.log("OK");
}
});
I'm using node.js with express and sequelize and my DB is mysql.
I tried to create a class model as written in the documentation: https://sequelize.org/master/manual/model-basics.html. I was able to connect to the DB, but couldn't figure out how to sync the model with the DB in order to create tables. here is my user model:
const { DataTypes, Model } = require('sequelize');
const connection = require("../server");
export class User extends Model { }
User.init({
id: {
type: DataTypes.BIGINT,
autoIncrement: true,
primaryKey: true,
unique: true,
allowNull: false
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
userType: {
type: DataTypes.STRING,
allowNull: false,
}
}, {
connection
});
and here is my server.js:
const express = require("express");
const Sequelize = require("sequelize");
const app = express();
const port = 8080;
const connection = new Sequelize("coupons2", "root", "1234", {
host: "localhost",
dialect: "mysql"
})
connection.sync()
.then(() => {
console.log("Connection to DB was successful");
})
.catch(err => {
console.error("Unable to connect to DB", err);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
})
module.exports = connection;
Thank you!
I think you forgot to require the Sequelize as shown in document you mentioned above
`const {Sequelize, DataTypes, Model } = require('sequelize');
and if you want that it table create automatically by models you can use sequelize.sync in your project
await sequelize.sync({ force: true });
console.log("All models were synchronized successfully.");
User.sync() - creates the table if it doesn't exist (and does nothing if it already exists)
User.sync({ force: true }) - creates the table, dropping it first if it already existed
User.sync({ alter: true }) - checks what is the current state of the table in the database (which columns it has, what are their data types, etc), and then performs the necessary changes in the table to make it match the model.
(async () => {
await sequelize.sync({ alter: true });
})();
I managed to create tables by importing the models to my server.js:
const models = require("./src/models/index");, and using the sync() method.
I write the code below. According to my understanding the "max:" in pool shows the max number of connection we can create in pool. So if I set the value of max as 0. Then it should not established any connection. I just want to clarify this.
var sequelize = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD, {
pool:{
max: 0,
min: 0,
idle: 10000,
acquire:30000
},
logging: console.log("connection created"),
host: process.env.DATABASE_HOST,
dialect: 'mysql',
operatorsAliases: false
}
);
I have no idea about Sequelize but simply we can create a connection this way.I think this will be little bit helpful for you.
/**
* create connection pool
*/
var con = mysql.createPool({
connectionLimit: 100000,
host: 'enter host',
user: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME
});
con.getConnection(async function(err, connection) {
let result=connection.query('SELECT * FROM table_name');
console.log(result);
if(err) throw err;
})
what's wrong with the way they have it set up in their examples exactly?
const sequelize = new Sequelize(config.db.uri, {
pool: {
max: 100,
min: 0,
idle: 10000,
},
dialectOptions: {
ssl: config.db.ssl,
},
// logging: logger.debug.bind(logger),
});
the models utilize it on their own... what am i missing here?
For "sequelize": "^5.21.3", you can't set pool.max to 0 anymore. If you do this, sequelize will throw an error:
max must be an integer > 0
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 :)