Sequelize "Connection closed" on Azure SQL - node.js

I have a Node v10.17.0 app on Ubuntu 16.04 using Sequelize 4.41.1 calling Azure SQL.
On every start up, the node app runs sync on each database model for any migrations. Sequelize sends queries to the database IF OBJECT_ID('[Shop]', 'U') IS NULL CREATE TABLE [Client] ... etc to create the tables if they don't exist. Then EXEC sys.sp_helpindex #objname = N''[Shop]. The first couple of tables sync successfully (there aren't any changes).
Then we get to the query that returns the error SequelizeDatabaseError message":"Connection closed before request completed." "code":"ECLOSE"
IF OBJECT_ID('[Client]', 'U') IS NULL CREATE TABLE [Client] ([id] NVARCHAR(255) , [name] NVARCHAR(255) NOT NULL, [timezone] VARCHAR(255) CHECK ([timezone] IN(N'Africa/Abidjan', N'Africa/Accra', N'Africa/Addis_Ababa', N'Africa/Algiers', N'Africa/Asmara', N'Africa/Asmera', N'Africa/Bamako', N'Africa/Bangui', N'Africa/Banjul', N'Africa/Bissau', N'Africa/Blantyre', N'Africa/Brazzaville', N'Africa/Bujumbura', N'Africa/Cairo', N'Africa/Casablanca', N'Africa/Ceuta', N'Africa/Conakry', N'Africa/Dakar', N'Africa/Dar_es_Salaam', N'Africa/Djibouti', N'Africa/Douala', N'Africa/El_Aaiun', N'Africa/Freetown', N'Africa/Gaborone', N'Africa/Harare', N'Africa/Johannesburg', N'Africa/Juba', N'Africa/Kampala', N'Africa/Khartoum', N'Africa/Kigali', N'Africa/Kinshasa', N'Africa/Lagos', N'Africa/Libreville', N'Africa/Lome', N'Africa/Luanda', N'Africa/Lubumbashi', N'Africa/Lusaka', N'Africa/Malabo', N'Africa/Maputo', N'Africa/Maseru', N'Africa/Mbabane', N'Africa/Mogadishu', N'Africa/Monrovia', N'Africa/Nairobi', N'Africa/Ndjamena', N'Africa/Niamey', N'Africa/Nouakchott', N'Africa/Ouagadougou', N'Africa/Porto-Novo', N'Africa/Sao_Tome', N'Africa/Timbuktu', N'Africa/Tripoli', N'Africa/Tunis', N'Africa/Windhoek', N'America/Adak', N'America/Anchorage', N'America/Anguilla', N'America/Antigua', N'America/Araguaina', N'America/Argentina/Buenos_Aires', N'America/Argentina/Catamarca', N'America/Argentina/ComodRivadavia', N'America/Argentina/Cordoba', N'America/Argentina/Jujuy', N'America/Argentina/La_Rioja', N'America/Argentina/Mendoza', N'America/Argentina/Rio_Gallegos', N'America/Argentina/Salta', N'America/Argentina/San_Juan', N'America/Argentina/San_Luis', N'America/Argentina/Tucuman', N'America/Argentina/Ushuaia', N'America/Aruba', N'America/Asuncion', N'America/Atikokan', N'America/Atka', N'America/Bahia', N'America/Bahia_Banderas', N'America/Barbados', N'America/Belem', N'America/Belize', N'America/Blanc-Sablon', N'America/Boa_Vista', N'America/Bogota', N'America/Boise', N'America/Buenos_Aires', N'America/Cambridge_Bay', N'America/Campo_Grande', N'America/Cancun', N'America/Caracas', N'America/Catamarca', N'America/Cayenne', N'America/Cayman', N'America/Chicago', N'America/Chihuahua', N'America/Coral_Harbour', N'America/Cordoba', N'America/Costa_Rica', N'America/Creston', N'America/Cuiaba', N'America/Curacao', N'America/Danmarkshavn', N'America/Dawson', N'America/Dawson_Creek', N'America/Denver', N'America/Detroit', N'America/Dominica', N'America/Edmonton', N'America/Eirunepe', N'America/El_Salvador', N'America/Ensenada', N'America/Fort_Nelson', N'America/Fort_Wayne', N'America/Fortaleza', N'America/Glace_Bay', N'America/Godthab', N'America/Goose_Bay', N'America/Grand_Turk', N'America/Grenada', N'America/Guadeloupe', N'America/Guatemala', N'America/Guayaquil', N'America/Guyana', N'America/Halifax', N'America/Havana', N'America/Hermosillo', N'America/Indiana/Indianapolis', N'America/Indiana/Knox', N'America/Indiana/Marengo', N'America/Indiana/Petersburg', N'America/Indiana/Tell_City', N'America/Indiana/Vevay', N'America/Indiana/Vincennes', N'America/Indiana/Winamac', N'America/Indianapolis', N'America/Inuvik', N'America/Iqaluit', N'America/Jamaica', N'America/Jujuy', N'America/Juneau', N'America/Kentucky/Louisville', N'America/Kentucky/Monticello', N'America/Knox_IN', N'America/Kralendijk', N'America/La_Paz', N'America/Lima', N'America/Los_Angeles', N'America/Louisville', N'America/Lower_Princes', N'America/Maceio', N'America/Managua', N'America/Manaus', N'America/Marigot', N'America/Martinique', N'America/Matamoros', N'America/Mazatlan', N'America/Mendoza', N'America/Menominee', N'America/Merida', N'America/Metlakatla', N'America/Mexico_City', N'America/Miquelon', N'America/Moncton', N'America/Monterrey', N'America/Montevideo', N'America/Montreal', N'America/Montserrat', N'America/Nassau', N'America/New_York', N'America/Nipigon', N'America/Nome', N'America/Noronha', N'America/North_Dakota/Beulah', N'America/North_Dakota/Center', N'America/North_Dakota/New_Salem', N'America/Ojinaga', N'America/Panama', N'America/Pangnirtung', N'America/Paramaribo', N'America/Phoenix', N'America/Port-au-Prince', N'America/Port_of_Spain', N'America/Porto_Acre', N'America/Porto_Velho', N'America/Puerto_Rico', N'America/Punta_Arenas', N'America/Rainy_River', N'America/Rankin_Inlet', N'America/Recife', N'America/Regina', N'America/Resolute', N'America/Rio_Branco', N'America/Rosario', N'America/Santa_Isabel', N'America/Santarem', N'America/Santiago', N'America/Santo_Domingo', N'America/Sao_Paulo', N'America/Scoresbysund', N'America/Shiprock', N'America/Sitka', N'America/St_Barthelemy', N'America/St_Johns', N'America/St_Kitts', N'America/St_Lucia', N'America/St_Thomas', N'America/St_Vincent', N'America/Swift_Current', N'America/Tegucigalpa', N'America/Thule', N'America/Thunder_Bay', N'America/Tijuana', N'America/Toronto', N'America/Tortola', N'America/Vancouver', N'America/Virgin', N'America/Whitehorse', N'America/Winnipeg', N'America/Yakutat', N'America/Yellowknife', N'Antarctica/Casey', N'Antarctica/Davis', N'Antarctica/DumontDUrville', N'Antarctica/Macquarie', N'Antarctica/Mawson', N'Antarctica/McMurdo', N'Antarctica/Palmer', N'Antarctica/Rothera', N'Antarctica/South_Pole', N'Antarctica/Syowa', N'Antarctica/Troll', N'Antarctica/Vostok', N'Arctic/Longyearbyen', N'Asia/Aden', N'Asia/Almaty', N'Asia/Amman', N'Asia/Anadyr', N'Asia/Aqtau', N'Asia/Aqtobe', N'Asia/Ashgabat', N'Asia/Ashkhabad', N'Asia/Atyrau', N'Asia/Baghdad', N'Asia/Bahrain', N'Asia/Baku', N'Asia/Bangkok', N'Asia/Barnaul', N'Asia/Beirut', N'Asia/Bishkek', N'Asia/Brunei', N'Asia/Calcutta', N'Asia/Chita', N'Asia/Choibalsan', N'Asia/Chongqing', N'Asia/Chungking', N'Asia/Colombo', N'Asia/Dacca', N'Asia/Damascus', N'Asia/Dhaka', N'Asia/Dili', N'Asia/Dubai', N'Asia/Dushanbe', N'Asia/Famagusta', N'Asia/Gaza', N'Asia/Harbin', N'Asia/Hebron', N'Asia/Ho_Chi_Minh', N'Asia/Hong_Kong', N'Asia/Hovd', N'Asia/Irkutsk', N'Asia/Istanbul', N'Asia/Jakarta', N'Asia/Jayapura', N'Asia/Jerusalem', N'Asia/Kabul', N'Asia/Kamchatka', N'Asia/Karachi', N'Asia/Kashgar', N'Asia/Kathmandu', N'Asia/Katmandu', N'Asia/Khandyga', N'Asia/Kolkata', N'Asia/Krasnoyarsk', N'Asia/Kuala_Lumpur', N'Asia/Kuching', N'Asia/Kuwait', N'Asia/Macao', N'Asia/Macau', N'Asia/Magadan', N'Asia/Makassar', N'Asia/Manila', N'Asia/Muscat', N'Asia/Nicosia', N'Asia/Novokuznetsk', N'Asia/Novosibirsk', N'Asia/Omsk', N'Asia/Oral', N'Asia/Phnom_Penh', N'Asia/Pontianak', N'Asia/Pyongyang', N'Asia/Qatar', N'Asia/Qyzylorda', N'Asia/Rangoon', N'Asia/Riyadh', N'Asia/Saigon', N'Asia/Sakhalin', N'Asia/Samarkand', N'Asia/Seoul', N'Asia/Shanghai', N'Asia/Singapore', N'Asia/Srednekolymsk', N'Asia/Taipei', N'Asia/Tashkent', N'Asia/Tbilisi', N'Asia/Tehran', N'Asia/Tel_Aviv', N'Asia/Thimbu', N'Asia/Thimphu', N'Asia/Tokyo', N'Asia/Tomsk', N'Asia/Ujung_Pandang', N'Asia/Ulaanbaatar', N'Asia/Ulan_Bator', N'Asia/Urumqi', N'Asia/Ust-Nera', N'Asia/Vientiane', N'Asia/Vladivostok', N'Asia/Yakutsk', N'Asia/Yangon', N'Asia/Yekaterinburg', N'Asia/Yerevan', N'Atlantic/Azores', N'Atlantic/Bermuda', N'Atlantic/Canary', N'Atlantic/Cape_Verde', N'Atlantic/Faeroe', N'Atlantic/Faroe', N'Atlantic/Jan_Mayen', N'Atlantic/Madeira', N'Atlantic/Reykjavik', N'Atlantic/South_Georgia', N'Atlantic/St_Helena', N'Atlantic/Stanley', N'Australia/ACT', N'Australia/Adelaide', N'Australia/Brisbane', N'Australia/Broken_Hill', N'Australia/Canberra', N'Australia/Currie', N'Australia/Darwin', N'Australia/Eucla', N'Australia/Hobart', N'Australia/LHI', N'Australia/Lindeman', N'Australia/Lord_Howe', N'Australia/Melbourne', N'Australia/NSW', N'Australia/North', N'Australia/Perth', N'Australia/Queensland', N'Australia/South', N'Australia/Sydney', N'Australia/Tasmania', N'Australia/Victoria', N'Australia/West', N'Australia/Yancowinna', N'Brazil/Acre', N'Brazil/DeNoronha', N'Brazil/East', N'Brazil/West', N'CET', N'CST6CDT', N'Canada/Atlantic', N'Canada/Central', N'Canada/Eastern', N'Canada/Mountain', N'Canada/Newfoundland', N'Canada/Pacific', N'Canada/Saskatchewan', N'Canada/Yukon', N'Chile/Continental', N'Chile/EasterIsland', N'Cuba', N'EET', N'EST', N'EST5EDT', N'Egypt', N'Eire', N'Etc/GMT', N'Etc/GMT+0', N'Etc/GMT+1', N'Etc/GMT+10', N'Etc/GMT+11', N'Etc/GMT+12', N'Etc/GMT+2', N'Etc/GMT+3', N'Etc/GMT+4', N'Etc/GMT+5', N'Etc/GMT+6', N'Etc/GMT+7', N'Etc/GMT+8', N'Etc/GMT+9', N'Etc/GMT-0', N'Etc/GMT-1', N'Etc/GMT-10', N'Etc/GMT-11', N'Etc/GMT-12', N'Etc/GMT-13', N'Etc/GMT-14', N'Etc/GMT-2', N'Etc/GMT-3', N'Etc/GMT-4', N'Etc/GMT-5', N'Etc/GMT-6', N'Etc/GMT-7', N'Etc/GMT-8', N'Etc/GMT-9', N'Etc/GMT0', N'Etc/Greenwich', N'Etc/UCT', N'Etc/UTC', N'Etc/Universal', N'Etc/Zulu', N'Europe/Amsterdam', N'Europe/Andorra', N'Europe/Astrakhan', N'Europe/Athens', N'Europe/Belfast', N'Europe/Belgrade', N'Europe/Berlin', N'Europe/Bratislava', N'Europe/Brussels', N'Europe/Bucharest', N'Europe/Budapest', N'Europe/Busingen', N'Europe/Chisinau', N'Europe/Copenhagen', N'Europe/Dublin', N'Europe/Gibraltar', N'Europe/Guernsey', N'Europe/Helsinki', N'Europe/Isle_of_Man', N'Europe/Istanbul', N'Europe/Jersey', N'Europe/Kaliningrad', N'Europe/Kiev', N'Europe/Kirov', N'Europe/Lisbon', N'Europe/Ljubljana', N'Europe/London', N'Europe/Luxembourg', N'Europe/Madrid', N'Europe/Malta', N'Europe/Mariehamn', N'Europe/Minsk', N'Europe/Monaco', N'Europe/Moscow', N'Europe/Nicosia', N'Europe/Oslo', N'Europe/Paris', N'Europe/Podgorica', N'Europe/Prague', N'Europe/Riga', N'Europe/Rome', N'Europe/Samara', N'Europe/San_Marino', N'Europe/Sarajevo', N'Europe/Saratov', N'Europe/Simferopol', N'Europe/Skopje', N'Europe/Sofia', N'Europe/Stockholm', N'Europe/Tallinn', N'Europe/Tirane', N'Europe/Tiraspol', N'Europe/Ulyanovsk', N'Europe/Uzhgorod', N'Europe/Vaduz', N'Europe/Vatican', N'Europe/Vienna', N'Europe/Vilnius', N'Europe/Volgograd', N'Europe/Warsaw', N'Europe/Zagreb', N'Europe/Zaporozhye', N'Europe/Zurich', N'GB', N'GB-Eire', N'GMT', N'GMT+0', N'GMT-0', N'GMT0', N'Greenwich', N'HST', N'Hongkong', N'Iceland', N'Indian/Antananarivo', N'Indian/Chagos', N'Indian/Christmas', N'Indian/Cocos', N'Indian/Comoro', N'Indian/Kerguelen', N'Indian/Mahe', N'Indian/Maldives', N'Indian/Mauritius', N'Indian/Mayotte', N'Indian/Reunion', N'Iran', N'Israel', N'Jamaica', N'Japan', N'Kwajalein', N'Libya', N'MET', N'MST', N'MST7MDT', N'Mexico/BajaNorte', N'Mexico/BajaSur', N'Mexico/General', N'NZ', N'NZ-CHAT', N'Navajo', N'PRC', N'PST8PDT', N'Pacific/Apia', N'Pacific/Auckland', N'Pacific/Bougainville', N'Pacific/Chatham', N'Pacific/Chuuk', N'Pacific/Easter', N'Pacific/Efate', N'Pacific/Enderbury', N'Pacific/Fakaofo', N'Pacific/Fiji', N'Pacific/Funafuti', N'Pacific/Galapagos', N'Pacific/Gambier', N'Pacific/Guadalcanal', N'Pacific/Guam', N'Pacific/Honolulu', N'Pacific/Johnston', N'Pacific/Kiritimati', N'Pacific/Kosrae', N'Pacific/Kwajalein', N'Pacific/Majuro', N'Pacific/Marquesas', N'Pacific/Midway', N'Pacific/Nauru', N'Pacific/Niue', N'Pacific/Norfolk', N'Pacific/Noumea', N'Pacific/Pago_Pago', N'Pacific/Palau', N'Pacific/Pitcairn', N'Pacific/Pohnpei', N'Pacific/Ponape', N'Pacific/Port_Moresby', N'Pacific/Rarotonga', N'Pacific/Saipan', N'Pacific/Samoa', N'Pacific/Tahiti', N'Pacific/Tarawa', N'Pacific/Tongatapu', N'Pacific/Truk', N'Pacific/Wake', N'Pacific/Wallis', N'Pacific/Yap', N'Poland', N'Portugal', N'ROC', N'ROK', N'Singapore', N'Turkey', N'UCT', N'US/Alaska', N'US/Aleutian', N'US/Arizona', N'US/Central', N'US/East-Indiana', N'US/Eastern', N'US/Hawaii', N'US/Indiana-Starke', N'US/Michigan', N'US/Mountain', N'US/Pacific', N'US/Pacific-New', N'US/Samoa', N'UTC', N'Universal', N'W-SU', N'WET', N'Zulu')), [clipTemplateId] NVARCHAR(255) NULL, [shopId] NVARCHAR(255) NULL, [createdAt] DATETIMEOFFSET NOT NULL, [updatedAt] DATETIMEOFFSET NOT NULL, PRIMARY KEY ([id]), FOREIGN KEY ([clipTemplateId]) REFERENCES [ClipTemplate] ([id]) ON DELETE NO ACTION, FOREIGN KEY ([shopId]) REFERENCES [Shop] ([id]) ON DELETE NO ACTION);
Same format as the other queries, except it's got a list of timezones. I can see the previous queries in the audit log, but not this one.
I've run SELECT OBJECT_ID('[Client]', 'U') and it returns 1218103380, so the query shouldn't actually be doing anything, given the IF at the top?
Reproducable every time against multiple Azure SQL databases in different development environments with the node app. Other SQL clients send the query with success. App last worked yesterday. No app or infra changes since.
When putting the query into some online SQL validators like https://www.eversql.com/sql-syntax-check-validator/, it says the query is invalid, but MS SQL Server Management Studio thinks it's fine, and I can run it without issue.
Sequelize Config:
database: {
database: process.env.DATABASE_NAME,
dialect: "mssql",
host: process.env.DATABASE_HOST,
loggingEnabled: false,
password: process.env.DATABASE_PASSWORD,
pool: {
acquire: 30000,
idle: 30000,
max: 10,
min: 0,
},
port: parseInt(process.env.DATABASE_PORT, 10) || 1433,
transactionTimeout: parseInt(process.env.DB_TRANSACTION_TIMEOUT, 10) || 30000,
username: process.env.DATABASE_USERNAME,
}

