mysql-where

1.0.2 • Public • Published

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.

Package Sidebar

Install

npm i mysql-where

Weekly Downloads

1

Version

1.0.2

License

MIT

Last publish

Collaborators

  • hperigo
  • gabroell
  • josdelgaur
  • jam3dev
  • njam3
  • iranreyes
  • namniak
  • donghyukjang
  • alemesa
  • neoli
  • colin-j3
  • jmckinnell
  • n0wak
  • amelierosser
  • artemleznikov
  • jeffong
  • nickfish