AdonisJS 5, dynamically connect database - node.js

Hi used Adonis Js 5 with the new version. I have multiple database clients with the same database structure, can I create a new database connection without declaring the config at config/database.ts? it means I can create a connection on the fly.

Yes finally I can make it, with this:
Database.manager.patch(this.database.name, {
client: 'mysql',
connection: {
host: Env.get('DB_HOST'),
port: Env.get('DB_PORT'),
user: this.database.username,
password: this.database.password,
database: this.database.name,
},
debug: Env.get('DB_DEBUG', false),
})
Database.manager.connect(this.database.name)

Related

SQL Server and NodeJS - Connection Pool availabilty is always zero

I'm trying to initialize a pool of SQL Server connections for my nodejs web application to use. I've set the config to create 10 min connections but when I start the app with the below code. I only have 0 available connections which doesn't allow me to begin any transactions.
Any help would be greatly appreciated!
test.js
const sql = require('mssql');
require('dotenv').config();
const appPool = new sql.ConnectionPool({
user: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
server: process.env.DB_HOST,
pool: {
min: 10,
max: 100,
acquireTimeoutMillis: 15000,
},
options: {
encrypt: true,
trustServerCertificate: false
}
});
appPool.connect().then(pool => {
console.log(`SERVER: Connected to the db and ${pool.available} connections are available!`);
});
Output
MINGW64 ~/Desktop/React Projects/dummy-project (master)
$ node test.js
SERVER: Connected to the db and 0 connections are available!
node-mssql uses tarn.js for connection pooling. The connections are not created preemptively. Instead, they're created as and when the connections are requested. Once the number of concurrent requests for connections exceed pool.min, the available connections in the pool stays at that level.
So, doing a transaction or running a query shouldn't be a problem, node-mssql will create the connection and fulfil the request.
If you really want to have pool.min connections in your pool, you can write a small script to fire some concurrent queries on your database which will warm up the pool for you.

NodeJS mysql2 - should I handle pool disconnections?

I use mysql2 module in my NodeJS project.
I understand the concept of database pooling in mysql2 module
(https://www.npmjs.com/package/mysql2).
Before using pool, I used regular connection with mysql2.createConnection() function,
but after some time I got 'PROTOCOL_CONNECTION_LOST' error.
My code is:
db.js:
const mysql = require('mysql2');
const pool = mysql.createPool({
host: 'sql server',
user: 'username',
database: 'database',
password: 'pass',
port: 3306,
connectionLimit: 10,
queueLimit: 0
});
module.exports = pool;
And how I using it:
const db = require('db');
db.query(...);
The query() and execute() functions on pool instance automatically call release() function of pool instance, and that is very good (because I don't need to write that command manually after any query).
But I need to understand: if I work like that (with pool, instead of without pool), there is a guarentee
'PROTOCOL_CONNECTION_LOST' will not be thrown? Should I handle that or the pooling mechanism does it automatically?
I ask because I saw on the internet some code that, for example, re-create the connection.
With pool, I need to re-create the connection sometime?
Thanks a lot!

Include Amazon RDS endpoint in Node JS error: An identifier or keyword cannot immediately follow a numeric literal

I was trying to connect my Node JS application with Amazon RDS following this tutorial Adding Node JS to RDS application, in my case I have the following:
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.factsDB.abcdefg.eu-west-2.rds.amazonaws.com,
user: process.env.johnDoe,
password: process.env.password123,
port: process.env.3306,
database: 'factsDB',
debug: false
});
However it gave me the following error:
An identifier or keyword cannot immediately follow a numeric literal
at rds at the host, and another error
',' expected
at env.3306 at port, any idea how to fix these?
The process.env in the link provided is used to read environmental variables. However, from what I understand, you provide your db details directly into createPool.
Thus you can try the following:
const pool = mysql.createPool({
connectionLimit: 10,
host: "factsDB.abcdefg.eu-west-2.rds.amazonaws.com",
user: "johnDoe",
password: "password123",
port: 3306,
database: 'factsDB',
debug: false
});

Using Connection URIs with Read Replication in Sequelize

If I have a connection URI, I can use that normally with Sequelize as such:
const sequelize = new Sequelize('postgres://user:pass#example.com:5432/dbname');
However, if I want to use Read and Write replication (https://sequelize.org/master/manual/read-replication.html), then there doesn't seem an option to use connection URI. Can I pass connection URI strings to read and write in the replication option as in:
const sequelize = new Sequelize(null, null, null, {
dialect: 'postgres',
replication: {
read: [
'postgres://user:pass#reader.example.com:5432/dbname',
'postgres://user:pass#anotherreader.example.com:5432/dbname'
],
write: 'postgres://user:pass#writer.example.com:5432/dbname'
}
})
EDIT:
I have already found a solution to the issue. and that is using an npm library like connection string to parse the connection string as shown below:
const write_uri = new ConnectionString(uri);
const sequelize = new Sequelize(null, null, null, {
dialect: 'postgres',
replication: {
read: [
'postgres://user:pass#reader.example.com:5432/dbname',
'postgres://user:pass#anotherreader.example.com:5432/dbname'
],
write: {
host: write_uri.hosts[0].name,
username: write_uri.user,
password: write_uri.password,
database: write_uri.path[0],
port: write_uri.hosts[0].port
}
}
});
But, that is not what I'm looking for.
As per sequelize source at master, you can't.
According to the sequelize source docs at sequelize.js, The Sequelize Constructor accepts options agrument, like this
constructor(database, username, password, options){
}
Where options.replication should be an object with two properties, read and write. Write should be an object (a single server for handling writes), and read an array of object (several servers to handle reads). Each read/write server can have the following properties: host, port, username, password, database.
you need to pass an array of objects to read:[] with connection values as props instead of passing strings.
You can pass config object to sequelize constructor even if you use uri connection.
Look the example in from the docs:
https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor
// with uri
const sequelize = new Sequelize('mysql://localhost:3306/database', {})
Look at the constructor overloading definition:
Sequelize(uri: string, options?: Sequelize.Options): Sequelize.Sequelize
Just pass the options you need.

Node.js - FTP through HTTP Tunnel (FTP over HTTP Proxy)

I am looking for an opportunity to connect FTP-Server via HTTP-Proxy-Server. I tried different npm packages (jsftp, ftp etc.), but the connection does not work. I have tried a few SOCKS packages but they seem outdated. Here is an example from the package jsftp. Unfortunately, the example does not work and I can not find any solution
const {SocksClient} = require('socks');
const ftp = new Ffp({
host: 'localhost',
port: 3333,
user: 'user',
pass: 'password',
createSocket: ({port, host}, firstAction) => {
return SocksClient.createConnection({
proxy: {
ipaddress: '159.203.75.200'
port: 1080,
type: 5
},
command: 'connect',
destination: {
host,
port
}
})
}
})
This solution worked for me. However, it uses platform-dependent packages and is only commercially available starting from USD 289.
This solution looked promising, but I couldn't get it to work properly. It created files I wanted to copy, but they always ended up being empty.

Resources