It seems 10.17.0 broke my DB connection for long queries. Rolling back to Node v10.16.3 has resolved the issue.
On start up, my Node program was pulling the latest 10.x version. It started using Node v10.17.0, released 21st October 2019, which was when the issue started.

Here is a solution for timeout error while running long queries.
connection timeout or already too many clients

Related

"No migrations pending" when attempting to run migrations , used to work with no problem

I have a web app, and I've written a migrator to create all my tables and relations, recently no matter what I try, typeorm does not appear to find this migrator and hence, does not run it.
My file structure (just the migrations)
src> Databas> Migrations>1663525805095-add_users.ts,1663529676790-make_institute_nullable.ts
ormconfig.ts
import { DataSource } from 'typeorm';
import { ConfigService } from '#nestjs/config';
import { config } from 'dotenv';
config();
const configService = new ConfigService();
const source = new DataSource({
type: 'postgres',
host: configService.get('POSTGRES_HOST'),
port: configService.get('POSTGRES_PORT'),
username: configService.get('POSTGRES_USER'),
password: configService.get('POSTGRES_PASSWORD'),
database: configService.get('POSTGRES_DB'),
synchronize: false,
logging: false,
migrations: ['src/database/migrations/*.ts'],
migrationsTableName: 'migrations',
entities: ['src/**/*.entity.ts'],
});
export default source;
In order to run this, I type yarn start:dev in order to get my Server started.
Then I run yarn migrations:run which I get:
query: SELECT * FROM current_schema()
query: SELECT version();
query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = 'public' AND "table_name" = 'migrations'
query: CREATE TABLE "migrations" ("id" SERIAL NOT NULL, "timestamp" bigint NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY ("id"))
query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC
No migrations are pending
When I look at my db, I see a migrations table with no entries.
I have tried to delete my migrator file and create it again with a more recent timestamp and that does not work either.
scripts from my package.json
"migrations:run": "yarn typeorm migration:run"
"typeorm": "typeorm-ts-node-commonjs -d ./ormconfig.ts"
"start:dev": "nest start --watch"
Other info
I'm using docker for the postgres DB and pgAdmin, it connects with no problem.
Any help would be greatly appreciated.

