mysql-crud-operations
An NPM package for mySQL operation.
There are total 11 methods are availible with this package. Add module:
var pool = require('./db');
var dbConfig = require('mysql-operations');
1. Insert Data:
Returns last inserted id
dbConfig.insertData(pool, tableName, insertData, function(err, response) { res.send(response); });
2. Get All Data
Returns an array of objects
dbConfig.getAll(pool, tableName, condition, fields, orderBy, offset, limit, groupBy, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition or you want to get all data from table can be an object too. fields ='field1, field2, field3' or '*' // orderBy = 'field desc' //blank in case don't you want to order offset = 0 //any number limit = 5 //any number groupBy = 'field' //blank in case don't you want to group
3. Get Row Data
Returns an objects
dbConfig.getRow(pool, tableName, condition, fields, orderBy, groupBy, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too. fields ='field1, field2, field3' or '*' // orderBy = 'field desc' //blank in case don't you want to order groupBy = 'field' //blank in case don't you want to group
4. Get Row Count
Returns total count
dbConfig.countRows(pool, tableName, condition, fields, groupBy, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too. fields ='field1, field2, field3' or '*' // groupBy = 'field' //blank in case don't you want to group
5. Get Sum of Column
Returns Sum of column
dbConfig.sumCloumn(pool, tableName, condition, field, groupBy, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too. fields ='field' //can only be one and must be int type groupBy = 'field' //blank in case don't you want to group
6.Get Average of Column
Returns Sum of column
dbConfig.avgCloumn(pool, tableName, condition, field, groupBy, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too. fields ='field' //can only be one and must be int type groupBy = 'field' //blank in case don't you want to group
7. Update data in table
Returns numbers of affetced rows
dbConfig.updateData(pool, tableName, condition, dataToUpdate, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too. dataToUpdate ={fieldName1: value1, fieldName2: value2} //object with
8. Delete data from table
Returns numbers of affetced rows
dbConfig.deleteData(pool, tableName, condition, function(err, response) { res.send(response); });Note:-
condition ='' //in case of no condition, can be an object too.
9. Get Result in Response from a custom made query
Returns an array of objects
dbConfig.queryWithResult(pool, query, function(err, response) { res.send(response); });Note:-
query ='' //a valid sql to get Results
10. Get Row in Response from a custom made query
Returns an objects
dbConfig.queryWithRow(pool, query, function(err, response) { res.send(response); });Note:-
query ='' //a valid sql to get Rows
11. Execute Custom Query Without Resultset
Return True if query executed successfully
dbConfig.queryWithNoResultSet(pool, query, function(err, response) { res.send(response); });Note:-
query ='' //a valid sql to get Rows