npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-mysql-query

v0.1.2

Published

Saving your time while using the "mysql" module, with critical word auto uppercase.

Downloads

15

Readme

easy-mysql-query

Saving your time querying while using the Mysql module based on NodeJS.

how to use

First, npm install the module.

npm install easy-mysql-query

you can run (not necessarily) the test and instances by npm run test under the 'development' mode.

Second, require the module in your project.

var easyMysqlQuery = require('easy-mysql-query');

Third, set the mysql settings.

easyMysqlQuery.setConnection({
  host: '127.0.0.1', // mysql address
  port: '3306', // mysql port
  user: 'root', // your user name
  password: '', // your password
  database: '' // choose the database
});

Or use the Mysql pool:

easyMysqlQuery.setPool({
  host: '127.0.0.1', // mysql address
  port: '3306', // mysql port
  user: 'root', // your user name
  password: '', // your password
  database: '', // choose the database
  connectionLimit: 15 // set the pool limit
});

Finally, we can start our happy-querying! The function ruquires 5 params:

easyMysqlQuery(method, table, columns, selectors, callback);

For example:

easyMysqlQuery('select', 'users', ['name', 'age'], ['WHERE id=', ['001']], function(e, result) {
  if (e) {
    throw e;
  }
  console.log(result);
});

The callback function receives two parameters, first is Error if any, second is the querying result of Mysql module.

params

methods

Four methods: 'insert', 'delete', 'update', 'search'.

table

Table must be a String, which is the single table you want to use.

columns

Columns can be any type during 'insert' method. Columns contains the values and certain column names.

Columns can be String or Number if there is only one column in the table.

var columns = 'ABC';

Columns can be Array if all columns are set in the table.

var columns = ['Tom', 24, 10000];

Columns must be an Object if you want to set certain columns.

var columns = {
  name: 'Tom',
  age: 24
}; 

Columns will be ignored during 'delete' method. You can set null.

easyMysqlQuery('delete', 'users', null, ['WHERE id=', ['001']], function(e, result) {});

Columns must be an Object during 'update' method. Like the insert method with the column names.

var columns = {
  name: 'Tom',
  age: 24
}; 

Columns can be String or Array during 'search' method. This time it only contains column names.

var columns = 'name';
var columns = ['name','age','salary'];

selectors

The selectors contains all the criterias added to your query. To make it simple and diretly, just use an Array and write your selectors like a normal query. For example:

var selectors = ['WHERE id=',[123],'AND age>',[20],'OR name LIKE',['Tom%'],'LIMIT',[5,5]];

Don't worry about the spaces between strings, or the semicolon after the query, we will do it for you. Note that you have to put your variables into an Array, so we can distinguish it from normal elements. Meanwhile multi-elements in one array will be seperated by ','.

Please put variables into Array if you want to use the anti-sql-injection of Mysql module. In this way we can replace variables with '?' and put the real value into an independent Array. For example:

mysql.query('SELECT * FROM users WHERE age=? AND salary<?;', ['30', 10000]), callback);

However, if the easy-mysql-query module works in query mode or if you don't really care about the origin anti-sql-injection of Mysql, you don't have to put variables into an Array. It will work like Array.join(), still we will add spaces while necessary. Selectors can also be an Object. But it only works if all criterais is '=' and all logic is 'AND' and will be applied to 'WHERE' selector.

var selectors = {id: '001'};

two modes

The easy-mysql-query module has two working modes, 'execute' by default and 'query'. You can switch mode by:

easyMysqlQuery.setMode('query');

In query mode, callback function will be ignored, the final query will be returned and you can use it in your own way. In execute mode, we will automatically query through the Mysql module, and pass Error or result to the callback function.

Missing callback will automatically change to 'query' mode.

auto-uppercase

We will check the common-used reserved key words in your query and change them to uppercase if possible. This will be useful in 'query' mode. For example:

var selectors = ['where year(birthday) between',['1988-06-02'],'and',['1993-12-31']];

will come out as

WHERE YEAR(birthday) BETWEEN \'1988-06-02\' AND \'1993-12-31\'

output Mysql settings

var settings = easyMysqlQuery.getPool();
var settings = easyMysqlQuery.getConnection();