Error when using docker-compose to generate a full-stack application

The API does not find the DB when using docker compose.
I already configured the DATABA_URL and it doesn't work.
.env file:
JWT_SECRET="palavrasecreta"
NODE_ENV=production
DATABASE_URL=postgres://postgres:1234#db:5432/postgres
docker-compose file:
version: "3"
services:
app-front-end:
build: charllenger-front/.
container_name: front-end-ui
expose:
- 3000
ports:
- 3000:3000
links:
- api
api:
container_name: charllenger-back-end-Api
build: Full-Stack-charlenger-API/.
volumes:
- ./src:/app/src
expose:
- 3001
ports:
- 3001:3001
depends_on:
- db
command: bash -c 'yarn migration:run && yarn dev'
links:
- db
db:
container_name: charllenger-Api-postgres
image: "postgres"
env_file:
- Full-Stack-charlenger-API/.env
expose:
- 5432
ports:
- 5432:5432
DATA SOURCE
import { DataSource } from "typeorm";
require("dotenv").config();
export const AppDataSource = new DataSource({
type: "postgres",
host: "database",
url: process.env.DATABASE_URL,
ssl:
process.env.NODE_ENV === "production"
? { rejectUnauthorized: false }
: false,
synchronize: false,
logging: true,
entities:
process.env.NODE_ENV === "production"
? ["src/entities/*.js"]
: ["src/entities/*.ts"],
migrations:
process.env.NODE_ENV === "production"
? ["src/migrations/*.js"]
: ["src/migrations/*.ts"],
});
AppDataSource.initialize()
.then(() => {
console.log("Data Source Initialized");
})
.catch((err) => {
console.error("Error during data source Initialization", err);
}
);
Migration
import { MigrationInterface, QueryRunner } from "typeorm";
export class initialMigration1673714934213 implements MigrationInterface {
name = 'initialMigration1673714934213'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "transaction" ("transaction_id" uuid NOT NULL, "value" numeric NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "debitedAccountAccountId" uuid, "creditedAccountAccountId" uuid, CONSTRAINT "PK_6e02e5a0a6a7400e1c944d1e946" PRIMARY KEY ("transaction_id"))`);
await queryRunner.query(`CREATE TABLE "user" ("user_id" uuid NOT NULL, "username" character varying NOT NULL, "password" character varying NOT NULL, "account" uuid, CONSTRAINT "REL_4ab2df0a57a74fdf904e0e2708" UNIQUE ("account"), CONSTRAINT "PK_758b8ce7c18b9d347461b30228d" PRIMARY KEY ("user_id"))`);
await queryRunner.query(`CREATE TABLE "account" ("account_id" uuid NOT NULL, "balance" double precision NOT NULL, CONSTRAINT "PK_ea08b54a9d7322975ffc57fc612" PRIMARY KEY ("account_id"))`);
await queryRunner.query(`ALTER TABLE "transaction" ADD CONSTRAINT "FK_bbbfcdb3330cc4e5846f2d23200" FOREIGN KEY ("debitedAccountAccountId") REFERENCES "account"("account_id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "transaction" ADD CONSTRAINT "FK_4ece0117a7c2689832bab37209b" FOREIGN KEY ("creditedAccountAccountId") REFERENCES "account"("account_id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "user" ADD CONSTRAINT "FK_4ab2df0a57a74fdf904e0e27086" FOREIGN KEY ("account") REFERENCES "account"("account_id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "user" DROP CONSTRAINT "FK_4ab2df0a57a74fdf904e0e27086"`);
await queryRunner.query(`ALTER TABLE "transaction" DROP CONSTRAINT "FK_4ece0117a7c2689832bab37209b"`);
await queryRunner.query(`ALTER TABLE "transaction" DROP CONSTRAINT "FK_bbbfcdb3330cc4e5846f2d23200"`);
await queryRunner.query(`DROP TABLE "account"`);
await queryRunner.query(`DROP TABLE "user"`);
await queryRunner.query(`DROP TABLE "transaction"`);
}
}
Error:
enter image description here
t's working on another computer, I've already changed localhost to db, I've tried other ports and it doesn't work.
In the docker-compose you define your database container as db.
db:
container_name: charllenger-Api-postgres
image: "postgres"
db: is the servicename.
If you want to connect from your api to the database container, inside your docker network, you need to use this servicename as db_host.
the section:
ports:
- 5432:5432
means: map the port 5432 inside the container to my host (your Pc where you installed docker).
So you can access the container on his port 5432, from your PC with localhost:5432 and this works fine if you execute your code localy even if postgres is running inside a container.
If your code runs in a container localhost refers to that container.
Lets say each container has it own localhost. So you can access the other containers only by its servicename. There are a few other possibilities, but lets remain to use the servicename.
In side your Typeorm configuration ensure that the connection to the database use the servicename when you run the code inside the conatiner, or localhost if you run it directly on your pc without docker.
When you deploy it to Production, again you must look where the db_host is. That depends on your deployment. If you use an external DB from a provvider it should be a full qualified domain name. If you deploy your db by yourself it depends if you use docker Kubernetes or what else.

I am unable to connect Mongodb atlas Cluster from node js getting following unable to connect DB error

{ error: 1, message: 'Command failed: mongodump -h cluster0.yckk6.mongodb.net --port=27017 -d databaseName -p -u --gzip --archive=/tmp/file_name_2022-09-19T09-42-05.gz\n' + '2022-09-19T14:42:08.931+0000\tFailed: error connecting to db server: no reachable servers\n' }
Can anyone help me to solve this problem and following is my backup code
function databaseBackup() {
let backupConfig = {
mongodb: "mongodb+srv://<username>:<password>#cluster0.yckk6.mongodb.net:27017/databaseName?
retryWrites=true&w=majority&authMechanism=SCRAM-SHA-1", // MongoDB Connection URI
s3: {
accessKey: "SDETGGAKIA2GL", //AccessKey
secretKey: "Asad23rdfdg2teE8lOS3JWgdfgfdgfg", //SecretKey
region: "ap-south-1", //S3 Bucket Region
accessPerm: "private", //S3 Bucket Privacy, Since, You'll be storing Database, Private is HIGHLY Recommended
bucketName: "backupDatabase" //Bucket Name
},
keepLocalBackups: false, //If true, It'll create a folder in project root with database's name and store backups in it and if it's false, It'll use temporary directory of OS
noOfLocalBackups: 5, //This will only keep the most recent 5 backups and delete all older backups from local backup directory
timezoneOffset: 300 //Timezone, It is assumed to be in hours if less than 16 and in minutes otherwise
}
MBackup(backupConfig).then(onResolve => {
// When everything was successful
console.log(onResolve);
}).catch(onReject => {
// When Anything goes wrong!
console.log(onReject);
});
}

Login problems connecting with SQL Server in nodejs

I'm working in osx with SQL Server using a docker image to be able to use it, running:
docker run -d --name sqlserver -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=myStrongPass' -e 'MSSQL_PID=Developer' -p 1433:1433 microsoft/mssql-server-linux:2017-latest
I can connect successfully in Azure Data Studio GUI with the following configuration
But the connection does not works in my nodejs code using mssql module.
const poolConnection = new sql.ConnectionPool({
database: 'myDbTest',
server: 'localhost',
port: 1433,
password: '*******',
user: 'sa',
connectionTimeout: 5000,
options: {
encrypt: false,
},
});
const [error, connection] = await to(poolConnection.connect());
The error always is the same:
ConnectionError: Login failed for user 'sa'
Is my first time working with SQL Server and is confusing for me the fact that I can connect correctly in the Azure Studio GUI but I can't do it in code.
I'm trying create new login users with CREATE LOGIN and give them privileges based on other post here in stackoverflow but nothing seems to work.
UPDATE:
I realize that i can connect correctly if i put master in database key.
Example:
const poolConnection = new sql.ConnectionPool({
database: 'master', <- Update here
server: 'localhost',
port: 1433,
password: '*******',
user: 'sa',
connectionTimeout: 5000,
options: {
encrypt: false,
},
});
1) Db that i can connect
2) Db that i want to connect but i can't.
Container error
2020-03-18 03:59:14.11 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'DoctorHoyCRM'. [CLIENT: 172.17.0.1]
I suspect a lot of people miss the sa password complexity requirement:
The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.
An example based on: Quickstart: Run SQL Server container images with Docker
docker pull mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=myStr0ngP4ssw0rd" -e "MSSQL_PID=Developer" -p 1433:1433 --name sqlserver -d mcr.microsoft.com/mssql/server:2017-latest
docker start sqlserver
Checking that the docker image is running (it should not say "Exited" under STATUS)...
docker ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# af9f01eacab2 mcr.microsoft.com/mssql/server:2017-latest "/opt/mssql/bin/nonr…" 45 seconds ago Up 34 seconds 0.0.0.0:1433->1433/tcp sqlserver
Testing from within the docker container that SQL Server is installed and running...
docker exec -it sqlserver /opt/mssql-tools/bin/sqlcmd \
-S localhost -U "sa" -P "myStr0ngP4ssw0rd" \
-Q "select ##VERSION"
# --------------------------------------------------------------------
# Microsoft SQL Server 2017 (RTM-CU19) (KB4535007) - 14.0.3281.6 (X64)
# Jan 23 2020 21:00:04
# Copyright (C) 2017 Microsoft Corporation
# Developer Edition (64-bit) on Linux (Ubuntu 16.04.6 LTS)
Finally, testing from NodeJS...
const sql = require('mssql');
const config = {
user: 'sa',
password: 'myStr0ngP4ssw0rd',
server: 'localhost',
database: 'msdb',
};
sql.on('error', err => {
console.error('err: ', err);
});
sql.connect(config).then(pool => {
return pool.request()
.query('select ##VERSION')
}).then(result => {
console.dir(result)
}).catch(err => {
console.error('err: ', err);
});
$ node test.js
tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules/mssql/lib/tedious/connection-pool.js:61:23
{
recordsets: [ [ [Object] ] ],
recordset: [
{
'': 'Microsoft SQL Server 2017 (RTM-CU19) (KB4535007) - 14.0.3281.6 (X64) \n' +
'\tJan 23 2020 21:00:04 \n' +
'\tCopyright (C) 2017 Microsoft Corporation\n' +
'\tDeveloper Edition (64-bit) on Linux (Ubuntu 16.04.6 LTS)'
}
],
output: {},
rowsAffected: [ 1 ]
}
Hope this helps.

