db-crud-api 0.3.23 → 0.3.25

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
@@ -66,3 +66,8 @@
66
66
 
67
67
  Added:
68
68
  - sessionStore
69
+ ## v0.3.24 (2025-12-21)
70
+
71
+ Added:
72
+ - appLog patameter
73
+ - delById(id, Ope)
package/README.md CHANGED
@@ -1,100 +1,100 @@
1
- # db-crud-api 💡
2
- CRUD api for database tables.
3
-
4
- ## Installation
5
- npm install db-crud-api
6
-
7
- # Usage
8
- ```javascript
9
- import dbCrudApi from "db-crud-api";
10
- const apiFactory = dbCrudApi(mySchema);
11
-
12
- const mySchema = {
13
- servers: {
14
- server1: {
15
- realName: "server1.mydomain.com",
16
- type: "ms-sql",
17
- instance: "DEFAULT",
18
- user: "User1",
19
- password: "mypassword",
20
- options: {
21
- appName: "myAppName",
22
- ...
23
- },
24
- databases: {
25
- db1: {
26
- realName: "db_orders",
27
- tables: {
28
- table1: {
29
- realName: "tb_orders_head",
30
- idField: "Id",
31
- autoId: true,
32
- fields: {
33
- Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
34
- CustomerId: { type: "string", lengh: undefined, canBeNull: false, description: "Customer Id", defaultValue: undefined },
35
- ...
36
- }
37
- },
38
- table2: {
39
- realName: "tb_orders_detail",
40
- idField: "Id",
41
- autoId: true,
42
- fields: {
43
- Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
44
- OrderId: { type: "uuid", lengh: undefined, canBeNull: false, description: "Order Id", defaultValue: undefined },
45
- RowSeq: { type: "numeric", lengh: undefined, canBeNull: false, description: "Row sequence", defaultValue: undefined },
46
- ...
47
- }
48
- },
49
- ...
50
- }
51
- },
52
- ...
53
- }
54
- },
55
- ...
56
- }
57
- };
58
-
59
- const apiOrder = apiFactory.newROApi("server1.db1.table1"); // Read Only api
60
- const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
61
- const apiExecuteSP = apiFactory.newExecuteApi("db1.my_storeproc"); // execute my_storeproc
62
-
63
- console.log(await apiFactory.testConnection()); // optional
64
-
65
- console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
66
- console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
67
- console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
68
- console.log(await apiOrder.getByFilter()); // this get all rows
69
- console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
70
- console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
71
- console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
72
- console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
73
- console.log(await apiExecuteSP.execute()); // execute procedure with no argument
74
- console.log(await apiExecuteSP.execute({execute: {arguments: "@Company, @OrderNumber", params: {Company: "XXX", OrderNumber: "12345"}}})); // execute procedure with arguments and params
75
- ```
76
-
77
- License
78
- -------
79
-
80
- MIT License
81
-
82
- Copyright (c) 2022
83
-
84
- Permission is hereby granted, free of charge, to any person obtaining a copy
85
- of this software and associated documentation files (the "Software"), to deal
86
- in the Software without restriction, including without limitation the rights
87
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
- copies of the Software, and to permit persons to whom the Software is
89
- furnished to do so, subject to the following conditions:
90
-
91
- The above copyright notice and this permission notice shall be included in all
92
- copies or substantial portions of the Software.
93
-
94
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ # db-crud-api 💡
2
+ CRUD api for database tables.
3
+
4
+ ## Installation
5
+ npm install db-crud-api
6
+
7
+ # Usage
8
+ ```javascript
9
+ import dbCrudApi from "db-crud-api";
10
+ const apiFactory = dbCrudApi(mySchema);
11
+
12
+ const mySchema = {
13
+ servers: {
14
+ server1: {
15
+ realName: "server1.mydomain.com",
16
+ type: "ms-sql",
17
+ instance: "DEFAULT",
18
+ user: "User1",
19
+ password: "mypassword",
20
+ options: {
21
+ appName: "myAppName",
22
+ ...
23
+ },
24
+ databases: {
25
+ db1: {
26
+ realName: "db_orders",
27
+ tables: {
28
+ table1: {
29
+ realName: "tb_orders_head",
30
+ idField: "Id",
31
+ autoId: true,
32
+ fields: {
33
+ Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
34
+ CustomerId: { type: "string", lengh: undefined, canBeNull: false, description: "Customer Id", defaultValue: undefined },
35
+ ...
36
+ }
37
+ },
38
+ table2: {
39
+ realName: "tb_orders_detail",
40
+ idField: "Id",
41
+ autoId: true,
42
+ fields: {
43
+ Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
44
+ OrderId: { type: "uuid", lengh: undefined, canBeNull: false, description: "Order Id", defaultValue: undefined },
45
+ RowSeq: { type: "numeric", lengh: undefined, canBeNull: false, description: "Row sequence", defaultValue: undefined },
46
+ ...
47
+ }
48
+ },
49
+ ...
50
+ }
51
+ },
52
+ ...
53
+ }
54
+ },
55
+ ...
56
+ }
57
+ };
58
+
59
+ const apiOrder = apiFactory.newROApi("server1.db1.table1"); // Read Only api
60
+ const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
61
+ const apiExecuteSP = apiFactory.newExecuteApi("db1.my_storeproc"); // execute my_storeproc
62
+
63
+ console.log(await apiFactory.testConnection()); // optional
64
+
65
+ console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
66
+ console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
67
+ console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
68
+ console.log(await apiOrder.getByFilter()); // this get all rows
69
+ console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
70
+ console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
71
+ console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
72
+ console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
73
+ console.log(await apiExecuteSP.execute()); // execute procedure with no argument
74
+ console.log(await apiExecuteSP.execute({execute: {arguments: "@Company, @OrderNumber", params: {Company: "XXX", OrderNumber: "12345"}}})); // execute procedure with arguments and params
75
+ ```
76
+
77
+ License
78
+ -------
79
+
80
+ MIT License
81
+
82
+ Copyright (c) 2022
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a copy
85
+ of this software and associated documentation files (the "Software"), to deal
86
+ in the Software without restriction, including without limitation the rights
87
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
+ copies of the Software, and to permit persons to whom the Software is
89
+ furnished to do so, subject to the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be included in all
92
+ copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
100
100
  SOFTWARE.
