db-crud-api 0.3.26 → 0.3.27
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/lib/mssql.js +3 -1
- package/lib/mysql.js +30 -24
- package/package.json +1 -1
package/lib/mssql.js
CHANGED
|
@@ -124,7 +124,9 @@ export async function query(connection, dbOpes) {
|
|
|
124
124
|
pool = new sql.ConnectionPool(connection); // Create new pool
|
|
125
125
|
try {
|
|
126
126
|
await pool.connect(); // wait that connection are made
|
|
127
|
-
} catch (err) {
|
|
127
|
+
} catch (err) {
|
|
128
|
+
throw (err); // using original error
|
|
129
|
+
}
|
|
128
130
|
pools[connection.id] = pool; // add new pool to pools
|
|
129
131
|
}
|
|
130
132
|
|
package/lib/mysql.js
CHANGED
|
@@ -14,7 +14,7 @@ const pools = {};
|
|
|
14
14
|
function stringifyValue(fieldName, value, tSchema) {
|
|
15
15
|
|
|
16
16
|
// null or undefined
|
|
17
|
-
if (value == undefined)
|
|
17
|
+
if (value == undefined)
|
|
18
18
|
return 'null';
|
|
19
19
|
|
|
20
20
|
// detect field type
|
|
@@ -26,7 +26,7 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
26
26
|
// if datetime
|
|
27
27
|
if (_fieldType == 'datetime') {
|
|
28
28
|
// my-sql not accepts 'Z' at end of ISO string
|
|
29
|
-
if (value instanceof Date)
|
|
29
|
+
if (value instanceof Date)
|
|
30
30
|
return `\'${value.toISOString().slice(0, -1)}\'`;
|
|
31
31
|
if (typeof value == 'string') {
|
|
32
32
|
const valueDate = new Date(Date.parse(value));
|
|
@@ -34,9 +34,9 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
34
34
|
}
|
|
35
35
|
return value;
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
// if boolean
|
|
39
|
-
if (_fieldType == 'boolean' && typeof value == 'boolean')
|
|
39
|
+
if (_fieldType == 'boolean' && typeof value == 'boolean')
|
|
40
40
|
return `\'${value}\'`;
|
|
41
41
|
|
|
42
42
|
// if string or uuid
|
|
@@ -49,11 +49,11 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
49
49
|
|
|
50
50
|
// field not in schema
|
|
51
51
|
if (_fieldType == undefined) {
|
|
52
|
-
if (value instanceof Date)
|
|
52
|
+
if (value instanceof Date)
|
|
53
53
|
return `\'${value.toISOString().slice(0, -1)}\'`;
|
|
54
|
-
if (typeof value == 'boolean')
|
|
54
|
+
if (typeof value == 'boolean')
|
|
55
55
|
return `\'${value}\'`;
|
|
56
|
-
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"')
|
|
56
|
+
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"')
|
|
57
57
|
return sql.escape(`${value}`);
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -103,15 +103,20 @@ export async function closeAllConnections() {
|
|
|
103
103
|
|
|
104
104
|
// Test connection
|
|
105
105
|
export async function testConnection(connection) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
106
|
+
let _pool;
|
|
107
|
+
try {
|
|
108
|
+
// const _connection = await sql.createConnection(connection);
|
|
109
|
+
// await _connection.end();
|
|
110
|
+
_pool = new sql.createPool(connection); // Create new pool
|
|
111
|
+
await _pool.query('SELECT 1');
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
throw (error);
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
if (_pool) await _pool.end();
|
|
119
|
+
}
|
|
115
120
|
}
|
|
116
121
|
|
|
117
122
|
// Query
|
|
@@ -129,14 +134,14 @@ export async function query(connection, dbOpes) {
|
|
|
129
134
|
let appLog = true;
|
|
130
135
|
// if exists, input params will be added to request
|
|
131
136
|
if (Array.isArray(dbOpes)) {
|
|
132
|
-
dbOpes.forEach((dbOpe, index) => {
|
|
137
|
+
dbOpes.forEach((dbOpe, index) => {
|
|
133
138
|
sqlString += ToSql(dbOpe, pool);
|
|
134
|
-
if (dbOpe?.hasOwnProperty('appLog') && dbOpe.appLog === false) appLog = false;
|
|
139
|
+
if (dbOpe?.hasOwnProperty('appLog') && dbOpe.appLog === false) appLog = false;
|
|
135
140
|
});
|
|
136
141
|
}
|
|
137
142
|
else {
|
|
138
143
|
sqlString += ToSql(dbOpes, pool);
|
|
139
|
-
if (dbOpes?.hasOwnProperty('appLog') && dbOpes.appLog === false) appLog = false;
|
|
144
|
+
if (dbOpes?.hasOwnProperty('appLog') && dbOpes.appLog === false) appLog = false;
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
sqlString = normalizeSpecialName(sqlString);
|
|
@@ -149,13 +154,14 @@ export async function query(connection, dbOpes) {
|
|
|
149
154
|
|
|
150
155
|
// Run query
|
|
151
156
|
let sqlresult = undefined;
|
|
152
|
-
let sqlconn = undefined;
|
|
157
|
+
//let sqlconn = undefined;
|
|
153
158
|
try {
|
|
154
|
-
sqlconn = await pool.getConnection();
|
|
155
|
-
sqlresult = await sqlconn.query(sqlString);
|
|
159
|
+
// sqlconn = await pool.getConnection();
|
|
160
|
+
// sqlresult = await sqlconn.query(sqlString);
|
|
161
|
+
sqlresult = await pool.query(sqlString);
|
|
156
162
|
}
|
|
157
163
|
catch (err) { throw (err); } // using original error
|
|
158
|
-
finally { if (sqlconn) sqlconn.release(); }
|
|
164
|
+
// finally { if (sqlconn) sqlconn.release(); }
|
|
159
165
|
|
|
160
166
|
// Log
|
|
161
167
|
if (appLog) {
|
|
@@ -182,7 +188,7 @@ function normalizeSpecialName(sql) {
|
|
|
182
188
|
// Gestione delle virgolette singole '
|
|
183
189
|
if (char === "'" && !inDoubleQuote) {
|
|
184
190
|
inSingleQuote = !inSingleQuote;
|
|
185
|
-
}
|
|
191
|
+
}
|
|
186
192
|
// Gestione delle virgolette doppie "
|
|
187
193
|
else if (char === '"' && !inSingleQuote) {
|
|
188
194
|
inDoubleQuote = !inDoubleQuote;
|