Nodejs MySQL Query Builder
A small lib that builds query using mysql and nodejs
credits to https://github.com/felixge/node-mysql for mysql nodejs module
Table of Contents
- Introduction
- Database Configuration and Connecting to your Database
- Connection Options
- Queries
- Select Statement
- Update Statement
- Delete Statement
- Insert Statement
- Native Query
- Set Field Clause
- Build a Condition Clause
- Build a Join Clause
- View the Generated Query
Introduction
This is a small mysql query builder using the nodejs mysql driver by felixge.
Sample Usage:
var query_builder = ; var dbconn_default = host : 'host' user : 'user' pass : 'password' dbase : 'database_schema'; var qb = dbconn_default ; var select_details = table : 'table_name' fields : 'field1' 'field2' ... conditions : field1 : 'value1' field2 : 'value2' ; qb;
By this, we can generate a query something like this:
SELECT `field1`, `field2` FROM `table_name` WHERE `field1` = 'value1' AND `field2` = 'value2'
Database Configuration and Connecting to your Database
This module used the poolcluster functionality that provides multiple host connection.
Below is how you connect to your default database schema.
var dbconn_default = host : 'host' user : 'user' pass : 'password' dbase : 'database_schema'; var default_schema = dbconn_default ;
OR
var dbconn_default = host : 'host' user : 'user' pass : 'password' dbase : 'database_schema'; var qb = ; qb;
Connect in other database
var dbconn1 = host : 'host' user : 'user' pass : 'password' dbase : 'other_database_schema' pool_name : 'DBCONN1'; qb; qb;
Connection Options
Configuration to establish connection are as follows:
host
: Hostname of the database.user
: The username used to connect to the database.pass
: The password used to connect to the database.dbase
: The name of the database you want to connect to.pool_name
: Connection name identifier ( use when using multiple connections ).
Queries
To submit a query, use the following functions.
qb;qb;qb;qb;qb;
This will automatically generate, escape and execute a query.
Select Statement
Options for Select Statement
table
: The table name that you want to select. Required.fields
: An array of table fields that you want to show.conditions
: This is forWHERE
clause. Please seeBuild a Condition Parameters
.joins
: This is forJOIN
portion of your query. Please seeBuild a Join Parameters
.order
: Set anORDER BY
clause.group
: Permits you to create aGROUP BY
clause.limit
: Limit the number of rows you would like returned by the query.start_row
: Set a result offset. Commonly used on pagination functionality.count
: Determine the number of rows in a particular table. A boolean type. Set tofalse
by default.count_fields
: Field set withCOUNT
option. Sample is COUNT( users.id ) as X. This will overwrite thefields
option.show_query
: View the generated query. Please seeView the Generated Query
.
qb;
Update Statement
Options for Update Statement
table
: The table name where you want to update a particular record. Required.details
: This is where we set the fields and its new values. Please see buildSet Fields Parameters
.conditions
: This is forWHERE
clause. Please seeBuild a Condition Parameters
.show_query
: View the generated query. Please seeView the Generated Query
.
qb;
Delete Statement
Options for Delete Statement
table
: The table name where you want to delete a particular record. Required.conditions
: This is forWHERE
clause. Please seeBuild a Condition Parameters
.show_query
: View the generated query. Please seeView the Generated Query
.
qb;
Insert Statement
Options for Insert Statement
table
: The table name where you want to insert a particular record. Required.details
: This is where we set the fields and its values. Please see buildSet Fields Parameters
.show_query
: View the generated query. Please seeView the Generated Query
.
qb;
Native Query
Options for executing a query
query
: Your query statement to execute.pool_name
: Use this if you have multiple connections. Optional.
qb;
Set Field Clause
This is for select and insert queries.
To build on what fields to insert and/or update, please create an object of field names and values
field1 : 'value' field2 : 'value'
This will generate
UPDATE `table` SET `field1` = 'value', `field2` = 'value';INSERT INTO `table` SET `field1` = 'value', `field2` = 'value';
Build a Condition Clause
This is where we build the WHERE
clause.
For simple condition, this will generate an AND operator by default
field1 : 'value' field2 : 'value'
`field1` = 'value' AND `field2` = 'value'
Joined by OR
or : field1 : 'value' field2 : 'value'
`field1` = 'value' OR `field2` = 'value'
IN clauses
field1 : field1 : 'value' field2 : 'value'
`field1` IN( 'value', 'value' )
NOT clauses
1 not : field1 : 'value' field2 : 'value' 2 'field1 !=' : 'value'
1. `field1` != 'value' AND `field2` != 'value' 2. `field1` != 'value'
LIKE clause, by default, this will joined by AND
'field1 LIKE' : '%value%' 'field2 NOT LIKE' : 'value%'
`field1` LIKE '%value%' AND `field2` NOT LIKE 'value%'
OR LIKE clause
LIKE clause, by default, this will joined by AND
or : 'field1 LIKE' : '%value%' 'field2 NOT LIKE' : 'value%'
`field1` LIKE '%value%' OR `field2` NOT LIKE 'value%'
Nested clause
and : and : key1 : 'value' key2 : 'value' or : key3 : 'value' key4 : 'value' not : key5 : 'value' key6 : 'value'
( ( `key1` = 'value' and `key2` = 'value' ) and ( `key3` = 'value' or `key4` = 'value' ) and ( `key5` != 'value' AND `key6` != 'value' ) )
Other operators
'field1 !=' : 1 'field2 <=' : 1 'field3 >=' : 1
field1 != 1 AND field2 <= 1 AND field3 >= 1
Summary of Condition clause
field1 : 'value' field2 : 'value' 'field1 !=' : 'value' 'field1 LIKE' : '%value%' 'field2 NOT LIKE' : 'value%' 'field1 !=' : 1 'field2 <=' : 1 'field3 >=' : 1 or : field1 : 'value' field2 : 'value' 'field1 LIKE' : '%value%' 'field2 NOT LIKE' : 'value%' and : field1 : 'value' field2 : 'value' field3 : key1 : 'value' key2 : 'value' not : field1 : 'value' field2 : 'value' and : and : key1 : 'value' key2 : 'value' or : key3 : 'value' key4 : 'value' not : key5 : 'value' key6 : 'value'
SELECT * FROM `table` WHERE `field1` = 'value' AND `field2` = 'value' AND field1 != 1 AND field1 LIKE '%value%' AND field2 NOT LIKE 'value%' AND field2 <= 1 AND field3 >= 1 AND ( `field1` = 'value' or `field2` = 'value' or field1 LIKE '%value%' or field2 NOT LIKE 'value%' ) AND ( ( `key1` = 'value' and `key2` = 'value' ) and ( `key3` = 'value' or `key4` = 'value' ) and ( `key5` != 'value' AND `key6` != 'value' ) ) AND `field3` IN ( 'value', 'value' ) AND ( `field1` != 'value' AND `field2` != 'value' ) ;
Build a Join Clause
Permits you to write the JOIN portion of your query.
Options are as follows:
joins
: An object of table details to join.type
: Optional and this isJOIN
by default. Join types areleft
,right
andinner
.ON clause
: This is where the ON condition resides. Similar toCondition
parameter format.
qb;
SELECT * FROM `users` LEFT OUTER JOIN `user_types` ON users.user_type_id != user_types.user_type_id
View the Generated Query
This will log the generated query using console.log()
.
Setting is as simple as assigning a boolean value to show_query
. This is set false
by default.