Extends the mysql package to
1- Promisify the database connection object.
2- Provide a method for executing queries synchronously.
- Run command:
npm install mysql-sync-query
- Declare a variable/constant to require the installed package e.g.
let db = require("mysql-sync-query");
- The defined variable in the last step represents a class with a constructor that takes an existing database name. e.g.
let databaseObject = new db("employees_db");
Connects Node to an existing local MySQL server. Parameters:
- host - host name e.g. localHost
- port - database connection port e.g. 3306
- user - database login username
- password - database login password
Connects Node to an existing remote MySQL server. Parameters:
- URL - e.g.
process.env.JAWSDB_URL
Executes the input query and returns the affected rows
Parameters:
- textQuery - the SQL query string e.g.
"SELECT * FROM table_name"
Returns: - a JSON object that contains database records.
Terminates the connection.
const db = require("mysql-sync-query");
const dbObj = new db("employees_db");
async function start() {
dbObj.connectLocal("localHost", 3306, "user", "password");
try {
let res = await dbObj.executeQuery("SELECT * FROM employee");
console.log(res);
}
catch (err) {
console.log(err);
}
finally {
dbObj.disconnect();
}
}
start();
None - at this time.
Please email us for further questions at ashkiani@yahoo.com
Node.js, MySQL
Direct link to package: https://www.npmjs.com/package/mysql-sync-query
This software is licensed under MIT license.