ags-sql-gen 1.0.2 → 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 CHANGED
@@ -1,13 +1,15 @@
1
+
1
2
  # AGS SQL Library
2
3
 
3
4
  AGS is a lightweight Node.js MySQL helper library that simplifies database operations such as **SELECT**, **INSERT**, **UPDATE**, **DELETE**, and **JOIN** queries.
4
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.
5
8
 
6
- ---
7
9
 
8
10
  ## 🚀 Features
9
11
 
10
- - Simple MySQL query wrapper using async/await
12
+ - Simple MySQL query wrapper using async/await
11
13
  - Automatic handling for:
12
14
  - Single or multiple table queries
13
15
  - Batch insert, update, and delete
@@ -15,78 +17,111 @@ It also includes a fun bonus function: `getDadJokes()` — because debugging is
15
17
  - Minimal setup required — just plug in your MySQL connection
16
18
  - Fun extra: fetches a random dad joke via `curl`
17
19
 
18
- ---
19
-
20
20
  ## 📦 Installation
21
21
 
22
22
  ```bash
23
- npm install ags-sql-lib mysql2
23
+ npm install ags-sql-lib
24
+ npm install mysql2
25
+ ```
26
+ ## Usage Sample
24
27
 
25
- ## Importing
28
+ ### Importing
29
+ ```js
26
30
  import AGS from "ags-sql-lib";
27
31
 
28
- const sql = new AGS("root", "password", "localhost", "my_database");
29
-
30
- // Select all columns
31
- const result = await sql.select("*", "users");
32
-
33
- // Select from multiple tables
34
- const result = await sql.select("*", ["users", "products"]);
35
-
36
- // Single
37
- const user = await sql.selectWithId("id", "*", "users", 1);
38
-
39
- // Multiple
40
- const users = await sql.selectWithId("id", "*", "users", [1, 2, 3]);
41
-
42
- ## InsertInto
43
- // Single insert
44
- await sql.insertInto("users", { name: "John", age: 25 });
45
-
46
- // Multiple inserts
47
- await sql.insertInto("users", [
32
+ const sql = new AGS.connect("root", "password", "localhost", "my_database");
33
+ ```
34
+ ### select
35
+ Select all columns
36
+ ```js
37
+ const result = await sql.select("*", "tblusers");
38
+
39
+ Select from multiple tables
40
+ const result = await sql.select("*", ["tblusers", "products"]);
41
+
42
+ Single
43
+ const user = await sql.selectWithId("id", "*", "tblusers", 1);
44
+
45
+ Multiple
46
+ const users = await sql.selectWithId("id", "*", "tblusers", [1, 2, 3]);
47
+ ```
48
+ ### InsertInto
49
+ Single insert
50
+ ```js
51
+ await sql.insertInto("tblusers", { name: "John", age: 25 });
52
+ ```
53
+ Multiple inserts
54
+ ```js
55
+ await sql.insertInto("tblusers", [
48
56
  { name: "Alice", age: 22 },
49
57
  { name: "Bob", age: 30 }
50
58
  ]);
51
-
52
- // Single update
53
- await sql.update("id", "users", { age: 26 }, 1);
54
-
55
- // Multiple updates
56
- await sql.update("id", "users", [{ age: 22 }, { age: 28 }], [1, 2]);
57
-
58
- ## DELETE
59
-
60
- // Single delete
61
- await sql.delete("users", "id", 1);
62
-
63
- // Multiple delete
64
- await sql.delete("users", "id", [2, 3]);
65
-
66
- ## Joining Table
67
-
68
- // LEFT JOIN example
59
+ ```
60
+ ### Update
61
+ Single update
62
+ ```js
63
+ await sql.update("id", "tblusers", { age: 26 }, 1);
64
+ ```
65
+ Multiple updates
66
+ ```js
67
+ await sql.update("id", "tblusers", [{ age: 22 }, { age: 28 }], [1, 2]);
68
+ ```
69
+ ### DELETE
70
+ Single delete
71
+ ```js
72
+ await sql.delete("tblusers", "id", 1);
73
+
74
+ Multiple delete
75
+ await sql.delete("tblusers", "id", [2, 3]);
76
+ ```
77
+ ### Joining Table
78
+
79
+ LEFT JOIN example
80
+ ```js
69
81
  const result = await sql.selectLeftAndRightJoin(
70
- "users",
71
- "orders",
72
- "users.name, orders.total",
73
- "users.id = orders.user_id",
74
- "users.id",
82
+ "tblusers",
83
+ "tblorders",
84
+ "tblusers.name, tblorders.total",
85
+ "tblusers.id = tblorders.user_id",
86
+ "tblusers.id",
75
87
  "LEFT JOIN",
76
88
  1
77
89
  );
78
-
79
- // INNER JOIN
90
+ ```
91
+ INNER JOIN
92
+ ```js
80
93
  const result = await sql.selectInnerJoin(
81
- "users",
82
- "orders",
83
- "users.name, orders.total",
84
- "users.id = orders.user_id"
94
+ "tblusers",
95
+ "tblorders",
96
+ "tblusers.name, tblorders.total",
97
+ "tblusers.id = tblorders.user_id"
85
98
  );
86
-
87
- ## BONUS
88
-
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
+ ```
121
+ ### BONUS
122
+ ```js
89
123
  import { getDadJokes } from "ags-sql-lib";
90
124
 
91
125
  const joke = await getDadJokes();
92
126
  console.log(joke);
127
+
package/connection.js ADDED
@@ -0,0 +1,217 @@
1
+ import connection from "./database/dbConnection.js";
2
+
3
+ export default class {
4
+ #sqlOperation;
5
+
6
+ constructor(user, password, host, database) {
7
+ this.#sqlOperation = connection(user, password, host, database);
8
+ }
9
+
10
+ async select(columnName, tableName) {
11
+ try {
12
+ if(Array.isArray(tableName)){
13
+ let result = [];
14
+ for(let i in tableName){
15
+ const sql = `SELECT * FROM ${tableName[i]}`;
16
+ const [selectResult] = await this.#sqlOperation.query(sql);
17
+ result.push(selectResult);
18
+ }
19
+ return result;
20
+ }
21
+ const sql = `SELECT ${columnName || "*"} FROM ${tableName}`;
22
+ const [result] = await this.#sqlOperation.query(sql);
23
+ return result;
24
+ } catch (error) {
25
+ console.log(error);
26
+ }
27
+ }
28
+
29
+ async selectWithId(yourIdName, columnName, tableName, idValue) {
30
+
31
+ try {
32
+ if(Array.isArray(idValue)){
33
+ let result = [];
34
+ for(let i in idValue){
35
+ const sql = `SELECT ${columnName || "*"} FROM ${tableName} WHERE ${
36
+ yourIdName || "Id"
37
+ } = ${idValue[i]}`;
38
+ const [selectResult] = await this.#sqlOperation.query(sql)
39
+ result.push(selectResult[0]);
40
+ }
41
+ return result;
42
+ }
43
+
44
+ const sql = `SELECT ${columnName || "*"} FROM ${tableName} WHERE ${
45
+ yourIdName || "Id"
46
+ } = ${idValue}`;
47
+ const [selectResult] = await this.#sqlOperation.query(sql)
48
+ return selectResult[0];
49
+
50
+ } catch (error) {
51
+ console.log(error);
52
+ }
53
+ }
54
+
55
+ async insertInto(tableName, objectValue) {
56
+ try {
57
+ if(Array.isArray(objectValue)){
58
+ let result = [];
59
+ for(let i in objectValue){
60
+ const sql = `INSERT INTO ${tableName} SET ?`;
61
+ const [insertResult] = await this.#sqlOperation.query(sql, [objectValue[i]]);
62
+ result.push(insertResult);
63
+ }
64
+ return result;
65
+ }
66
+ const sql = `INSERT INTO ${tableName} SET ?`;
67
+ const [insertResult] = await this.#sqlOperation.query(sql, [objectValue]);
68
+ return insertResult;
69
+ } catch (error) {
70
+ console.log(error);
71
+ }
72
+ }
73
+
74
+ async update(yourIdName, tableName, objectValue, idValue) {
75
+
76
+ try {
77
+ if(Array.isArray(objectValue) && Array.isArray(idValue)){
78
+ let result = [];
79
+ for(let i in objectValue){
80
+ const sql = `UPDATE ${tableName} SET ? WHERE ${yourIdName} = ${idValue[i]}`;
81
+ const [updateResult] = await this.#sqlOperation.query(sql, [objectValue[i]]);
82
+ result.push(updateResult);
83
+ }
84
+ return result;
85
+ }
86
+ const sql = `UPDATE ${tableName} SET ? WHERE ${yourIdName} = ${idValue}`;
87
+ const [updateResult] = await this.#sqlOperation.query(sql, [objectValue]);
88
+ return updateResult;
89
+ } catch (error) {
90
+ console.log(error);
91
+ }
92
+ }
93
+
94
+ async delete(tableName, yourIdName, idValue) {
95
+ try {
96
+ if(Array.isArray(idValue)){
97
+ let result = [];
98
+ for(let i in idValue){
99
+ const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue[i]}`;
100
+ const [deleteResult] = await this.#sqlOperation.query(sql);
101
+ result.push(deleteResult);
102
+ }
103
+ return result;
104
+ }
105
+ const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue}`;
106
+ const [deleteResult] = await this.#sqlOperation.query(sql);
107
+ return deleteResult;
108
+ } catch (error) {
109
+ console.log(error);
110
+ }
111
+ }
112
+
113
+ async selectLeftAndRightJoin(tbl_a, tbl_b, columnName, onCondition, yourIdName, joinType, idValue) {
114
+ try {
115
+ if(Array.isArray(idValue)){
116
+ let result = [];
117
+ for(let i in idValue){
118
+ const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} ${joinType} ${tbl_b} ON ${onCondition} WHERE ${yourIdName} = ${idValue[i]}`;
119
+ const [selectResult] = await this.#sqlOperation.query(sql);
120
+ result.push(selectResult[0]);
121
+ }
122
+ return result;
123
+ }
124
+ const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} ${joinType} ${tbl_b} ON ${onCondition} WHERE ${yourIdName} = ${idValue}`;
125
+ const [selectResult] = await this.#sqlOperation.query(sql);
126
+ return selectResult;
127
+ } catch (error) {
128
+ console.log(error);
129
+ }
130
+ }
131
+
132
+ async selectInnerJoin(tbl_a, tbl_b, columnName, onCondition){
133
+ try {
134
+ const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} INNER JOIN ${tbl_b} ON ${onCondition}`;
135
+ const [result] = await this.#sqlOperation.query(sql);
136
+ return result;
137
+ } catch (error) {
138
+ console.log(error);
139
+ }
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
+ }
217
+ };
@@ -1,6 +1,6 @@
1
1
  import mysql from "mysql2";
