-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql-with-node.js
More file actions
78 lines (69 loc) · 2.07 KB
/
Copy pathmysql-with-node.js
File metadata and controls
78 lines (69 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const mysql = require('mysql');
const mySqlConf = {
host: 'localhost',
port: '3306',
user: 'root',
password: 'dinga',
database: 'test'
};
const SQLConfig = {};
let pool = mysql.createPool({
connectionLimit: 25,
host: mySqlConf.host,
user: mySqlConf.user,
password: mySqlConf.password,
database: mySqlConf.database,
debug: false
});
/* For Fetching and deleting data from table
For Fetching data
SELECT * from tableName or SELECT * from tableName WHERE tableNameId = anything
For Deleting data from table
DELETE FROM tableName WHERE tableColumn = anything
* */
SQLConfig.executeQuery = function (query, callback) {
pool.getConnection(function (err, connection) {
if (err) {
return callback(err, null);
}
else if (connection) {
connection.query(query, function (err, rows, fields) {
connection.release();
if (err) {
return callback(err, null);
}
return callback(null, rows);
})
}
else {
return callback(true, "No Connection");
}
});
};
/* Using post update query we can add new row or we can update existing row
* For adding new row
* INSERT INTO tableName SET ?" // provide json obj at place of placeholder (?)
*
* For updating existing row
* UPDATE tableName SET ? WHERE columnName=anything // provide json obj at place of placeholder (?)
* */
SQLConfig.executePostQuery = function (query, data, callback) {
pool.getConnection(function (err, connection) {
if (err) {
return callback(err, null);
}
else if (connection) {
connection.query(query, data, function (err, rows, fields) {
connection.release();
if (err) {
return callback(err, null);
}
return callback(null, rows);
})
}
else {
return callback(true, "No Connection");
}
});
};
module.exports = SQLConfig;