Use multiSubnetFailover in sequelize and mssql packages - node.js

I have a backend application (Node.JS) that uses mssql (v7) and sequelize (v6) npm packages.
Since my production DB configuration is (and can be only) accessed by an AGL, hence I need to set multisubnetfailover=true in the DB connection string.
Although support for this existed in previous versions, I am unable to find the same in the current stable versions of the respective packages. (Here's a sample code for previous sequelize and mssql version)
Is there a way to enable this in the newer version?

For sequelize (v6):
Solution:
...existing sequelize configuration
dialect: "mssql",
dialectOptions : {
options: {
multiSubnetFailover: true,
}
}
...
Approach:
I was looking at the source code links sent by #AlwaysLearning in the comments above, and found that, if multiSubnetFailover's value is not a boolean, an error is thrown.
I then updated my configuration to dialectOptions.multiSubnetFailover : "1234", however I did not get the TypeError I was expecting. Then I looked at some more code and found that multiSubnetFailover should be used inside dialectOptions.options.
For mssql (v7):
Got help from a contributor.
...existing mssql configuration
options: {
multiSubnetFailover: true,
}
...
Cheers!

Related

Connecting Sequelize to MSSQL with Windows Authentication

I need help with the right libraries to connect Sequelize to MSSQL database using Windows Authentication.
I have a requirement for a client where I cannot use passwords to connect to the database on the server. Their required method of use is to connect to MSSQL database using Windows Authentication.
The problem I have is that we are using Sequelize and the only Dialect using msnodesqlv8 (which supports Windows Authentication) that I was able to find is not maintained any more. https://www.npmjs.com/package/sequelize-msnodesqlv8
Tedious which is the default dialect for Sequelize does not support Windows Authentication without password. It has the option of using ntlm, but it also requires a password.
Unfortunately, after a lot of searching for solutions, I was unable to find any. The only two viable solutions are to either build a dialect library for Sequelize using msnodesqlv8 or to create a custom version of tedious driver using sspi-client library.
I ended up with the later approach of custom version of tedious driver with sspi-client https://www.npmjs.com/package/#sregger/sspi-client thanks to some legacy code samples and help from Tediousjs community. One word of caution is that if you are using sspi-client, Worker will not work. To use Worker, use custom library https://www.npmjs.com/package/shinobi-worker otherwise you will get the error of "Module did not self-register"
I found the solution with this configuration:
database: 'DB_NAME',
host: 'DB_HOST',
dialect: 'mssql',
dialectOptions: {
authentication: {
type: 'ntlm',
options: {
userName: 'DB_USER',
password: 'DB_PASS',
domain: 'MY_COMPANY_DOMAIN',
},
},
options: {
port: 1433,
requestTimeout: 60000,
},
},
moreinfo: https://github.com/vishwasjois/sequelize/blob/988e754c6eef323b1a9dc11f5bee3fb535579da8/docs/upgrade-to-v5.md#dialect-specific
Hope this help

Unable to set up sample React project correctly?

I got an interview assessment next week and they provided me a sample code project of what to expect. The problem is I'm having a LOT of trouble simply setting it up to run as expected...
Edited: Removed project for privacy
I cloned the project and installed all dependencies.
Problem 1
I followed the README in which I created a Database called messenger and then created a .env file in the server directory. The problem is I can't retrieve any values from that .env file through process.env.REACT_APP_CUSTOM_VAR. Console.logging "process.env" does not show ANY custom variables. It's as if they weren't even created...
Problem 2
On the frontend side, it should have been simple (npm install and then npm start). When starting, I found that there was NO CSS applied (despite reading the code and there's Material UI used). When I inspected the page, I'm getting the error 'The server responded with a status of 431 (Request header fields too large).
I have a hard time believing the company gave me super broken code to the point where I can't even run the sample code properly... Can anyone please help me and try installing the code project above? Please let me know if you got the same problem or found any solutions!
Submit an issue requesting that they add .nvmrc and package-lock.json such that you can ensure your machine is prepared to absorb dependencies and properly posture with the correct version of node. It's a crap-shoot of testing various versions of node against their build otherwise.
Your best bet is to check the first publish date of the client (javascript) code against the latest versions of node from that time. It sucks, but that's what I would do.
The project runs fine.
Theres not really styling applied, but that seems to be intentional.
Most likely you would benefit from using whatever database you have access to that already works (in my case, mssql) by changing the db.js
const db = new Sequelize('dbname', 'user', 'password', {
host: 'host',
port: 1433,
logging: false,
dialect: 'mssql',
dialectOptions: {
encrypt: true,
},
});
What you should however not do if you want the job, is critique it. It works just fine as the baseline for a recruitment assignment. Your enviroment is the issue in this case.
Do not forget to run the seed on the backend.
For the problem 1 on the server, I've create an .env file on the ./server directory and logging process.env logged the variables from that file. Also, on the backend, you don't need the REACT_APP_CUSTOM_VAR prefix when accessing environment variables.
For the problem 2 on the frontend, after the instalation, the client loaded the css from mui properly. I'm using node 14.17.0.

Connecting to a postgresql database with Sails 1.0.0?

I just decided to upgrade an existing Sails project to 1.0.0 and managed to get through most of the upgrade process until I tried connecting to the database. When attempting to lift this is the error I receive:
The adapter used by the default datastore is not compatible with
the current version of Sails/Waterline.
The adapter should expose a valid adapterApiVersion.
If you're using the beta release of Sails 1.0, try:
npm install --save sails-disk#beta
My datastores.js file contains the following:
module.exports.datastores = {
// In previous versions, datastores (then called 'connections') would only be loaded
// if a model was actually using them. Starting with Sails 1.0, _all_ configured
// datastores will be loaded, regardless of use. So we'll only include datastores in
// this file that were actually being used. Your original `connections` config is
// still available as `config/connections-old.js.txt`.
'developmentPostgres': {
adapter: require('sails-postgresql'),
url: process.env.postgresHost,
user: process.env.postgresUser,
password: process.env.postgresPassword,
database: process.env.postgresDatabase
}
};
Where all of the values are provided as environment variables. "sails-postgresql": "^1.0.0" is installed and saved in package.json
models.js also contains the following line: datastore: 'developmentPostgres', which to my understanding means that all of my models should be using the above database by default.
Up until this point the upgrade process has been very straightforward. I assume I am just missing something simple here as well. I would appreciate any insight anyone can provide and would be happy to provide any additional information as well.
Turns out the project looks for a datastore labeled default. Changing the name to default in both datastore.js and model.js solved the issue.

Relation does not exist error with pg-promise and postgresql?

I am using pg-promise on Node 7.2.1 with PostgreSQL 9.4. I am using the code below to connect to the PostgreSQL server:
// pg-promise connection elements.
// Database initialization options.
const pg_options = {
// Initialization options.
};
// Create an instance of the pg-promise library.
const g_pg_promise = require('pg-promise')(pg_options);
// Connection string for a local connection to the 'summarize' database.
const g_pg_connection =
{
host: 'localhost',
port: 5432,
database: 'db_stats',
user: 'db_user',
password: '{password here}'
}
// Connect to the database.
const g_pg_database = g_pg_promise(g_pg_connection);
// Expose the database object globally. The database object should only
// be instantiated once and then shared.
module.exports = {
g_pg_promise: g_pg_promise,
g_pg_database: g_pg_database
};
I know the connection parameters are valid because I use the exact same values in other non-Node.JS apps to connect to the same PostgreSQL server. I also know that db_stats is a valid database name. I have worked with that database for quite a while with other non Node.JS apps and via pgAdmin 3.x.
However, when I attempt to connect using pg-promise I get the following error:
error: relation "db_stats" does not exist
I did see the SO post below:
Unable to query PostgreSQL database in NodeJS using pg-promise - "relation does not exist"
But that post did not make sense to me because I believe Vitaly, the author of pg-promise, was telling the poster that he did not have a table called users, when it looks to me that the poster was trying to access the database called users and he definitely had a database with that name.
In any case, I definitely have a database named db_stats so I'm not sure why I am getting this error. How can I solve this?
error: relation "db_stats" does not exist
The error you get from PostgreSQL is not referring to a database named "db_stats", but to a relation (either a table or a view) with that name.
That is, db_stats most probably appears in the FROM clause of a SELECT query (although it may be also a INSERT, UPDATE, DELETE, ...).
There is neither a db_stats table nor a view in your database. Or, maybe, it exists, but it exists in a schema that is not part of your current search_path.
To find out, check two things:
SELECT
*
FROM
information_schema.tables
WHERE
table_name = 'db_stats' ;
If there is a table there... you already know which schema(s) contains it/them.
Then, make sure that this schema is part of your search_path by means of:
SHOW search_path ;
You might have to execute this SQL statements by adding the adequate code to your application, and use a debugger to check what's returned in that environment.

Sails Upgrade v10.5 Waterline "Migrate - safe" option gives errors?

When I upgraded Sails from V0.9 to V0.10 I ran into trouble with Waterline. I have exhausted my options as far as finding the solution online. The problem is:
Sails v0.10 ask for the migrate property to be set and I do set it inside my models.js config file - migrate:"safe".
My server console keeps giving me the following error:
node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:77
Object.keys(attrs[attr]).forEach(function(prop) {
TypeError: Object.keys called on non-object
Did anybody else run into this problem?
I realized after a bit that I had earlier added the following to my models. After removing it, I was able to run sails lift with no problems. Maybe check your models to ensure that everything is kosher.
autoCreatedAt: true,
autoUpdatedAt: true

Resources