ags-sql-gen 1.0.3 → 1.0.4
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 +78 -3
- 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,7 +18,6 @@ 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;
|
|
@@ -139,4 +138,80 @@ module.exports = class {
|
|
|
139
138
|
console.log(error);
|
|
140
139
|
}
|
|
141
140
|
}
|
|
141
|
+
|
|
142
|
+
async sum(columnName, tableName){
|
|
143
|
+
try {
|
|
144
|
+
if(Array.isArray(tableName)){
|
|
145
|
+
let result = [];
|
|
146
|
+
for(let i in tableName){
|
|
147
|
+
const sql = `SELECT SUM(${columnName}) FROM ${tableName[i]}`;
|
|
148
|
+
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
149
|
+
result.push(selectResult);
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
const sql = `SELECT SUM(${columnName}) FROM ${tableName}`;
|
|
154
|
+
const [result] = await this.#sqlOperation.query(sql);
|
|
155
|
+
return result;
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.log(error);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async max(columnName, tableName){
|
|
162
|
+
try {
|
|
163
|
+
if(Array.isArray(tableName)){
|
|
164
|
+
let result = [];
|
|
165
|
+
for(let i in tableName){
|
|
166
|
+
const sql = `SELECT MAX(${columnName}) FROM ${tableName[i]}`;
|
|
167
|
+
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
168
|
+
result.push(selectResult);
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
const sql = `SELECT MAX(${columnName}) FROM ${tableName}`;
|
|
173
|
+
const [result] = await this.#sqlOperation.query(sql);
|
|
174
|
+
return result;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.log(error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async min(columnName, tableName){
|
|
181
|
+
try {
|
|
182
|
+
if(Array.isArray(tableName)){
|
|
183
|
+
let result = [];
|
|
184
|
+
for(let i in tableName){
|
|
185
|
+
const sql = `SELECT MIN(${columnName}) FROM ${tableName[i]}`;
|
|
186
|
+
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
187
|
+
result.push(selectResult);
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
const sql = `SELECT MIN(${columnName}) FROM ${tableName}`;
|
|
192
|
+
const [result] = await this.#sqlOperation.query(sql);
|
|
193
|
+
return result;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
console.log(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async avg(columnName, tableName){
|
|
200
|
+
try {
|
|
201
|
+
if(Array.isArray(tableName)){
|
|
202
|
+
let result = [];
|
|
203
|
+
for(let i in tableName){
|
|
204
|
+
const sql = `SELECT AVG(${columnName}) FROM ${tableName[i]}`;
|
|
205
|
+
const [selectResult] = await this.#sqlOperation.query(sql);
|
|
206
|
+
result.push(selectResult);
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
const sql = `SELECT AVG(${columnName}) FROM ${tableName}`;
|
|
211
|
+
const [result] = await this.#sqlOperation.query(sql);
|
|
212
|
+
return result;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
console.log(error);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
142
217
|
};
|
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.4",
|
|
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
|
},
|