Cypress mysql connector throws an error Error: Webpack Compilation Error - mysql

I am new to cypress. My cypress test suite are runing good before configure a mysql connector to connect to mysql database. It throw below Webpack Compilation Error error when add mysql connector code in plugins >> Index.js. following BDD apporche so feature file is used.
Error encountered:
Error: Webpack Compilation Error
./cypress/integration/Login.feature 1:15
Module parse failed: Unexpected token (1:15)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> Feature: Login test
Code in plugins >> index.js
const mysql = require('mysql2')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(Cypress.env('passwrd'))
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
};
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
};
Calling function in step definition
cy.task(
"queryDb",
`select * from city LIMIT 5`
).then(count => {
expect(count).to.have.lengthOf(1);
});
Cypress.json
"env1": {
"db":
{"host": "xxxx.test.mysql",
"user": "xxxxxxx",
"passwrd": "xxxxxxx",
"database": "main" ,
"connectionLimit":10
}
}
tsconfig.js
"compilerOptions": {
"target": "es5",
"outDir": "./built",
"lib": ["es5", "dom"],
"allowSyntheticDefaultImports": false,
"allowJs": true,
"types": ["cypress", "node", "cypress-xpath"]
}
tried lots of ways likes
by refresh installation of node_modules
reinstall mysql2 and mysql package
reinstalling #cypress/webpack-preprocessor
but nothing works.

Related

MYSQL returns error when a query is made through an endpoint

I'm developing a simple GET endpoint using NodeJS, express and MySql, but whenever i use the mysql.query('select * from table'), through an service, the server is shutdown. The same query is successful when i declare it in the database.js file, but not when integrated with the rest of my system.
My database.js is as follows:
const mysql = require("mysql");
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "rootpwd",
port: 3306,
database: "blog",
});
con.query("select * from post", (er, row) => {
if (er) throw er;
console.log(row);
return row;
});
This con.query function is only declared for test purpose, and deleted when endpoint is called. When i run my server and this query is declared, it logs in my console all the content in this table.
When i run node database.js all my entries in posts table are shown in console.
But when i call, in another file,
database.query('select * from post', (er, rows) => { if (er) throw er return rows })
The localhost is shutdown with the following message:
-> starting at object with constructor 'Query'
| property '_timer' -> object with constructor 'Timer'
--- property '_object' closes the circle
at JSON.stringify (<anonymous>)
at stringify (/home/guilherme/Documentos/Projects/rest_api/node_modules/express/lib/response.js:1150:12)
at ServerResponse.json (/home/guilherme/Documentos/Projects/rest_api/node_modules/express/lib/response.js:271:14)
at /home/guilherme/Documentos/Projects/rest_api/server/route/postsRoute.js:7:19
at processTicksAndRejections (internal/process/task_queues.js:95:5)
I've installed body-parser in my project and i'm using express.json() as a middleware in my app:
const express = require("express");
const app = express();
app.use(express.json());
app.use("/", require("./route/postsRoute"));
app.listen(process.env.PORT || 3000, () =>
console.log(`Server running on port ${process.env.PORT || 3000}`)
);
My route file:
const express = require("express");
const router = express.Router();
const postService = require("../service/postService");
router.get("/posts", async (req, res) => {
const posts = await postService.getPosts();
res.status(200).json(posts);
res.end();
});
My Service file:
exports.getPosts = async () => {
const test = await postsData.getPosts();
console.log(test, "this is what is returned");
return test;
};
And finally, my data file:
exports.getPosts = () =>
database.query("select * from post;", (er, rows) => {
if (er) throw er;
return JSON.stringify(rows);
});
this JSON.strinfy was inserted for test purpose, and the error is returned the same way.
Node version: v14.18.0
dependecies:
"dependencies": {
"axios": "^1.2.1",
"body-parser": "^1.20.1",
"express": "^4.18.2",
"jest": "^29.3.1",
"mysql": "^2.18.1",
}
If anyone needs more information to help me debug this, please let me know.
I've tried parsing the content that is returned in my query, but it didn't returned anything useful. I've added async and awaits, but it didn't helped either.
I'm expecting to see all my tables content when i access the /posts route.
You're converting the result to JSON twice, in the data file and the services file. You should only do the JSON conversion in one place, not both.
I recommend doing it only in the service, so in the data code use
exports.getPosts = () =>
database.query("select * from post;", (er, rows) => {
if (er) throw er;
return rows;
});
So the main issue was that 'mysql' lib does not handle promises correctly.
I've added mysql2 and and changed my Data.js file to
database.promise().query("select * from post;");
And it works just fine now.

Pool does Not exists typeORM

