mysql-qbuilder
Advanced tools
Comparing version 1.2.0 to 1.3.1
54
index.js
const mysql = require('mysql') | ||
const queryBuilder = require('./lib/QueryBuilder') | ||
const dbBuilder = require('./lib/DatabaseBuilder') | ||
const qModel = require('./lib/ModelBinding') | ||
// const dbBuilder = require('./lib/DatabaseBuilder') | ||
const queryModel = require('./lib/QueryModel') | ||
const parameters = require('./lib/Parameters') | ||
@@ -44,18 +44,18 @@ | ||
/** | ||
* [Start to change the Database strcture] | ||
* @return {DatabaseBuilder} [Database Builder Creator for modify the structure of Table on database] | ||
*/ | ||
exports.setDatabaseStructure = () => { | ||
return dbBuilder | ||
} | ||
// /** | ||
// * [Start to change the Database strcture] | ||
// * @return {DatabaseBuilder} [Database Builder Creator for modify the structure of Table on database] | ||
// */ | ||
// exports.setDatabaseStructure = () => { | ||
// return dbBuilder | ||
// } | ||
/** | ||
* [Make table scheme and use some common methods] | ||
* @param {Array} schema [List of columns on table which You want to used for current case] | ||
* @param {String} table [Database table for that schema] | ||
* @return {ModelBinding} [description] | ||
* @param {String} [table=''] [Database table for that schema] | ||
* @return {ModelBinding} [description] | ||
*/ | ||
exports.useScheme = (schema, table) => { | ||
return qModel.setSchema(schema, table) | ||
exports.useScheme = function (table = '') { | ||
parameters.params = [] | ||
return queryModel.setTable(table) | ||
} | ||
@@ -124,28 +124,4 @@ | ||
exports.prepare = function () { | ||
if (parameters.option === 'select') { | ||
parameters.command = parameters.select + parameters.from + | ||
parameters.join + parameters.groupBy + parameters.where + | ||
parameters.orderBy + parameters.limit + parameters.skip | ||
} else if (parameters.option === 'insert') { | ||
parameters.command = parameters.insert | ||
} else if (parameters.option === 'update') { | ||
parameters.command = parameters.update + parameters.where + parameters.limit + parameters.skip | ||
} else if (parameters.option === 'delete') { | ||
parameters.command = parameters.delete + parameters.where + parameters.limit + parameters.skip | ||
} else { | ||
parameters.command = parameters.where + parameters.limit + parameters.skip | ||
} | ||
parameters.prepare() | ||
parameters.select = '' | ||
parameters.from = '' | ||
parameters.join = '' | ||
parameters.where = '' | ||
parameters.groupBy = '' | ||
parameters.orderBy = '' | ||
parameters.limit = '' | ||
parameters.skip = '' | ||
parameters.insert = '' | ||
parameters.update = '' | ||
parameters.delete = '' | ||
return this | ||
@@ -152,0 +128,0 @@ } |
const parameters = require('./Parameters') | ||
// Show every method for operation with tables like delete, insert, update | ||
@@ -4,0 +3,0 @@ exports.check = function (column, operator, value) { |
@@ -15,4 +15,33 @@ module.exports.select = '' | ||
module.exports.where = '' | ||
module.exports.command = '' | ||
module.exports.option = '' | ||
module.exports.params = [] | ||
module.exports.option = '' | ||
module.exports.prepare = function () { | ||
if (this.option === 'select') { | ||
this.command = this.select + this.from + | ||
this.join + this.groupBy + this.where + | ||
this.orderBy + this.limit + this.skip | ||
} else if (this.option === 'insert') { | ||
this.command = this.insert | ||
} else if (this.option === 'update') { | ||
this.command = this.update + this.where + this.limit + this.skip | ||
} else if (this.option === 'delete') { | ||
this.command = this.delete + this.where + this.limit + this.skip | ||
} else { | ||
this.command = this.where + this.limit + this.skip | ||
} | ||
this.select = '' | ||
this.from = '' | ||
this.join = '' | ||
this.where = '' | ||
this.groupBy = '' | ||
this.orderBy = '' | ||
this.limit = '' | ||
this.skip = '' | ||
this.insert = '' | ||
this.update = '' | ||
this.delete = '' | ||
} |
@@ -710,5 +710,1 @@ const parameters = require('./Parameters') | ||
} | ||
// module.exports.whereExists = function () { | ||
// | ||
// } |
{ | ||
"name": "mysql-qbuilder", | ||
"version": "1.2.0", | ||
"version": "1.3.1", | ||
"description": "Query Builder for MySQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
345
README.md
@@ -7,5 +7,38 @@ # mysql-qbuilder | ||
[![npm](https://img.shields.io/npm/l/mysql-qbuilder.svg)]() | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
[![npm](https://img.shields.io/npm/dm/mysql-qbuilder.svg)]() | ||
[![npm](https://img.shields.io/npm/dt/mysql-qbuilder.svg)]() | ||
## Table of Contents | ||
- [Install](#install) | ||
- [Introduction](#introduction) | ||
- [Before Start to Used](#before-start-to-used) | ||
- [Clauses](#clauses) | ||
- [SELECT](#select) | ||
- [INSERT](#insert) | ||
- [FROM Table](#from-table) | ||
- [DELETE](#delete) | ||
- [UPDATE](#update) | ||
- [JOIN](#join) | ||
- [ORDER BY](#order-by) | ||
- [GROUP BY](#group-by) | ||
- [LIMIT](#limit) | ||
- [OFFSET](#offset) | ||
- [WHERE](#where) | ||
- [All Query Builder Functions](#query-builder) | ||
- [How to Execute](#how-to-execute) | ||
- [Helper Queries](#helper-queries) | ||
- [Add](#add) | ||
- [Get First](#get-first) | ||
- [Get Last](#get-last) | ||
- [Get All](#get-all) | ||
- [Find by Id](#find-by-id) | ||
- [Find by Fields](#find-by-fields) | ||
- [How to write Your own Query](#how-to-write-own-query) | ||
- [Get Mysql Module](#get-mysql-module) | ||
- [Change log](#change-log) | ||
## Install | ||
SQL Query builder working with NodeJS | ||
@@ -26,3 +59,34 @@ ``` | ||
## What You get from that module | ||
## Before Start to Used | ||
Before to start to used the mysql-qbuilder is need to set some options | ||
In this section is show What You need for to work with mysql-qbuilder | ||
### Require the module | ||
```JavaScript | ||
const qBuilder = require('mysql-qbuilder') | ||
``` | ||
### Set the parameters for mysql connection | ||
```JavaScript | ||
qBuilder.setOptions({ | ||
hostname: 'hostName', | ||
username: 'userName', | ||
password: 'passWord', | ||
database: 'databaseName' | ||
}) | ||
``` | ||
### Then connecto to database | ||
```JavaScript | ||
qBuilder.connectToDatabase() | ||
``` | ||
### Start to make query | ||
```JavaScript | ||
qBuilder.makeQuery() | ||
``` | ||
## Clauses | ||
All common cases to use the `sql query clauses` | ||
@@ -60,33 +124,5 @@ --- | ||
## How to use | ||
## SELECT | ||
### `select()` `addSelect()` | ||
### Require the module | ||
```JavaScript | ||
const qBuilder = require('mysql-qbuilder') | ||
``` | ||
### Set the parameters for mysql connection | ||
```JavaScript | ||
qBuilder.setOptions({ | ||
hostname: 'hostName', | ||
username: 'userName', | ||
password: 'passWord', | ||
database: 'databaseName' | ||
}) | ||
``` | ||
### Then connecto to database | ||
```JavaScript | ||
qBuilder.connectToDatabase() | ||
``` | ||
### Start to make query | ||
```JavaScript | ||
qBuilder.makeQuery() | ||
``` | ||
## Select `select()` `addSelect()` | ||
Some times You don't want to select all columns from database | ||
@@ -117,3 +153,4 @@ Then You need to enter just the columns. | ||
## Insert `add()` | ||
## INSERT | ||
### `add()` | ||
@@ -132,3 +169,4 @@ Many times You don't want to get the data from database. Just want to add new record | ||
## From, Table `from()` `table()` | ||
## FROM Table | ||
### `from()` `table()` | ||
@@ -148,3 +186,4 @@ The from and table method is just set the table of query | ||
## DELETE `delete()` | ||
## DELETE | ||
### `delete()` | ||
@@ -161,3 +200,4 @@ The query builder may also be used to delete records from the table used delete function | ||
## UPDATE `update()` | ||
## UPDATE | ||
### `update()` | ||
@@ -177,3 +217,4 @@ Some times You don't want to select or add new columns or delete | ||
## JOIN `join()` | ||
## JOIN | ||
### `join()` | ||
@@ -190,3 +231,3 @@ With Join is possible to get from database record from two tables with one query. | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
// data is the array of objects | ||
}) | ||
@@ -196,3 +237,4 @@ ``` | ||
## ORDER BY `orderBy()` | ||
## ORDER BY | ||
### `orderBy()` | ||
@@ -214,3 +256,4 @@ Is A method which You can order by some column | ||
## GROUP BY `groupBy()` | ||
## GROUP BY | ||
### `groupBy()` | ||
@@ -227,7 +270,8 @@ Is A method which You can group by some column | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
// data is the array of objects | ||
}) | ||
``` | ||
## LIMIT `take()` | ||
## LIMIT | ||
### `take()` | ||
@@ -244,3 +288,3 @@ Is A method which You can get only few records from database | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
// data is the array of objects | ||
}) | ||
@@ -250,3 +294,4 @@ ``` | ||
## OFFSET `skip()` | ||
## OFFSET | ||
### `skip()` | ||
@@ -264,3 +309,3 @@ Is A method which You can skip first few records from database | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
// data is the array of objects | ||
}) | ||
@@ -271,3 +316,4 @@ ``` | ||
## WHERE `where()` | ||
## WHERE | ||
### `where()` | ||
@@ -285,3 +331,3 @@ You may use the where method on a query builder instance to add where clauses to the query | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -301,3 +347,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -316,3 +362,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -331,3 +377,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -349,3 +395,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -364,3 +410,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -379,3 +425,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -394,3 +440,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -409,3 +455,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -424,3 +470,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -439,3 +485,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -454,3 +500,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -469,3 +515,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -484,3 +530,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -499,3 +545,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -514,3 +560,3 @@ ``` | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
@@ -520,3 +566,3 @@ ``` | ||
## Used one of that functions for create sql query | ||
## All Query Builder Functions | ||
```JavaScript | ||
@@ -649,32 +695,76 @@ | ||
### After we finish with the build the query is need to prepare and execute | ||
## How to Execute | ||
### Is have two different ways to execute the created query | ||
#### 1) With execute which not return result | ||
Is possible to used when You want to add new record on database | ||
```JavaScript | ||
// Build the query from all simple parts | ||
qBuilder.prepare() | ||
// Set all parameters which You need to used on mysql query builder | ||
.setParameters([param1, param2, param3]) | ||
// Just execute the query and no return result | ||
// Just execute the query and don't return some result | ||
.execute() | ||
``` | ||
#### 2) With getResult which return result | ||
Is possible to used when You want to get some records from database | ||
### OR | ||
```JavaScript | ||
// Build the query from all simple parts | ||
qBuilder.prepare() | ||
// Set all parameters which You need to used on mysql query builder | ||
.setParameters([param1, param2, param3]) | ||
// Get the result of executed query | ||
.getResult((err, data) => { | ||
if (err) console.log(err) | ||
.getResult((err, data) => { // is return the Array | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
// data is result of mysql query and is return like array of objects or single object | ||
console.log(data) | ||
## Helper Queries | ||
From v1.3.1 Is possible to used mysql-qbuilder much easier for some common cases like | ||
* Add some record on database | ||
* Get first element from table | ||
* Get last element from table | ||
* Get all elements from table | ||
* Find element by Id | ||
* Find elements by few columns from table | ||
### IMPORTANT: For to Used Helper Queries is not need to start with `makeQuery()` | ||
### Is need to start with `useScheme('tableName')` | ||
### If You want to used same table from database then is not need anymore to set table only used `useScheme()` | ||
## Add | ||
Add new record on database | ||
```JavaScript | ||
qBuilder.useScheme('tableName') | ||
// First field is Object where the keys is column of database and values is the values of columns | ||
.add({ 'username': 'administrator', 'email': 'administrator@admin.com' }) | ||
qBuilder.execute() | ||
``` | ||
## Get First | ||
Get first record from table | ||
```JavaScript | ||
qBuilder.useScheme() // used same table like before | ||
// First field is String or Array with all columns which You want to get from database | ||
.getFirst('email, username') | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
@@ -684,19 +774,93 @@ ``` | ||
### If You don't trust of the developer then You have option to write Your own query | ||
## Get Last | ||
Get last record from table | ||
```JavaScript | ||
qBuilder.useScheme() // used same table like before | ||
// First field is String or Array with all columns which You want to get from database | ||
.getLast('email, username') | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
## Get All | ||
Get all records from table | ||
```JavaScript | ||
qBuilder.useScheme() // used same table like before | ||
// First field is String or Array with all columns which You want to get from database | ||
.getAll('email, username') | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
## Find by Id | ||
Find record on database by entered id | ||
```JavaScript | ||
qBuilder.useScheme('tableName') | ||
// First field is id of the record | ||
// Second is all columns which You want to get from database | ||
.findById(2, 'email') | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
## Find by Fields | ||
Find records on database by many search conditions or (only one) | ||
```JavaScript | ||
qBuilder.useScheme('tableName') | ||
// First field is Object where the keys is column of database and values is the values of columns | ||
// Second is all columns which You want to get from database | ||
// Third is do You want every condition to be true or only one | ||
.findByFields({ 'id': 2, 'username': 'administrator' }, 'email', 'or') | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
## How to write Your own Query | ||
### To write Your own query is need only to call method setCommand | ||
```JavaScript | ||
qBuilder.setCommand('SELECT * FROM Table WHERE id > ?') | ||
// Build the query from all simple parts | ||
.prepare() | ||
// Set all parameters which You need to used on mysql query builder | ||
.setParameters([param1]) | ||
// Get the result of executed query | ||
.getResult((err, data) => { | ||
if (err) console.log(err) | ||
// data is result of mysql query and is return like array of objects or single object | ||
console.log(data) | ||
.getResult((err, data) => { // is return the Array | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
@@ -706,3 +870,4 @@ ``` | ||
### For more mysql advanced functions You can used that which return the mysql module | ||
## Get Mysql Module | ||
### If You want to set more advanced options then You can get the MySql module | ||
@@ -714,4 +879,12 @@ ```JavaScript | ||
## Change log | ||
* v1.2.1 | ||
* v1.3.1 | ||
* * Add Helper Query functions with for some common cases like: | ||
* * * `add` Add record on database | ||
* * * `getFirst` Get first element from table | ||
* * * `getLast` Get last element from table | ||
* * * `getAll` Get all elements from table | ||
* * * `findById` Find record from table by id | ||
* * * `findByFields` Find records from table by selected few columns | ||
* v1.2.0 | ||
* * Add `WhereDay` `WhereMonth` `WhereYear` | ||
* * Add One more (Optional) Parameter on Where methods which is possible choose between AND or OR (Default = AND) |
62871
991
854