ags-sql-gen 1.0.4 → 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.
Files changed (2) hide show
  1. package/connection.js +45 -49
  2. package/package.json +1 -1
package/connection.js CHANGED
@@ -22,7 +22,7 @@ export default class {
22
22
  const [result] = await this.#sqlOperation.query(sql);
23
23
  return result;
24
24
  } catch (error) {
25
- console.log(error);
25
+ throw new Error(error.message);
26
26
  }
27
27
  }
28
28
 
@@ -32,23 +32,19 @@ export default class {
32
32
  if(Array.isArray(idValue)){
33
33
  let result = [];
34
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)
35
+ const sql = `SELECT ${columnName || "*"} FROM ?? WHERE ?? = ?`;
36
+ const [selectResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue[i]]);
39
37
  result.push(selectResult[0]);
40
38
  }
41
39
  return result;
42
40
  }
43
41
 
44
- const sql = `SELECT ${columnName || "*"} FROM ${tableName} WHERE ${
45
- yourIdName || "Id"
46
- } = ${idValue}`;
47
- 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]);
48
44
  return selectResult[0];
49
45
 
50
46
  } catch (error) {
51
- console.log(error);
47
+ throw new Error(error.message);
52
48
  }
53
49
  }
54
50
 
@@ -67,7 +63,7 @@ export default class {
67
63
  const [insertResult] = await this.#sqlOperation.query(sql, [objectValue]);
68
64
  return insertResult;
69
65
  } catch (error) {
70
- console.log(error);
66
+ throw new Error(error.message);
71
67
  }
72
68
  }
73
69
 
@@ -77,17 +73,17 @@ export default class {
77
73
  if(Array.isArray(objectValue) && Array.isArray(idValue)){
78
74
  let result = [];
79
75
  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]]);
76
+ const sql = `UPDATE ?? SET ? WHERE ?? = ?`;
77
+ const [updateResult] = await this.#sqlOperation.query(sql, [tableName, objectValue[i], yourIdName, idValue[i]]);
82
78
  result.push(updateResult);
83
79
  }
84
80
  return result;
85
81
  }
86
- const sql = `UPDATE ${tableName} SET ? WHERE ${yourIdName} = ${idValue}`;
87
- 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]);
88
84
  return updateResult;
89
85
  } catch (error) {
90
- console.log(error);
86
+ throw new Error(error.message);
91
87
  }
92
88
  }
93
89
 
@@ -96,17 +92,17 @@ export default class {
96
92
  if(Array.isArray(idValue)){
97
93
  let result = [];
98
94
  for(let i in idValue){
99
- const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue[i]}`;
100
- 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]]);
101
97
  result.push(deleteResult);
102
98
  }
103
99
  return result;
104
100
  }
105
- const sql = `DELETE FROM ${tableName} WHERE ${yourIdName || "Id"} = ${idValue}`;
106
- const [deleteResult] = await this.#sqlOperation.query(sql);
101
+ const sql = `DELETE FROM ?? WHERE ?? = ?`;
102
+ const [deleteResult] = await this.#sqlOperation.query(sql, [tableName, yourIdName, idValue]);
107
103
  return deleteResult;
108
104
  } catch (error) {
109
- console.log(error);
105
+ throw new Error(error.message);
110
106
  }
111
107
  }
112
108
 
@@ -115,27 +111,27 @@ export default class {
115
111
  if(Array.isArray(idValue)){
116
112
  let result = [];
117
113
  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);
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]]);
120
116
  result.push(selectResult[0]);
121
117
  }
122
118
  return result;
123
119
  }
124
- const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} ${joinType} ${tbl_b} ON ${onCondition} WHERE ${yourIdName} = ${idValue}`;
125
- 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]);
126
122
  return selectResult;
127
123
  } catch (error) {
128
- console.log(error);
124
+ throw new Error(error.message);
129
125
  }
130
126
  }
131
127
 
132
128
  async selectInnerJoin(tbl_a, tbl_b, columnName, onCondition){
133
129
  try {
134
- const sql = `SELECT ${columnName || "*"} FROM ${tbl_a} INNER JOIN ${tbl_b} ON ${onCondition}`;
135
- 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]);
136
132
  return result;
137
133
  } catch (error) {
138
- console.log(error);
134
+ throw new Error(error.message);
139
135
  }