I'm using nestJS with typeorm(0.3.10,"#nestjs/typeorm": "^9.0.1") with MySQL. its working fine when i start the server but after one day(based on traffic) getting pool doesn't exist error. I have upgraded Typeform 0.2x to 0.3.10. based on new config I have set poolSize:100 but still getting Error: Pool does Not exists.
when I checked DB connection used in AWS, it reached almost 100 (but traffic is not much that which consumes 100 connections).
Here is my cofigProdiver
export const MysqlProvider = [
{
provide: MYSQL_PROVIDER,
useFactory: (tunnelService: TunnelService): Promise<any> => {
return typeOrm
.createConnection({
type: "mysql",
poolSize: parseInt(process.env.DB_MAX_POOL),
entities: [...],
replication: {
master: {
host: process.env.MASTER_HOST,
port: parseInt(process.env.MASTER_PORT),
username: process.env.MASTER_USERNAME,
password: process.env.MASTER_PASSWORD,
database: process.env.MASTER_DATABASE,
},
slaves: [
{
host: process.env.SLAVE_HOST,
port: parseInt(process.env.SLAVE_PORT),
username: process.env.SLAVE_USERNAME,
password: process.env.SLAVE_PASSWORD,
database: process.env.SLAVE_DATABASE,
},
],
},
synchronize: false,
} as typeOrm.ConnectionOptions)
.then((conn) => {
Logger.log("Connected to SQL Database");
return conn;
})
.catch((err) => {
console.log(err);
Logger.error(`Couldnt connect to Mysql ${err}`, "AppModule");
});
},
inject: [TunnelService],
},
];
Here you can see I'm using Replications, in project I have used transaction and masterRead multiple times. In each scenario, I'm releasing the queryRunner.
Here the example:
async update_branch_pickup_metadata(data, language) {
const queryRunner = this.connection.createQueryRunner("master");
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const updatedDelivery = await queryRunner.manager
.createQueryBuilder()
.update(Deliveries)
.set({ ... })
.where(...)
.execute();
await queryRunner.commitTransaction();
return data.id
} catch (err) {
console.log(err, "transaction error");
await queryRunner.rollbackTransaction();
throw new HttpException(
businessErros(language, CODE_TECH_ISSUES),
422
);
} finally {
await queryRunner.release();
}
}
I have set poolSize:100 and released queryRunner on each transaction but still getting Error: Pool does Not exists typeORM.
Here the error sample:
Error: Pool does Not exists.
at PoolNamespace.getConnection (/usr/src/app/node_modules/mysql2/lib/pool_cluster.js:37:17)
at PoolCluster.getConnection (/usr/src/app/node_modules/mysql2/lib/pool_cluster.js:177:15)
at /usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:724:30
at new Promise (<anonymous>)
at MysqlDriver.obtainSlaveConnection (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:723:16)
at MysqlQueryRunner.connect (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:50:18)
at /usr/src/app/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:145:55
at new Promise (<anonymous>)
at MysqlQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:143:16)
at SelectQueryBuilder.loadRawResults (/usr/src/app/node_modules/typeorm/query-builder/SelectQueryBuilder.js:2056:43) getOrders error
can anyone suggest me where I'm doing wrong because its creating lot of issue on production, I have to restart the service every day which cost lot of failure queries.

Cypress fixtures - Cannot read properties of undefined (reading 'runTests')

I am trying to connect to mysql in cypress test cases. But getting an error on when trying to import file mysql-db-connect.js in steps definition file.
mysql-db-connect.js
function queryTestDb(query, pool) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = createPool.createConnection(pool)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
module.exports={queryTestDb}
queryTestDb("select * from test LIMIT 5", pool).then(res => console.log(res));
Above queryTestDb() code works perfectly when execute from same file.
But throws an error when trying to import and run steps definition file
Step definition format
///<reference types="cypress" />
import { Before,After,Given, When, Then, And } from "cypress-cucumber-preprocessor/steps";
import{closeTabs} from '../../../support/utility/tearDown.js';
import {queryTestDb} from '../../../support/utility/mysql-db-connect.js'; ---> ///getting error on this line
const pool = {host: "xxxx", user: "xxxx", password: "xxxx", database: "main", connectionLimit:10 }
Given("connecting to db", function(){
queryTestDb("select * from test LIMIT 5", pool).then(res => console.log(res));
})

Getting "Client does not support authentication protocol requested by server; consider upgrading MySQL client" in Cypress