package/lib/api-batch.js CHANGED
@@ -101,11 +101,11 @@ export default class apiBatch {
101
101
  }
102
102
 
103
103
  // Delete by id
104
- delById(tablePath, id) {
104
+ delById(tablePath, id, reqOpe) {
105
105
  const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
106
106
  const _connection = dbOpe.prepareConnection(_schema);
107
- this.#dbOperations.push(dbOpe.prepareDeleteById(_schema, _connection, id));
108
- this.#operations.push({});
107
+ this.#dbOperations.push(dbOpe.prepareDeleteById(_schema, _connection, reqOpe, id));
108
+ this.#operations.push(reqOpe);
109
109
  return;
110
110
  }
111
111
 
package/lib/api-full.js CHANGED
@@ -57,14 +57,21 @@ export default class apiFull {
57
57
 
58
58
  // Add or update item by Id
59
59
  async putById(reqOpe, id) {
60
- const _old = await this.getById(id, { get: { fields: [dbOpe.idField(this.#schema)] } });
60
+ const _getOpe = {
61
+ get: { fields: [dbOpe.idField(this.#schema)]},
62
+ appLog: reqOpe?.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
63
+ };
64
+ const _old = await this.getById(id, _getOpe);
61
65
  if (!_old) {
62
66
  const _dbOpePut = dbOpe.preparePutById(this.#schema, this.#connection, reqOpe, id);
63
67
  await dbOpe.runQuery(_dbOpePut);
64
68
  return _dbOpePut.put.sets;
65
69
  }
66
70
  else {
67
- const _patchOpe = { patch: {sets: reqOpe.put.sets} };
71
+ const _patchOpe = {
72
+ patch: {sets: reqOpe.put.sets},
73
+ appLog: reqOpe?.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
74
+ };
68
75
  await this.patchById(_patchOpe, id);
69
76
  return _patchOpe.patch.sets;
70
77
  }
@@ -79,8 +86,8 @@ export default class apiFull {
79
86
  }
80
87
 
81
88
  // Delete by id
82
- async delById(id) {
83
- const _dbOpe = dbOpe.prepareDeleteById(this.#schema, this.#connection, id);
89
+ async delById(id, reqOpe) {
90
+ const _dbOpe = dbOpe.prepareDeleteById(this.#schema, this.#connection, reqOpe, id);
84
91
  return await dbOpe.runQuery(_dbOpe);
85
92
  }
86
93
 
@@ -23,7 +23,7 @@ export default class sessionStore extends session.Store {
23
23
  get(sid, callback) {
24
24
 
25
25
  log(`Get session '${sid}' from store.`, 30);
26
- this.sessions.getById(sid)
26
+ this.sessions.getById(sid, { appLog: false })
27
27
  .then((data) => {
28
28
  if (data) {
29
29
  try {
@@ -54,8 +54,8 @@ export default class sessionStore extends session.Store {
54
54
  Expires: session.cookie.expires ? new Date(session.cookie.expires) : null
55
55
  };
56
56
 
57
- // Salva la sessione nel DB
58
- this.sessions.putById({ put: { sets: _session } }, sid)
57
+ // Salva la sessione nel DB (disabilito logging)
58
+ this.sessions.putById({ put: { sets: _session }, appLog: false }, sid)
59
59
  .then(() => {
60
60
  callback(null); // Successo
61
61
  })
@@ -71,7 +71,7 @@ export default class sessionStore extends session.Store {
71
71
  */
72
72
  destroy(sid, callback) {
73
73
  log(`Destroy session '${sid}' from store.`, 30);
74
- this.sessions.delById(sid)
74
+ this.sessions.delById(sid, { appLog: false })
75
75
  .then(() => {
76
76
  callback(null); // Successo
77
77
  })
@@ -86,7 +86,7 @@ export default class sessionStore extends session.Store {
86
86
  */
87
87
  clear(callback) {
88
88
  log(`Clear all sessions from store.`, 30);
89
- this.sessions.delByFilter({ delete: { filters: [] } })
89
+ this.sessions.delByFilter({ delete: { filters: [] }, appLog: false })
90
90
  .then(() => {
91
91
  callback(null); // Successo
92
92
  })
@@ -101,7 +101,7 @@ export default class sessionStore extends session.Store {
101
101
  */
102
102
  length(callback) {
103
103
  log(`Count all sessions from store.`, 30);
104
- this.sessions.getByFilter({ get: { fields: ['count(*) as NumOfRecords'], filters: [] } })
104
+ this.sessions.getByFilter({ get: { fields: ['count(*) as NumOfRecords'], filters: [] }, appLog: false })
105
105
  .then((_sessions) => {
106
106
  try {
107
107
  const _count = (_sessions && _sessions.length > 0 && _sessions[0].NumOfRecords) ? parseInt(_sessions[0].NumOfRecords) : 0;
@@ -122,7 +122,7 @@ export default class sessionStore extends session.Store {
122
122
  all(callback) {
123
123
  log(`Get all sessions from store.`, 30);
124
124
  const now = new Date();
125
- this.sessions.getByFilter({ get: { fields: ['*'] } })
125
+ this.sessions.getByFilter({ get: { fields: ['*'] }, appLog: false })
126
126
  .then((_sessions) => {
127
127
  try {
128
128
  let _result = [];
@@ -153,7 +153,7 @@ export default class sessionStore extends session.Store {
153
153
  const _sets = {
154
154
  Expires: session.cookie.expires ? new Date(session.cookie.expires) : null
155
155
  };
156
- this.sessions.patchById({ patch: { sets: _sets } }, sid)
156
+ this.sessions.patchById({ patch: { sets: _sets }, appLog: false }, sid)
157
157
  .then(() => {
158
158
  callback(null); // Successo
159
159
  })
@@ -106,19 +106,19 @@ export function prepareConnection(schema) {
106
106
  throw new TypeError('server type not supported');
107
107
  }
108
108
 
109
- export function closeConnection(connection) {
109
+ export async function closeConnection(connection) {
110
110
  // MSSQL
111
111
  if (connection.serverType === serverType.mssql) { return mssql.closeConnection(connection); }
112
112
  // MySQL
113
113
  if (connection.serverType === serverType.mysql) { return mysql.closeConnection(connection); }
114
114
  }
115
115
 
116
- export function closeAllConnections() {
116
+ export async function closeAllConnections() {
117
117
  // MSSQL
118
- mssql.closeAllConnections();
118
+ await mssql.closeAllConnections();
119
119
  // MySQL
120
- mysql.closeAllConnections();
121
-
120
+ await mysql.closeAllConnections();
121
+
122
122
  return;
123
123
  }
124
124
 
@@ -269,7 +269,8 @@ export function prepareGet(tSchema, connection, reqOpe) {
269
269
  orderBy: _ope?.get?.orderBy,
270
270
  params: _ope?.get?.params
271
271
  },
272
- connection: connection
272
+ connection: connection,
273
+ appLog: _ope?.hasOwnProperty('appLog') ? _ope.appLog : undefined
273
274
  });
274
275
  });
275
276
  if (_result.length > 1) return _result;
@@ -290,7 +291,8 @@ export function prepareGetById(tSchema, connection, reqOpe, idValue) {
290
291
  fields: reqOpe?.get?.fields ?? defautlFields,
291
292
  filters: _filters
292
293
  },
293
- connection: connection
294
+ connection: connection,
295
+ appLog: reqOpe?.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
294
296
  }
295
297
  };
296
298
 
@@ -309,7 +311,8 @@ export function preparePatch(tSchema, connection, reqOpe) {
309
311
  filters: _ope.patch.filters,
310
312
  params: _ope.patch.params
311
313
  },
312
- connection: connection
314
+ connection: connection,
315
+ appLog: _ope.hasOwnProperty('appLog') ? _ope.appLog : undefined
313
316
  });
314
317
  }
315
318
  else throw new Error('Request sintax error, missing "patch" property.');
@@ -333,7 +336,8 @@ export function preparePatchById(tSchema, connection, reqOpe, idValue) {
333
336
  sets: reqOpe.patch.sets,
334
337
  filters: _filters
335
338
  },
336
- connection: connection
339
+ connection: connection,
340
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
337
341
  }
338
342
  };
339
343
 
@@ -351,7 +355,8 @@ export function preparePut(tSchema, connection, reqOpe) {
351
355
  sets: _ope.put.sets,
352
356
  params: _ope.put.params
353
357
  },
354
- connection: connection
358
+ connection: connection,
359
+ appLog: _ope.hasOwnProperty('appLog') ? _ope.appLog : undefined
355
360
  });
356
361
  }
357
362
  else throw new Error('Request sintax error, missing "put" property.');
@@ -371,7 +376,8 @@ export function preparePutById(tSchema, connection, reqOpe, idValue) {
371
376
  sets: reqOpe.put.sets,
372
377
  params: reqOpe.put.params
373
378
  },
374
- connection: connection
379
+ connection: connection,
380
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
375
381
  }
376
382
  };
377
383
 
@@ -388,7 +394,8 @@ export function prepareDelete(tSchema, connection, reqOpe) {
388
394
  filters: _ope.delete.filters,
389
395
  params: _ope.delete.params
390
396
  },
391
- connection: connection
397
+ connection: connection,
398
+ appLog: _ope.hasOwnProperty('appLog') ? _ope.appLog : undefined
392
399
  });
393
400
  }
394
401
  else throw new Error('Request sintax error, missing "delete" property.');
@@ -398,13 +405,14 @@ export function prepareDelete(tSchema, connection, reqOpe) {
398
405
  return undefined;
399
406
  };
400
407
 
401
- export function prepareDeleteById(tSchema, connection, idValue) {
408
+ export function prepareDeleteById(tSchema, connection, reqOpe, idValue) {
402
409
  return {
403
410
  delete: {
404
411
  schema: tSchema,
405
412
  filters: [idField(tSchema) + ' = ' + toStringValue(idValue)]
406
413
  },
407
- connection: connection
414
+ connection: connection,
415
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
408
416
  }
409
417
  };
410
418
 
@@ -420,7 +428,8 @@ export function prepareExecute(pSchema, connection, reqOpe) {
420
428
  arguments: _ope.execute?.arguments,
421
429
  params: _ope.execute?.params
422
430
  },
423
- connection: connection
431
+ connection: connection,
432
+ appLog: _ope.hasOwnProperty('appLog') ? _ope.appLog : undefined
424
433
  });
425
434
  });
426
435
  if (_result.length > 1) return _result;
@@ -441,7 +450,8 @@ export function preparePassthrough(connection, reqOpe) {
441
450
  command: _ope.passthrough.command,
442
451
  params: _ope.passthrough.params
443
452
  },
444
- connection: connection
453
+ connection: connection,
454
+ appLog: _ope.hasOwnProperty('appLog') ? _ope.appLog : undefined
445
455
  });
446
456
  }
447
457
  else throw new Error('Request sintax error, missing "passthrough" property.');
@@ -454,20 +464,23 @@ export function preparePassthrough(connection, reqOpe) {
454
464
  export function prepareBegin(connection, reqOpe) {
455
465
  return {
456
466
  begin: {},
457
- connection: connection
467
+ connection: connection,
468
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
458
469
  }
459
470
  };
460
471
 
461
472
  export function prepareCommit(connection, reqOpe) {
462
473
  return {
463
474
  commit: {},
464
- connection: connection
475
+ connection: connection,
476
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
465
477
  }
466
478
  };
467
479
 
468
480
  export function prepareRollback(connection, reqOpe) {
469
481
  return {
470
482
  rollback: {},
471
- connection: connection
483
+ connection: connection,
484
+ appLog: reqOpe.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
472
485
  }
473
486
  };