How to make SQL query on your local MySQL
Blog 💡: Use Znote as SQL client
Install SQL driver for your database
npm install --save mysql2
npm install --save sequelize
Install tablify to read results as table
npm install --save tablify
Create these 2 functions f(x) below to connect to your database and tablify results or just run the block of code to declare functions before running the last block.
// global 1
// custom functions
async function localSQL(sqlQuery) {
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize("", "root", "root", {
dialect: "mysql"/* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
host: "localhost"
});
sequelize.authenticate();
const [results, metadata] = await sequelize.query(sqlQuery);
sequelize.close()
return results;
}
async function printSQL(sqlQuery) {
const tablify = require('tablify').tablify
print(tablify(await localSQL(sqlQuery)));
}
await printSQL("select * from book.vente_item limit 10");