I was trying to connect to Mysql DB from cypress, did the following steps for the same.
Step 1: Added MySQL library in cypress
Step 2: Added following code in plugins/index.js file (This I have got while searching in google)
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json
env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb :query => {
return queryTestDb(query, config)
},
})
}
Step 3: Added the following dependencies in cypress.json
"env" : {
"db": {
"host": "name",
"user": "user",
"password": "password"
}
}
Step 4: Added the following code in the spec.js file
it('Verify the retrieved data', () => {
cy.task('queryDb','select * from soandso where soandso = value').then((resp) => {
console.log(resp.rows)
})
})
But while running the spec file getting the below error
cy.task('queryDb') failed with the following error: > ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Please help me to fix this error.
can you check your dependency in the package.json?
It should have sth like below.
"dependencies": {
"mysql": "^2.18.1"
}
If you do not have this dependency, you need to run 'npm install mysql'

Column name uniquekey is not working [duplicate]

I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.
How can I use MySQL with Node.js?
Check out the node.js module list
node-mysql — A node.js module implementing the MySQL protocol
node-mysql2 — Yet another pure JS async driver. Pipelining, prepared statements.
node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient
node-mysql looks simple enough:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret',
});
connection.connect(function(err) {
// connected! (unless `err` is set)
});
Queries:
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
node-mysql is probably one of the best modules out there used for working with MySQL database which is actively maintained and well documented.
Since this is an old thread just adding an update:
To install the MySQL node.js driver:
If you run just npm install mysql, you need to be in the same directory that your run your server. I would advise to do it as in one of the following examples:
For global installation:
npm install -g mysql
For local installation:
1- Add it to your package.json in the dependencies:
"dependencies": {
"mysql": "~2.3.2",
...
2- run npm install
Note that for connections to happen you will also need to be running the mysql server (which is node independent)
To install MySQL server:
There are a bunch of tutorials out there that explain this, and it is a bit dependent on operative system. Just go to google and search for how to install mysql server [Ubuntu|MacOSX|Windows]. But in a sentence: you have to go to http://www.mysql.com/downloads/ and install it.
Here is production code which may help you.
Package.json
{
"name": "node-mysql",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.6",
"mysql": "^2.5.4"
}
}
Here is Server file.
var express = require("express");
var mysql = require('mysql');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'address_book',
debug : false
});
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.query("select * from user",function(err,rows){
connection.release();
if(!err) {
res.json(rows);
}
});
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
Reference : https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
KnexJs can be used as an SQL query builder in both Node.JS and the browser.
I find it easy to use. Let try it - Knex.js
$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
You can use it like this
knex.select('*').from('users')
or
knex('users').where({
first_name: 'Test',
last_name: 'User'
}).select('id')
Imo, you should try MySQL Connector/Node.js which is the official Node.js driver for MySQL.
See ref-1 and ref-2 for detailed explanation.
I have tried mysqljs/mysql which is available here, but I don't find detailed documentation on classes, methods, properties of this library.
So I switched to the standard MySQL Connector/Node.js with X DevAPI, since it is an asynchronous Promise-based client library and provides good documentation.
Take a look at the following code snippet :
const mysqlx = require('#mysql/xdevapi');
const rows = [];
mysqlx.getSession('mysqlx://localhost:33060')
.then(session => {
const table = session.getSchema('testSchema').getTable('testTable');
// The criteria is defined through the expression.
return table.update().where('name = "bar"').set('age', 50)
.execute()
.then(() => {
return table.select().orderBy('name ASC')
.execute(row => rows.push(row));
});
})
.then(() => {
console.log(rows);
});
You can also try out a newer effort known as Node.js DB that aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
Specifically you could use its db-mysql driver for Node.js MySQL support.
connect the mysql database by installing a library. here, picked the stable and easy to use node-mysql module.
npm install mysql#2.0.0-alpha2
var http = require('http'),
mysql = require('mysql');
var sqlInfo = {
host: 'localhost',
user: 'root',
password: 'urpass',
database: 'dbname'
}
client = mysql.createConnection(sqlInfo);
client.connect();
For NodeJS mysql connecting and querying example
You can skip the ORM, builders, etc. and simplify your DB/SQL management using sqler and sqler-mdb.
-- create this file at: db/mdb/setup/create.database.sql
CREATE DATABASE IF NOT EXISTS sqlermysql
const conf = {
"univ": {
"db": {
"mdb": {
"host": "localhost",
"username":"admin",
"password": "mysqlpassword"
}
}
},
"db": {
"dialects": {
"mdb": "sqler-mdb"
},
"connections": [
{
"id": "mdb",
"name": "mdb",
"dir": "db/mdb",
"service": "MySQL",
"dialect": "mdb",
"pool": {},
"driverOptions": {
"connection": {
"multipleStatements": true
}
}
}
]
}
};
// create/initialize manager
const manager = new Manager(conf);
await manager.init();
// .sql file path is path to db function
const result = await manager.db.mdb.setup.create.database();
console.log('Result:', result);
// after we're done using the manager we should close it
process.on('SIGINT', async function sigintDB() {
await manager.close();
console.log('Manager has been closed');
});