ags-sql-gen 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -1
- package/connection.js +103 -32
- package/database/dbConnection.js +2 -2
- package/getJokes.js +2 -2
- package/index.js +7 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
AGS is a lightweight Node.js MySQL helper library that simplifies database operations such as **SELECT**, **INSERT**, **UPDATE**, **DELETE**, and **JOIN** queries.
|
|
5
5
|
It also includes a fun bonus function: `getDadJokes()` — because debugging is better with a laugh. 😄
|
|
6
|
+
`AGS-SQL-Template` is a ready-to-use SQL helper class designed for developers who frequently interact with MySQL databases in Node.js.
|
|
7
|
+
It provides easy methods for `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `JOIN`, and aggregation queries (`SUM`, `MIN`, `MAX`, `AVG`) without needing to manually write long SQL statements every time.
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
## 🚀 Features
|
|
@@ -27,7 +29,7 @@ npm install mysql2
|
|
|
27
29
|
```js
|
|
28
30
|
import AGS from "ags-sql-lib";
|
|
29
31
|
|
|
30
|
-
const sql = new AGS("root", "password", "localhost", "my_database");
|
|
32
|
+
const sql = new AGS.connect("root", "password", "localhost", "my_database");
|
|
31
33
|
```
|
|
32
34
|
### select
|
|
33
35
|
Select all columns
|
|
@@ -95,6 +97,27 @@ const result = await sql.selectInnerJoin(
|
|
|
95
97
|
"tblusers.id = tblorders.user_id"
|
|
96
98
|
);
|
|
97
99
|
```
|
|
100
|
+
### Aggregation queries
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
Calculate the total of all 'grade' values in one table
|
|
104
|
+
const totalGrades = await sql.sum("grade", "tbl_grades");
|
|
105
|
+
|
|
106
|
+
Get the lowest grade
|
|
107
|
+
const lowestGrade = await sql.min("grade", "tbl_grades");
|
|
108
|
+
|
|
109
|
+
Get the highest grade
|
|
110
|
+
const highestGrade = await sql.max("grade", "tbl_grades");
|
|
111
|
+
|
|
112
|
+
Get the average grade
|
|
113
|
+
const averageGrade = await sql.avg("grade", "tbl_grades");
|
|
114
|
+
|
|
115
|
+
multiple tables
|
|
116
|
+
const totalResults = await db.sum("amount", ["tbl_sales", "tbl_purchases"]);
|
|
117
|
+
const minResults = await db.min("score", ["tbl_quiz1", "tbl_quiz2"]);
|
|
118
|
+
const maxResults = await db.max("salary", ["tbl_teachers", "tbl_admin"]);
|
|
119
|
+
const avgResults = await db.avg("grade", ["tbl_midterm", "tbl_final"]);
|
|
120
|
+
```
|
|
98
121
|
### BONUS
|
|
99
122
|
```js
|
|
100
123
|
import { getDadJokes } from "ags-sql-lib";
|
package/connection.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import connection from "./database/dbConnection.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default class {
|
|
4
4
|
#sqlOperation;
|
|
5
5
|
|
|
6
6
|
constructor(user, password, host, database) {
|
|
@@ -18,12 +18,11 @@ module.exports = class {
|
|
|
18
18
|
}
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
21
|
const sql = `SELECT ${columnName || "*"} FROM ${tableName}`;
|
|
23
22
|
const [result] = await this.#sqlOperation.query(sql);
|
|
24
23
|
return result;
|
|
25
24
|
} catch (error) {
|
|
26
|
-
|
|
25
|
+
throw new Error(error.message);
|
|
27
26
|
}
|
|
28
27
|
}
|
|
29
28
|
|
|
@@ -33,23 +32,19 @@ module.exports = class {
|
|
|
33
32
|
if(Array.isArray(idValue)){
|
|
34
33
|
let result = [];
|
|
35
34
|
for(let i in idValue){
|
|
36
|
-
const sql = `SELECT ${columnName || "*"} FROM
|
|
37
|
-
|
|
38
|
-
} = ${idValue[i]}`;
|
|
39
|
-
const [selectResult] = await this.#sqlOperation.query(sql)
|
|
35
|
+
const sql = `SELECT ${columnName || "*"} FROM ?? WHERE ?? = ?`;
|
|
36
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue[i]]);
|
|
40
37
|
result.push(selectResult[0]);
|
|
41
38
|
}
|
|
42
39
|
return result;
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
const sql = `SELECT ${columnName || "*"} FROM
|
|
46
|
-
yourIdName
|
|
47
|
-
} = ${idValue}`;
|
|
48
|
-
const [selectResult] = await this.#sqlOperation.query(sql)
|
|
42
|
+
const sql = `SELECT ${columnName || "*"} FROM ?? WHERE ?? = ?`;
|
|
43
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue]);
|
|
49
44
|
return selectResult[0];
|
|
50
45
|
|
|
51
46
|
} catch (error) {
|
|
52
|
-
|
|
47
|
+
throw new Error(error.message);
|
|
53
48
|
}
|
|
54
49
|
}
|
|
55
50
|
|
|
@@ -68,7 +63,7 @@ module.exports = class {
|
|
|
68
63
|
const [insertResult] = await this.#sqlOperation.query(sql, [objectValue]);
|
|
69
64
|
return insertResult;
|
|
70
65
|
} catch (error) {
|
|
71
|
-
|
|
66
|
+
throw new Error(error.message);
|
|
72
67
|
}
|
|
73
68
|
}
|
|
74
69
|
|
|
@@ -78,17 +73,17 @@ module.exports = class {
|
|
|
78
73
|
if(Array.isArray(objectValue) && Array.isArray(idValue)){
|
|
79
74
|
let result = [];
|
|
80
75
|
for(let i in objectValue){
|
|
81
|
-
const sql = `UPDATE
|
|
82
|
-
const [updateResult] = await this.#sqlOperation.query(sql, [objectValue[i]]);
|
|
76
|
+
const sql = `UPDATE ?? SET ? WHERE ?? = ?`;
|
|
77
|
+
const [updateResult] = await this.#sqlOperation.query(sql, [tableName, objectValue[i], yourIdName, idValue[i]]);
|
|
83
78
|
result.push(updateResult);
|
|
84
79
|
}
|
|
85
80
|
return result;
|
|
86
81
|
}
|
|
87
|
-
const sql = `UPDATE
|
|
88
|
-
const [updateResult] = await this.#sqlOperation.query(sql, [objectValue]);
|
|
82
|
+
const sql = `UPDATE ?? SET ? WHERE ?? = ?`;
|
|
83
|
+
const [updateResult] = await this.#sqlOperation.query(sql, [tableName, objectValue, yourIdName, idValue]);
|
|
89
84
|
return updateResult;
|
|
90
85
|
} catch (error) {
|
|
91
|
-
|
|
86
|
+
throw new Error(error.message);
|
|
92
87
|
}
|
|
93
88
|
}
|
|
94
89
|
|
|
@@ -97,17 +92,17 @@ module.exports = class {
|
|
|
97
92
|
if(Array.isArray(idValue)){
|
|
98
93
|
let result = [];
|
|
99
94
|
for(let i in idValue){
|
|
100
|
-
const sql = `DELETE FROM
|
|
101
|
-
const [deleteResult] = await this.#sqlOperation.query(sql);
|
|
95
|
+
const sql = `DELETE FROM ?? WHERE ?? = ?`;
|
|
96
|
+
const [deleteResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue[i]]);
|
|
102
97
|
result.push(deleteResult);
|
|
103
98
|
}
|
|
104
99
|
return result;
|
|
105
100
|
}
|
|
106
|
-
const sql = `DELETE FROM
|
|
107
|
-
const [deleteResult] = await this.#sqlOperation.query(sql);
|
|
101
|
+
const sql = `DELETE FROM ?? WHERE ?? = ?`;
|
|
102
|
+
const [deleteResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue]);
|
|
108
103
|
return deleteResult;
|
|
109
104
|
} catch (error) {
|
|
110
|
-
|
|
105
|
+
throw new Error(error.message);
|
|
111
106
|
}
|
|
112
107
|
}
|
|
113
108
|
|
|
@@ -116,27 +111,103 @@ module.exports = class {
|
|
|
116
111
|
if(Array.isArray(idValue)){
|
|
117
112
|
let result = [];
|
|
118
113
|
for(let i in idValue){
|
|
119
|
-
const sql = `SELECT ${columnName || "*"} FROM
|
|
120
|
-
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
114
|
+
const sql = `SELECT ${columnName || "*"} FROM ?? ${joinType} ?? ON ${onCondition} WHERE ?? = ?`;
|
|
115
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tbl_a, tbl_b, yourIdName, idValue[i]]);
|
|
121
116
|
result.push(selectResult[0]);
|
|
122
117
|
}
|
|
123
118
|
return result;
|
|
124
119
|
}
|
|
125
|
-
const sql = `SELECT ${columnName || "*"} FROM
|
|
126
|
-
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
120
|
+
const sql = `SELECT ${columnName || "*"} FROM ?? ${joinType} ?? ON ${onCondition} WHERE ?? = ?`;
|
|
121
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tbl_a, tbl_b, yourIdName, idValue]);
|
|
127
122
|
return selectResult;
|
|
128
123
|
} catch (error) {
|
|
129
|
-
|
|
124
|
+
throw new Error(error.message);
|
|
130
125
|
}
|
|
131
126
|
}
|
|
132
127
|
|
|
133
128
|
async selectInnerJoin(tbl_a, tbl_b, columnName, onCondition){
|
|
134
129
|
try {
|
|
135
|
-
const sql = `SELECT ${columnName || "*"} FROM
|
|
136
|
-
const [result] = await this.#sqlOperation.query(sql);
|
|
130
|
+
const sql = `SELECT ${columnName || "*"} FROM ?? INNER JOIN ?? ON ${onCondition}`;
|
|
131
|
+
const [result] = await this.#sqlOperation.query(sql, [tbl_a, tbl_b]);
|
|
137
132
|
return result;
|
|
138
133
|
} catch (error) {
|
|
139
|
-
|
|
134
|
+
throw new Error(error.message);
|
|
140
135
|
}
|
|
141
136
|
}
|
|
137
|
+
|
|
138
|
+
async sum(columnName, tableName){
|
|
139
|
+
try {
|
|
140
|
+
if(Array.isArray(tableName)){
|
|
141
|
+
let result = [];
|
|
142
|
+
for(let i in tableName){
|
|
143
|
+
const sql = `SELECT SUM(${columnName}) FROM ??`;
|
|
144
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
|
|
145
|
+
result.push(selectResult);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
const sql = `SELECT SUM(${columnName}) FROM ??`;
|
|
150
|
+
const [result] = await this.#sqlOperation.query(sql, [tableName]);
|
|
151
|
+
return result;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
throw new Error(error.message);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async max(columnName, tableName){
|
|
158
|
+
try {
|
|
159
|
+
if(Array.isArray(tableName)){
|
|
160
|
+
let result = [];
|
|
161
|
+
for(let i in tableName){
|
|
162
|
+
const sql = `SELECT MAX(${columnName}) FROM ??`;
|
|
163
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
|
|
164
|
+
result.push(selectResult);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
const sql = `SELECT MAX(${columnName}) FROM ??`;
|
|
169
|
+
const [result] = await this.#sqlOperation.query(sql, [tableName]);
|
|
170
|
+
return result;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
throw new Error(error.message);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async min(columnName, tableName){
|
|
177
|
+
try {
|
|
178
|
+
if(Array.isArray(tableName)){
|
|
179
|
+
let result = [];
|
|
180
|
+
for(let i in tableName){
|
|
181
|
+
const sql = `SELECT MIN(${columnName}) FROM ??`;
|
|
182
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
|
|
183
|
+
result.push(selectResult);
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
const sql = `SELECT MIN(${columnName}) FROM ??`;
|
|
188
|
+
const [result] = await this.#sqlOperation.query(sql, [tableName]);
|
|
189
|
+
return result;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw new Error(error.message);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async avg(columnName, tableName){
|
|
196
|
+
try {
|
|
197
|
+
if(Array.isArray(tableName)){
|
|
198
|
+
let result = [];
|
|
199
|
+
for(let i in tableName){
|
|
200
|
+
const sql = `SELECT AVG(${columnName}) FROM ??`;
|
|
201
|
+
const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
|
|
202
|
+
result.push(selectResult);
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
const sql = `SELECT AVG(${columnName}) FROM ??`;
|
|
207
|
+
const [result] = await this.#sqlOperation.query(sql, [tableName]);
|
|
208
|
+
return result;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
throw new Error(error.message);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
142
213
|
};
|
package/database/dbConnection.js
CHANGED
package/getJokes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { exec } from "child_process";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default async () => {
|
|
4
4
|
return new Promise((resolve, reject) => {
|
|
5
5
|
try {
|
|
6
6
|
exec("curl https://icanhazdadjoke.com", (error, stdout, stderr) => {
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ags-sql-gen",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "lightweight Node.js MySQL helper library",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"type": "
|
|
6
|
+
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|