Skip to content

Experience-Monks/mysql-where

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mysql-where

stable

A utility to create clean WHERE statements from an object using the mysql node library

Usage

NPM

This module will create clean and escaped WHERE statements from an object. It uses a syntax similar to mondob to determine the operators. Simply pass an object to it and will return a string for use in your sql statements.

createWhere({
  category: 'blog',
  date: {$gte: '2016-01-12', $lte: '2016-01-14'},
  author: {$ne: 'steve'}
});
// returns "`category` = 'blog' AND `date` <= '2016-01-14' AND `date` >= '2016-01-12' AND `author` != 'steve'"

Here is how you use it with the mysql library.

var createWhere = require('mysql-where');
db.query('SELECT * FROM table WHERE '+createWhere({
  category: 'blog',
  date: {$gte: '2016-01-12', $lte: '2016-01-14'},
  author: {$ne: 'steve'}
}),function(err,rows) {
  
});

createWhere(statement,join=' AND ')

statement Any normal key / value pairs will be escaped properly and join with a '=' in your where statement. There are also 5 special keys to change there operator.

$gte equates to >=
$lte equates to <=
$gt equates to >
$lt equates to <
$ne equates to !=

join By default these are all joined via ' AND ' but you can pass in a second parameter to act as a different joiner, such as ' OR '

License

MIT, see LICENSE.md for details.

About

MySQL where statement module, useful with the node mysql library

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published