ags-sql-gen 1.0.2
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 +92 -0
- package/database/dbConnection.js +14 -0
- package/index.js +158 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# AGS SQL Library
|
|
2
|
+
|
|
3
|
+
AGS is a lightweight Node.js MySQL helper library that simplifies database operations such as **SELECT**, **INSERT**, **UPDATE**, **DELETE**, and **JOIN** queries.
|
|
4
|
+
It also includes a fun bonus function: `getDadJokes()` — because debugging is better with a laugh. 😄
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 🚀 Features
|
|
9
|
+
|
|
10
|
+
- Simple MySQL query wrapper using async/await
|
|
11
|
+
- Automatic handling for:
|
|
12
|
+
- Single or multiple table queries
|
|
13
|
+
- Batch insert, update, and delete
|
|
14
|
+
- Built-in JOIN methods (INNER, LEFT, RIGHT)
|
|
15
|
+
- Minimal setup required — just plug in your MySQL connection
|
|
16
|
+
- Fun extra: fetches a random dad joke via `curl`
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 📦 Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install ags-sql-lib mysql2
|
|
24
|
+
|
|
25
|
+
## Importing
|
|
26
|
+
import AGS from "ags-sql-lib";
|
|
27
|
+
|
|
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", [
|
|
48
|
+
{ name: "Alice", age: 22 },
|
|
49
|
+
{ name: "Bob", age: 30 }
|
|
50
|
+
]);
|
|
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
|
|
69
|
+
const result = await sql.selectLeftAndRightJoin(
|
|
70
|
+
"users",
|
|
71
|
+
"orders",
|
|
72
|
+
"users.name, orders.total",
|
|
73
|
+
"users.id = orders.user_id",
|
|
74
|
+
"users.id",
|
|
75
|
+
"LEFT JOIN",
|
|
76
|
+
1
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// INNER JOIN
|
|
80
|
+
const result = await sql.selectInnerJoin(
|
|
81
|
+
"users",
|
|
82
|
+
"orders",
|
|
83
|
+
"users.name, orders.total",
|
|
84
|
+
"users.id = orders.user_id"
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
## BONUS
|
|
88
|
+
|
|
89
|
+
import { getDadJokes } from "ags-sql-lib";
|
|
90
|
+
|
|
91
|
+
const joke = await getDadJokes();
|
|
92
|
+
console.log(joke);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import mysql from "mysql2";
|
|
2
|
+
|
|
3
|
+
export const db = (user, password, host, database) => {
|
|
4
|
+
const dbConnection = mysql
|
|
5
|
+
.createPool({
|
|
6
|
+
user: user,
|
|
7
|
+
password: password,
|
|
8
|
+
host: host,
|
|
9
|
+
database: database
|
|
10
|
+
})
|
|
11
|
+
.promise();
|
|
12
|
+
|
|
13
|
+
return dbConnection;
|
|
14
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import { db } from "./database/dbConnection.js";
|
|
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
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export default AGS;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ags-sql-gen",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "preset query",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Adrian294-AGS/AGS-SQL-Template.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"SQL"
|
|
16
|
+
],
|
|
17
|
+
"author": "Santiago, Adrian",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/Adrian294-AGS/AGS-SQL-Template/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/Adrian294-AGS/AGS-SQL-Template#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"child_process": "^1.0.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"mysql2": "^3.15.2"
|
|
28
|
+
}
|
|
29
|
+
}
|