2
2
 
3
- export const db = (user, password, host, database) => {
3
+ export default (user, password, host, database) => {
4
4
  const dbConnection = mysql
5
5
  .createPool({
6
6
  user: user,
@@ -11,4 +11,4 @@ export const db = (user, password, host, database) => {
11
11
  .promise();
12
12
 
13
13
  return dbConnection;
14
- };
14
+ }
package/getJokes.js ADDED
@@ -0,0 +1,13 @@
1
+ import { exec } from "child_process";
2
+
3
+ export default async () => {
4
+ return new Promise((resolve, reject) => {
5
+ try {
6
+ exec("curl https://icanhazdadjoke.com", (error, stdout, stderr) => {
7
+ return resolve(stdout);
8
+ });
9
+ } catch (error) {
10
+ return reject(error);
11
+ }
12
+ });
13
+ };
package/index.js CHANGED
@@ -1,158 +1,7 @@
1
- import { exec } from "child_process";
2
- import { db } from "./database/dbConnection.js";
1
+ import connect from "./connection.js";
2
+ import getJokes from "./getJokes.js";
3
3
 
4
- class AGS {
5
-
6
- #sqlOperation;
7
-
8
- constructor(aUser, aPassword, aHost, aDatabase) {
9
- this.#sqlOperation = db(aUser, aPassword, aHost, aDatabase);
10
- }
11
-
12
- async select(columnName, tableName) {
13
- try {
14
- if(Array.isArray(tableName)){
15
- let result = [];
16
- for(let i in tableName){
17
- const sql = `SELECT * FROM ${tableName[i]}`;
18
- const [selectResult] = await this.#sqlOperation.query(sql);
19
- result.push(selectResult);
20
- }
21
- return result;
22
- }
23
-
24
- const sql = `SELECT ${columnName || "*"} FROM ${tableName}`;
25
- const [result] = await this.#sqlOperation.query(sql);
26
- return result;
27
- } catch (error) {
28
- console.log(error);
29
- }
30
- }
31
-
32
- async selectWithId(yourIdName, columnName, tableName, idValue) {
33
-
34
- try {
35
- if(Array.isArray(idValue)){
36
- let result = [];
37
- for(let i in idValue){
38
- const sql = `SELECT ${columnName || "*"} FROM ${tableName} WHERE ${
39
- yourIdName || "Id"
40
- } = ${idValue[i]}`;
41
- const [selectResult] = await this.#sqlOperation.query(sql)
42
- result.push(selectResult[0]);
43
- }
44
- return result;
45
- }
46
-
47
- const sql = `SELECT ${columnName || "*"} FROM ${tableName} WHERE ${
48
- yourIdName || "Id"
49
- } = ${idValue}`;
50
- const [selectResult] = await this.#sqlOperation.query(sql)
51
- return selectResult[0];
52
-
53
- } catch (error) {
54
- console.log(error);
55
- }
56
- }
57
-
58
- async insertInto(tableName, objectValue) {
59
- try {
60
- if(Array.isArray(objectValue)){
61
- let result = [];
62
- for(let i in objectValue){
63
- const sql = `INSERT INTO ${tableName} SET ?`;
64
- const [insertResult] = await this.#sqlOperation.query(sql, [objectValue[i]]);
65
- result.push(insertResult);
66
- }
67
- return result;
68
- }
69
- const sql = `INSERT INTO ${tableName} SET ?`;
70
- const [insertResult] = await this.#sqlOperation.query(sql, [objectValue]);
71
- return insertResult;
72
- } catch (error) {
73
- console.log(error);
74
- }
75
- }
76
-
77
- async update(yourIdName, tableName, objectValue, idValue) {
78
-
79
- try {
80
- if(Array.isArray(objectValue) && Array.isArray(idValue)){
81
- let result = [];
82
- for(let i in objectValue){
83
- const sql = `UPDATE ${tableName} SET ? WHERE ${yourIdName} = ${idValue[i]}`;
84
- const [updateResult] = await this.#sqlOperation.query(sql, [objectValue[i]]);
85
- result.push(updateResult);
86
- }
87
- return result;
88
- }
89
- const sql = `UPDATE ${tableName} SET ? WHERE ${yourIdName} = ${idValue}`;
90
- const [updateResult] = await this.#sqlOperation.query(sql, [objectValue]);
91
- return updateResult;
92
- } catch (error) {
93
- console.log(error);
94
- }
95
- }
96
-
97
- async delete(tableName, yourIdName, idValue) {
98
- try {
99
- if(Array.isArray(idValue)){
100
- let result = [];
101
- for(let i in idValue){
102
- const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue[i]}`;
103
- const [deleteResult] = await this.#sqlOperation.query(sql);
104
- result.push(deleteResult);
105
- }
106
- return result;
107
- }
108
- const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue}`;
109
- const [deleteResult] = await this.#sqlOperation.query(sql);
110
- return deleteResult;
111
- } catch (error) {
112
- console.log(error);
113
- }
114
- }
115
-
116
- async selectLeftAndRightJoin(tbl_a, tbl_b, columnName, onCondition, yourIdName, joinType, idValue) {
117
- try {
118
- if(Array.isArray(idValue)){
119
- let result = [];
120
- for(let i in idValue){
121
- const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} ${joinType} ${tbl_b} ON ${onCondition} WHERE ${yourIdName} = ${idValue[i]}`;
122
- const [selectResult] = await this.#sqlOperation.query(sql);
123
- result.push(selectResult[0]);
124
- }
125
- return result;
126
- }
127
- const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} ${joinType} ${tbl_b} ON ${onCondition} WHERE ${yourIdName} = ${idValue}`;
128
- const [selectResult] = await this.#sqlOperation.query(sql);
129
- return selectResult;
130
- } catch (error) {
131
- console.log(error);
132
- }
133
- }
134
-
135
- async selectInnerJoin(tbl_a, tbl_b, columnName, onCondition){
136
- try {
137
- const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} INNER JOIN ${tbl_b} ON ${onCondition}`;
138
- const [result] = await this.#sqlOperation.query(sql);
139
- return result;
140
- } catch (error) {
141
- console.log(error);
142
- }
143
- }
144
- }
145
-
146
- export const getDadJokes = async () => {
147
- return new Promise((resolve, reject) => {
148
- try {
149
- exec("curl https://icanhazdadjoke.com", (error, stdout, stderr) => {
150
- return resolve(stdout);
151
- });
152
- } catch (error) {
153
- return reject(error);
154
- }
155
- });
4
+ export default {
5
+ connect,
6
+ getJokes
156
7
  };
157
-
158
- export default AGS;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "ags-sql-gen",
3
- "version": "1.0.2",
4
- "description": "preset query",
3
+ "version": "1.0.4",
4
+ "description": "lightweight Node.js MySQL helper library",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
9
- "type": "module",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git+https://github.com/Adrian294-AGS/AGS-SQL-Template.git"