db-crud-api 0.3.10 → 0.3.12

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 CHANGED
@@ -13,7 +13,7 @@ function stringifyValue(fieldName, value, tSchema) {
13
13
  if (value == undefined) { return 'null' }
14
14
  if (typeof value !== 'string' || value.trimStart().charAt(0) !== '[') {
15
15
  if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
16
- if (tSchema.table.fields[fieldName].type == 'datetime') {
16
+ if (tSchema.table.fields[fieldName].type == 'datetime') {
17
17
  if (typeof value == 'datetime' || typeof value == 'object') return `\'${value.toISOString()}\'`;
18
18
  if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
19
19
  return value;
@@ -35,7 +35,7 @@ function stringifyValue(fieldName, value, tSchema) {
35
35
  export function prepareConnection(tSchema) {
36
36
  return {
37
37
  id: `${tSchema.server.realName}-${tSchema.database.realName}-${tSchema.server.user}`,
38
- server: ((tSchema.server.instance && tSchema.server.instance != 'DEFAULT') ? (tSchema.server.realName + '\\' + tSchema.server.instance) : tSchema.server.realName) ?? 'localhost',
38
+ server: ((tSchema.server.instance && tSchema.server.instance != 'DEFAULT') ? (tSchema.server.realName + '\\' + tSchema.server.instance) : tSchema.server.realName) ?? 'localhost',
39
39
  port: tSchema.server.port,
40
40
  user: tSchema.server.user,
41
41
  password: tSchema.server.password,
@@ -64,12 +64,31 @@ export async function closeAllConnections() {
64
64
  if (!pools.hasOwnProperty(poolId)) continue;
65
65
  let pool = pools[poolId];
66
66
  if (pool) {
67
- await pool.close(); // wait pool is closed
68
- pool = undefined; // remove pool from pools
67
+ await pool.close(); // wait pool is closed
68
+ pool = undefined; // remove pool from pools
69
69
  }
70
70
  }
71
71
  }
72
72
 
73
+ // Test connection
74
+ export async function testConnection(connection) {
75
+
76
+ let _connection;
77
+ try {
78
+ _connection = await sql.connect(connection);
79
+ return true;
80
+ }
81
+ catch (error) {
82
+ throw (error);
83
+ }
84
+ finally {
85
+ if (_connection) {
86
+ await _connection.close();
87
+ }
88
+ }
89
+
90
+ }
91
+
73
92
  // Query
74
93
  export async function query(connection, dbOpes) {
75
94
 
@@ -77,8 +96,9 @@ export async function query(connection, dbOpes) {
77
96
  let pool = pools[connection.id]; // Try to get an existing pool
78
97
  if (!pool) {
79
98
  pool = new sql.ConnectionPool(connection); // Create new pool
80
- try { await pool.connect(); // wait that connection are made
81
- } catch(err) { throw(err); } // using original error
99
+ try {
100
+ await pool.connect(); // wait that connection are made
101
+ } catch (err) { throw (err); } // using original error
82
102
  pools[connection.id] = pool; // add new pool to pools
83
103
  }
84
104
 
@@ -95,16 +115,16 @@ export async function query(connection, dbOpes) {
95
115
  const sqlresult = await sqlRequest.query(sqlString);
96
116
 
97
117
  // Return a single object if there is only 1 row
98
- if (sqlresult.recordsets.length === 0) return;
99
- if (sqlresult.recordsets.length === 1) return sqlresult.recordsets[0];
100
- else return sqlresult.recordsets;
118
+ if (sqlresult.recordset.length === 0) return;
119
+ if (sqlresult.recordset.length === 1) return sqlresult.recordset[0];
120
+ else return sqlresult.recordset;
101
121
  }
102
122
 
103
123
  // Normalize SpecialName
104
124
  function normalizeSpecialName(name) {
105
125
  let _odd = false; // interpreted as 0
106
126
  return name.replaceAll('\`', (_m) => {
107
- _odd = !_odd;
127
+ _odd = !_odd;
108
128
  if (_odd) return '[';
109
129
  else return ']';
110
130
  });
@@ -163,12 +183,12 @@ function getToSql(dbOpe, sqlRequest) {
163
183
  let result = 'select'
164
184
 
165
185
  if (dbOpe.get.options && dbOpe.get.options.length > 0) {
166
- if (Array.isArray(dbOpe.get.options)) { for (const s of dbOpe.get.options) { result += ' ' + replaceLIMITToTOP(s); }}
186
+ if (Array.isArray(dbOpe.get.options)) { for (const s of dbOpe.get.options) { result += ' ' + replaceLIMITToTOP(s); } }
167
187
  else result += ' ' + replaceLIMITToTOP(dbOpe.get.options);
168
188
  }
169
189
 
170
190
  if (dbOpe.get.fields && dbOpe.get.fields.length > 0) {
171
- if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
191
+ if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
172
192
  else result += ' ' + dbOpe.get.fields;
173
193
  }
174
194
  else { throw new Error('fields is missing.'); }
@@ -197,7 +217,7 @@ function getToSql(dbOpe, sqlRequest) {
197
217
 
198
218
  if (dbOpe.get.params) addParams(dbOpe.get.params, sqlRequest);
199
219
 
200
- result += ';'
220
+ result += ';'
201
221
 
202
222
  return result;
203
223
  }
@@ -206,8 +226,8 @@ function getToSql(dbOpe, sqlRequest) {
206
226
  function patchToSql(dbOpe, sqlRequest) {
207
227
  let result = 'update ' + fullyQualifiedTableName(dbOpe.patch.schema);
208
228
 
209
- if (dbOpe.patch.sets) {
210
- result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', ');
229
+ if (dbOpe.patch.sets) {
230
+ result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', ');
211
231
  }
212
232
  else { throw new Error('sets is missing.'); }
213
233
 
@@ -218,7 +238,7 @@ function patchToSql(dbOpe, sqlRequest) {
218
238
 
219
239
  if (dbOpe.patch.params) addParams(dbOpe.patch.params, sqlRequest);
220
240
 
221
- result += ';'
241
+ result += ';'
222
242
 
223
243
  return result;
224
244
  }
@@ -241,7 +261,7 @@ function putToSql(dbOpe, sqlRequest) {
241
261
 
242
262
  if (dbOpe.put.params) addParams(dbOpe.put.params, sqlRequest);
243
263
 
244
- result += ';'
264
+ result += ';'
245
265
 
246
266
  return result;
247
267
  }
@@ -257,7 +277,7 @@ function deleteToSql(dbOpe, sqlRequest) {
257
277
 
258
278
  if (dbOpe.delete.params) addParams(dbOpe.delete.params, sqlRequest);
259
279
 
260
- result += ';'
280
+ result += ';'
261
281
 
262
282
  return result;
263
283
  }
@@ -274,7 +294,7 @@ function executeToSql(dbOpe, sqlRequest) {
274
294
 
275
295
  if (dbOpe.execute.params) addParams(dbOpe.execute.params, sqlRequest);
276
296
 
277
- result += ';'
297
+ result += ';'
278
298
 
279
299
  return result;
280
300
  }
package/lib/mysql.js CHANGED
@@ -71,6 +71,19 @@ export async function closeAllConnections() {
71
71
  }
72
72
  }
73
73
 
74
+ // Test connection
75
+ export async function testConnection(connection) {
76
+ try {
77
+ const _connection = await sql.createConnection(connection);
78
+ await _connection.end();
79
+ return true;
80
+ }
81
+ catch (error)
82
+ {
83
+ throw (error);
84
+ }
85
+ }
86
+
74
87
  // Query
75
88
  export async function query(connection, dbOpes) {
76
89
 
@@ -311,4 +324,4 @@ function addParams(params, sqlRequest) {
311
324
  }
312
325
  }
313
326
  return;
314
- }
327
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "db-crud-api",
3
- "version": "0.3.10",
3
+ "version": "0.3.12",
4
4
  "type": "module",
5
5
  "description": "CRUD api for database tables",
6
6
  "main": "index.js",
@@ -10,9 +10,9 @@
10
10
  "author": "FF",
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
- "mssql": "^11.0.1",
14
- "mysql2": "^3.12.0",
15
- "uuid": "^11.0.3"
13
+ "mssql": "^12.0.0",
14
+ "mysql2": "^3.15.3",
15
+ "uuid": "^11.1.0"
16
16
  },
17
17
  "keywords": [
18
18
  "db",