140
136
  }
141
137
 
@@ -144,17 +140,17 @@ export default class {
144
140
  if(Array.isArray(tableName)){
145
141
  let result = [];
146
142
  for(let i in tableName){
147
- const sql = `SELECT SUM(${columnName}) FROM ${tableName[i]}`;
148
- const [selectResult] = await this.#sqlOperation.query(sql);
143
+ const sql = `SELECT SUM(${columnName}) FROM ??`;
144
+ const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
149
145
  result.push(selectResult);
150
146
  }
151
147
  return result;
152
148
  }
153
- const sql = `SELECT SUM(${columnName}) FROM ${tableName}`;
154
- const [result] = await this.#sqlOperation.query(sql);
149
+ const sql = `SELECT SUM(${columnName}) FROM ??`;
150
+ const [result] = await this.#sqlOperation.query(sql, [tableName]);
155
151
  return result;
156
152
  } catch (error) {
157
- console.log(error);
153
+ throw new Error(error.message);
158
154
  }
159
155
  }
160
156
 
@@ -163,17 +159,17 @@ export default class {
163
159
  if(Array.isArray(tableName)){
164
160
  let result = [];
165
161
  for(let i in tableName){
166
- const sql = `SELECT MAX(${columnName}) FROM ${tableName[i]}`;
167
- const [selectResult] = await this.#sqlOperation.query(sql);
162
+ const sql = `SELECT MAX(${columnName}) FROM ??`;
163
+ const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
168
164
  result.push(selectResult);
169
165
  }
170
166
  return result;
171
167
  }
172
- const sql = `SELECT MAX(${columnName}) FROM ${tableName}`;
173
- const [result] = await this.#sqlOperation.query(sql);
168
+ const sql = `SELECT MAX(${columnName}) FROM ??`;
169
+ const [result] = await this.#sqlOperation.query(sql, [tableName]);
174
170
  return result;
175
171
  } catch (error) {
176
- console.log(error);
172
+ throw new Error(error.message);
177
173
  }
178
174
  }
179
175
 
@@ -182,17 +178,17 @@ export default class {
182
178
  if(Array.isArray(tableName)){
183
179
  let result = [];
184
180
  for(let i in tableName){
185
- const sql = `SELECT MIN(${columnName}) FROM ${tableName[i]}`;
186
- const [selectResult] = await this.#sqlOperation.query(sql);
181
+ const sql = `SELECT MIN(${columnName}) FROM ??`;
182
+ const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
187
183
  result.push(selectResult);
188
184
  }
189
185
  return result;
190
186
  }
191
- const sql = `SELECT MIN(${columnName}) FROM ${tableName}`;
192
- const [result] = await this.#sqlOperation.query(sql);
187
+ const sql = `SELECT MIN(${columnName}) FROM ??`;
188
+ const [result] = await this.#sqlOperation.query(sql, [tableName]);
193
189
  return result;
194
190
  } catch (error) {
195
- console.log(error);
191
+ throw new Error(error.message);
196
192
  }
197
193
  }
198
194
 
@@ -201,17 +197,17 @@ export default class {
201
197
  if(Array.isArray(tableName)){
202
198
  let result = [];
203
199
  for(let i in tableName){
204
- const sql = `SELECT AVG(${columnName}) FROM ${tableName[i]}`;
205
- const [selectResult] = await this.#sqlOperation.query(sql);
200
+ const sql = `SELECT AVG(${columnName}) FROM ??`;
201
+ const [selectResult] = await this.#sqlOperation.query(sql, [tableName[i]]);
206
202
  result.push(selectResult);
207
203
  }
208
204
  return result;
209
205
  }
210
- const sql = `SELECT AVG(${columnName}) FROM ${tableName}`;
211
- const [result] = await this.#sqlOperation.query(sql);
206
+ const sql = `SELECT AVG(${columnName}) FROM ??`;
207
+ const [result] = await this.#sqlOperation.query(sql, [tableName]);
212
208
  return result;
213
209
  } catch (error) {
214
- console.log(error);
210
+ throw new Error(error.message);
215
211
  }
216
212
  }
217
213
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ags-sql-gen",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "lightweight Node.js MySQL helper library",
5
5
  "main": "index.js",
6
6
  "type": "module",