Mongodb - replica set - max connections

I have a replicaset of 3 mongo node, 1 primary, 1 secondary and 1 arbiter.
Connected on this replicaset, i have 20 node process, on 20 different serveur using their own connections to the replicaset. All those process use mongoose.
My primary replicaset show the following :
rsProd:PRIMARY> db.serverStatus().connections
{ "current" : 284, "available" : 50916, "totalCreated" : NumberLong(42655) }
From time to time, when i restart some nodejs node i have the following errors :
mongodb no valid seed servers in list
My connection string to the replicaset is the following :
"mongodb://mongo2aws.abcdef:27017/dbname,mongo1.abcdef:27017/dbname"
And my db options are the following :
config.db_options = {
user: "MYUSER",
pass: "MYPASSWORD",
replset: {
rs_name: "RSNAME",
ssl: true,
sslValidate:false,
sslCA: ca,
ca: ca,
sslKey: key,
sslCert: key
},
socketOptions : {
keepAlive : 1,
connectTimeoutMS : 1000
},
server: {
ssl: true,
sslValidate:false,
sslCA: ca,
ca: ca,
sslKey: key,
sslCert: key
},
auth: {
authdb: 'MYAUTHDB'
}
};
I haven't this error when i was running only 16 node process.
According to this i suppose that i have reach a limit of max concurrent connections or something like this.
But, if i restart again crashing node, it finally seems to work.
Why mongo / mongoose raise this error ?
What can i do to prevent this / increase limit ?
Thanks in advance
Best regards.
Solved by increasing ulimit open files
Default ulimit for open files in AWS EC2 ubuntu server is set to 1000 by default.
In addition, adding reconnect options prevent this problem :
config.db_options.reconnectTries=10;
config.db_options.reconnectInterval=500;
config.db_options.poolSize=20;
config.db_options.connectTimeoutMS=5000;

Resources