db-crud-api 0.3.9 → 0.3.11

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/CHANGELOG.md CHANGED
@@ -45,3 +45,7 @@
45
45
 
46
46
  Bug fix:
47
47
  - autoId flag not onored
48
+ ## v0.3.11 (2025-10-25)
49
+
50
+ Added:
51
+ - testConnection()
package/README.md CHANGED
@@ -60,6 +60,8 @@
60
60
  const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
61
61
  const apiExecuteSP = apiFactory.newExecuteApi("db1.my_storeproc"); // execute my_storeproc
62
62
 
63
+ console.log(await apiFactory.testConnection()); // optional
64
+
63
65
  console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
64
66
  console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
65
67
  console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
@@ -24,4 +24,8 @@ export function newBatchApi(useTransaction) {
24
24
 
25
25
  export function closeAllDbConnections() {
26
26
  return dbOpe.closeAllConnections();
27
+ }
28
+
29
+ export function testDbConnection() {
30
+ return dbOpe.testConnection();
27
31
  }
@@ -76,6 +76,16 @@ export function closeAllConnections() {
76
76
  return;
77
77
  }
78
78
 
79
+ export function testConnection() {
80
+ const _connection = prepareConnection(prepareSchema("test", objectType.procedure));
81
+ // MSSQL
82
+ if (_connection.serverType === serverType.mssql) { return mssql.testConnection(_connection); }
83
+ // MySQL
84
+ if (_connection.serverType === serverType.mysql) { return mysql.testConnection(_connection); }
85
+ // server type not supported
86
+ throw new TypeError('server type not supported');
87
+ }
88
+
79
89
  export function toStringValue(value) {
80
90
  if (!value) return 'null';
81
91
  if (value.trimStart().charAt(0) === '\'') return value;
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
 
@@ -104,7 +124,7 @@ export async function query(connection, dbOpes) {
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
  });
@@ -162,42 +182,42 @@ function getToSql(dbOpe, sqlRequest) {
162
182
 
163
183
  let result = 'select'
164
184
 
165
- if ((dbOpe.get.options) && (Object.keys(dbOpe.get.options).length > 0)) {
166
- if (Array.isArray(dbOpe.get.options)) { for (const s of dbOpe.get.options) { result += ' ' + replaceLIMITToTOP(s); }}
185
+ if (dbOpe.get.options && dbOpe.get.options.length > 0) {
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
- if ((dbOpe.get.fields) && (Object.keys(dbOpe.get.fields).length > 0)) {
171
- if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
190
+ if (dbOpe.get.fields && dbOpe.get.fields.length > 0) {
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.'); }
175
195
 
176
196
  result += ' from ' + fullyQualifiedTableName(dbOpe.get.schema);
177
197
 
178
- if ((dbOpe.get.filters) && (Object.keys(dbOpe.get.filters).length > 0)) {
198
+ if (dbOpe.get.filters && dbOpe.get.filters.length > 0) {
179
199
  if (Array.isArray(dbOpe.get.filters)) result += ' where ' + dbOpe.get.filters.join(' ');
180
200
  else result += ' where ' + dbOpe.get.filters;
181
201
  }
182
202
 
183
- if ((dbOpe.get.groups) && (Object.keys(dbOpe.get.groups).length > 0)) {
203
+ if (dbOpe.get.groups && dbOpe.get.groups.length > 0) {
184
204
  if (Array.isArray(dbOpe.get.groups)) result += ' group by ' + dbOpe.get.groups.join(', ');
185
205
  else result += ' group by ' + dbOpe.get.groups;
186
206
  }
187
207
 
188
- if ((dbOpe.get.groupsFilters) && (Object.keys(dbOpe.get.groupsFilters).length > 0)) {
208
+ if (dbOpe.get.groupsFilters && dbOpe.get.groupsFilters.length > 0) {
189
209
  if (Array.isArray(dbOpe.get.groupsFilters)) result += ' having ' + dbOpe.get.groupsFilters.join(' ');
190
210
  else result += ' having ' + dbOpe.get.groupsFilters;
191
211
  }
192
212
 
193
- if ((dbOpe.get.orderBy) && (Object.keys(dbOpe.get.orderBy).length > 0)) {
213
+ if (dbOpe.get.orderBy && dbOpe.get.orderBy.length > 0) {
194
214
  if (Array.isArray(dbOpe.get.orderBy)) result += ' order by ' + dbOpe.get.orderBy.join(', ');
195
215
  else result += ' order by ' + dbOpe.get.orderBy;
196
216
  }
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,19 +226,19 @@ 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
 
214
- if ((dbOpe.patch.filters) && (Object.keys(dbOpe.patch.filters).length > 0)) {
234
+ if (dbOpe.patch.filters && dbOpe.patch.filters.length > 0) {
215
235
  if (Array.isArray(dbOpe.patch.filters)) result += ' where ' + dbOpe.patch.filters.join(' ');
216
236
  else result += ' where ' + dbOpe.patch.filters;
217
237
  }
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
  }
@@ -250,14 +270,14 @@ function putToSql(dbOpe, sqlRequest) {
250
270
  function deleteToSql(dbOpe, sqlRequest) {
251
271
  let result = 'delete from ' + fullyQualifiedTableName(dbOpe.delete.schema);
252
272
 
253
- if ((dbOpe.delete.filters) && (Object.keys(dbOpe.delete.filters).length > 0)) {
273
+ if (dbOpe.delete.filters && dbOpe.delete.filters.length > 0) {
254
274
  if (Array.isArray(dbOpe.delete.filters)) result += ' where ' + dbOpe.delete.filters.join(' ');
255
275
  else result += ' where ' + dbOpe.delete.filters;
256
276
  }
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
 
@@ -168,14 +181,14 @@ function getToSql(dbOpe, sqlRequest) {
168
181
  let result = 'select'
169
182
 
170
183
  // omit 'limit' or 'top'
171
- if ((dbOpe.get.options) && (Object.keys(dbOpe.get.options).length > 0)) {
184
+ if (dbOpe.get.options && dbOpe.get.options.length > 0) {
172
185
  if (Array.isArray(dbOpe.get.options)) {
173
186
  for (const s of dbOpe.get.options) { if (!containLIMIT(s) && !containTOP(s)) result += ' ' + s; }
174
187
  }
175
188
  else if (!containLIMIT(dbOpe.get.options) && !containTOP(dbOpe.get.options)) result += ' ' + dbOpe.get.options;
176
189
  }
177
190
 
178
- if ((dbOpe.get.fields) && (Object.keys(dbOpe.get.fields).length > 0)) {
191
+ if (dbOpe.get.fields && dbOpe.get.fields.length > 0) {
179
192
  if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
180
193
  else result += ' ' + dbOpe.get.fields;
181
194
  }
@@ -183,28 +196,28 @@ function getToSql(dbOpe, sqlRequest) {
183
196
 
184
197
  result += ' from ' + fullyQualifiedTableName(dbOpe.get.schema);
185
198
 
186
- if ((dbOpe.get.filters) && (Object.keys(dbOpe.get.filters).length > 0)) {
199
+ if (dbOpe.get.filters && dbOpe.get.filters.length > 0) {
187
200
  if (Array.isArray(dbOpe.get.filters)) result += ' where ' + dbOpe.get.filters.join(' ');
188
201
  else result += ' where ' + dbOpe.get.filters;
189
202
  }
190
203
 
191
- if ((dbOpe.get.groups) && (Object.keys(dbOpe.get.groups).length > 0)) {
204
+ if (dbOpe.get.groups && dbOpe.get.groups.length > 0) {
192
205
  if (Array.isArray(dbOpe.get.groups)) result += ' group by ' + dbOpe.get.groups.join(', ');
193
206
  else result += ' group by ' + dbOpe.get.groups;
194
207
  }
195
208
 
196
- if ((dbOpe.get.groupsFilters) && (Object.keys(dbOpe.get.groupsFilters).length > 0)) {
209
+ if (dbOpe.get.groupsFilters && dbOpe.get.groupsFilters.length > 0) {
197
210
  if (Array.isArray(dbOpe.get.groupsFilters)) result += ' having ' + dbOpe.get.groupsFilters.join(' ');
198
211
  else result += ' having ' + dbOpe.get.groupsFilters;
199
212
  }
200
213
 
201
- if ((dbOpe.get.orderBy) && (Object.keys(dbOpe.get.orderBy).length > 0)) {
214
+ if (dbOpe.get.orderBy && dbOpe.get.orderBy.length > 0) {
202
215
  if (Array.isArray(dbOpe.get.orderBy)) result += ' order by ' + dbOpe.get.orderBy.join(', ');
203
216
  else result += ' order by ' + dbOpe.get.orderBy;
204
217
  }
205
218
 
206
219
  // search if 'limit' or 'top'
207
- if ((dbOpe.get.options) && (Object.keys(dbOpe.get.options).length > 0)) {
220
+ if (dbOpe.get.options && dbOpe.get.options.length > 0) {
208
221
  if (Array.isArray(dbOpe.get.options)) {
209
222
  for (const s of dbOpe.get.options) { if (containLIMIT(s) || containTOP(s)) result += ' ' + replaceTOPToLIMIT(s); }
210
223
  }
@@ -225,7 +238,7 @@ function patchToSql(dbOpe, sqlRequest) {
225
238
  if (dbOpe.patch.sets) { result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', '); }
226
239
  else { throw new Error('sets is missing.'); }
227
240
 
228
- if ((dbOpe.patch.filters) && (Object.keys(dbOpe.patch.filters).length > 0)) {
241
+ if (dbOpe.patch.filters && dbOpe.patch.filters.length > 0) {
229
242
  if (Array.isArray(dbOpe.patch.filters)) result += ' where ' + dbOpe.patch.filters.join(' ');
230
243
  else result += ' where ' + dbOpe.patch.filters;
231
244
  }
@@ -264,7 +277,7 @@ function putToSql(dbOpe, sqlRequest) {
264
277
  function deleteToSql(dbOpe, sqlRequest) {
265
278
  let result = 'delete from ' + fullyQualifiedTableName(dbOpe.delete.schema);
266
279
 
267
- if ((dbOpe.delete.filters) && (Object.keys(dbOpe.delete.filters).length > 0)) {
280
+ if (dbOpe.delete.filters && dbOpe.delete.filters.length > 0) {
268
281
  if (Array.isArray(dbOpe.delete.filters)) result += ' where ' + dbOpe.delete.filters.join(' ');
269
282
  else result += ' where ' + dbOpe.delete.filters;
270
283
  }
@@ -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.9",
3
+ "version": "0.3.11",
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",
package/deploy DELETED
@@ -1,5 +0,0 @@
1
- ##### publish npm package/module
2
- ##### !!! pay attention to not publish files with secrets
3
- npm login
4
- npm publish
5
- npm unpublish db-crud-api@0.1.0