Switching the npm SQL pool from Postgres to MySQL

Here is the code snippet I am currently working with:

    console.log("-------------- Establishing connection to the database...");
    const client = await pool.connect();
    console.log("-------------- Connection successfully established.");
    console.log("-------------- Executing query.");  
    var sql = `SELECT * FROM users WHERE email = '${email}'`;
    console.log("-------------- Query executed successfully.");
    let query = await client.query(sql);
    client.release();
    pool.release();
    if(query.rows[0]){return "Email already exists. Please login."}
    return 0;

I would like to achieve this using:

    pool.getConnection

Can anyone guide me on how to accomplish it?

Answer №1

If you are utilizing the npm package called mysql, make sure to refer to the documentation in the readme file for information on the pool feature.

https://www.npmjs.com/package/mysql#pooling-connections

const mysql = require('mysql');
const pool  = mysql.createPool(...);

const email = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="760213050236130e171b061a15d8360502640762565" target="_blank" rel="noopener noreferrer nofollow">[email protected]</a>" // Storing user's email address.

pool.getConnection(function(err, connection) {
  if (err) throw err; // Connection failed!

  // Utilize the connection
  connection.query('SELECT * FROM users WHERE email = ?', [email], function (error, results, fields) {
    // Release the connection after usage.
    connection.release();

    // Handle errors once connection is released.
    if (error) throw error;

    // Do not use the connection at this point as it has been returned to the pool.

    if(!results.length) {
     // No results were found.
    }else {
     // Results have been found.
    }
  });
});

Please note that I have not executed this code yet, so if you encounter any issues, I recommend consulting the provided documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble initiating a cloned React application

After cloning a react app from Github to my local computer, I encountered an issue when trying to run 'npm start' which resulted in the following error message: ➜ sweet-movie-app git:(master) npm start internal/modules/cjs/loader.js:596 ...

Having trouble with preventDefault() not working during a keyup event?

I am struggling to make preventDefault() function properly. Here are a few variations of the code that I have attempted: First: $(document).keyup(function (evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode == 192) { ...

combining the package.json files of the client and server into a single file

I need assistance with merging server-side code (in nodejs) and client-side code (with react) into a single package.json file. The server file is located in the project root directory, while the client-side code resides in the /client folder along with oth ...

Add items to a new array with a property set to true, if they are present in the original array

Could someone assist me with the following situation? I need an array of strings. A function is required to map the array of strings, assigning each element a name and key, while also adding another object "checked: false". Another function should take t ...

Changing the story in javascript

I am trying to customize the legend to display the following values: 80+ (or 80%+) 75-80 70-75 65-70 60-65 55-50 <50% While I have organized the list in descending order, I seem to be facing an issue with getting the less than symbol to function correct ...

Tips for sending a variable from Javascript to node.js, specifically connecting to MYSQL

Can you help me with a simple example on how to pass a variable from JavaScript to Node.js? I need to store the user input from a text box in Node.js and perform some actions. Client <!DOCTYPE html> <html> <body> <h2>HTML Forms< ...

Using jQuery to select the third element from every group of six

I am attempting to select the 3rd .foo element out of every set of 6. To clarify, here is a brief example: 1 2 3 (this) 4 5 6 1 2 3 (this) 4 5 6 and so on... So far, I have only managed to target every 3rd element, which is not exactly what I need beca ...

Mastering the art of sequential requests with Node.js and Axios

I have a requirement to send 2 requests to my API in order to insert data into 2 separate tables: Workflow: The process involves making a request to retrieve the last ID + 1, creating an array (last_id, values), and then performing two MySQL INSERT opera ...

Identify alterations in an input field after selecting a value from a dropdown menu

Is there a way to detect changes in the input field when selecting a value from a drop-down menu, similar to the setup shown in the image below? html: <input type="text" class="AgeChangeInput" id="range"/> js:(not working) <script> $(docume ...

Is it possible to pass an AngularJS ng-form object as a parameter in ng-if?

When I try to preview, the save button in my preview mode remains enabled. You can view the code snippet here: http://plnkr.co/edit/I3n29LHP2Yotiw8vkW0i I believe this issue arises because the form object (testAddForm) is not accessible within the ng-if s ...

Organizing the dropdown menu in alphabetical order

I am facing an issue with the following element <li id="li_15" class="dropdown dropdown-alpha highlighted" style=""> <label class="description" for="element_15">Name <span id="required_15" class="required">*</span></labe ...

Tips for utilizing a vue.js nested for loop with two arrays using v-for

The issue has been resolved, and both my parent view and child component code are now correct and functioning properly I am using Vue.js with the goal of iterating similarly to a nested for loop to display a matrix table. Initially, I tried to achieve thi ...

Validation script needed for data list selection

<form action="order.php" method="post" name="myForm" id="dropdown" onsubmit="return(validate());"> <input list="From" name="From" autocomplete="off" type="text" placeholder="Starting Point"> <datalist id="From"> <option ...

What is the best way to display two columns in each row using Angular?

Can you please provide guidance on how to display two columns in each row using Angular? I am attempting to showcase only two columns per row, and if there are more than four items, I want to display them on an ion-slide. Further details will be provided. ...

Managing Emails with Vue and Firestore

I am facing an issue with updating the 'email' field. Whenever I try to change the email address, it gets updated correctly. However, when I attempt to log in again, the new email address does not work; only the old one seems to be functional. Ho ...

The selected jQuery plugin is not functioning properly within CodeIgniter framework

I recently downloaded the jQuery Chosen plugin to use the simple "multiselect" version on my website. I followed all the necessary steps and even copied and pasted the code into CodeIgniter. Despite my experience with jQuery, I am facing an issue where the ...

How Python Flask sends a string as &#34 to an HTML page

I am working on a Flask app and I need to send simple JSON data from the app.py file to an HTML page. Here is the relevant code in my app.py: jsonArr = [{"type": "circle", "label": "New York"}, {"type": "circle", "label": "New York"}] return ...

Error encountered with underscore template - Unforeseen SyntaxError: Unexpected token <

I encountered an error when attempting to load one of my underscore templates. It seems to be related to an issue in the for loop, which I suspect should be a .each loop, but I'm still trying to grasp its structure. Here is a snippet of my template: ...

Move the Ember application from using bower to npm for package management

It seems that the Ember Team is recommending migrating applications from bower to npm, but I have not been able to find a clear migration plan for this. Has anyone successfully migrated and could share their experience or provide useful links with detailed ...

Developing Modules in NodeJS using Constructors or Object Literals

I am currently developing a nodejs application that needs to communicate with various network resources, such as cache services and databases. To achieve this functionality, I have created a module imported through the require statement, which allows the a ...