db-crud-api 0.3.30 → 0.3.31
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/README.md +99 -99
- package/lib/api-batch.js +138 -138
- package/lib/api-full.js +13 -15
- package/lib/api-session-store.js +1 -1
- package/lib/mssql.js +2 -4
- package/lib/mysql.js +2 -5
- package/lib/schema.js +13 -13
- package/package.json +1 -1
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
|
@@ -1,139 +1,139 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Import modules
|
|
4
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
-
import * as dbOpe from './db-operations.js';
|
|
6
|
-
|
|
7
|
-
export default class apiBatch {
|
|
8
|
-
|
|
9
|
-
#operations = [];
|
|
10
|
-
#dbOperations = [];
|
|
11
|
-
#useTransaction = false;
|
|
12
|
-
|
|
13
|
-
constructor(useTransaction) {
|
|
14
|
-
if (typeof(useTransaction) === "boolean") this.#useTransaction = useTransaction;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get useTransaction() { return this.#useTransaction; }
|
|
18
|
-
set useTransaction(useTransaction) { if (typeof(useTransaction) === "boolean") this.#useTransaction = useTransaction; }
|
|
19
|
-
|
|
20
|
-
// reset Batch
|
|
21
|
-
#resetBatch() {
|
|
22
|
-
this.#operations = [];
|
|
23
|
-
this.#dbOperations = [];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// run batch
|
|
27
|
-
async runBatch() {
|
|
28
|
-
|
|
29
|
-
if (this.#dbOperations.length == 0) { throw new Error("runBatch: there are not operations in this batch.") }
|
|
30
|
-
|
|
31
|
-
// prepare transaction
|
|
32
|
-
if (this.#useTransaction) {
|
|
33
|
-
const _reqOpe_begin = {begin: {}};
|
|
34
|
-
const _dbOpe_begin = dbOpe.prepareBegin(this.#dbOperations[0].connection, _reqOpe_begin);
|
|
35
|
-
this.#dbOperations.unshift(_dbOpe_begin);
|
|
36
|
-
this.#operations.unshift(_reqOpe_begin);
|
|
37
|
-
|
|
38
|
-
const _reqOpe_commit = {commit: {}};
|
|
39
|
-
const _dbOpe_commit = dbOpe.prepareCommit(this.#dbOperations[0].connection, _reqOpe_commit);
|
|
40
|
-
this.#dbOperations.push(_dbOpe_commit);
|
|
41
|
-
this.#operations.push(_reqOpe_commit);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// run
|
|
45
|
-
const _return = await dbOpe.runQuery(this.#dbOperations);
|
|
46
|
-
|
|
47
|
-
// reset
|
|
48
|
-
this.#resetBatch();
|
|
49
|
-
|
|
50
|
-
return _return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// execute
|
|
54
|
-
execute(procPath, reqOpe) {
|
|
55
|
-
const _schema = dbOpe.prepareSchema(procPath, dbOpe.objectType.procedure);
|
|
56
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
57
|
-
this.#dbOperations.push(dbOpe.prepareExecute(_schema, _connection, reqOpe));
|
|
58
|
-
this.#operations.push(reqOpe);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Get by id
|
|
63
|
-
// note that getById when executed in a batch, return an array instead of single object
|
|
64
|
-
getById(tablePath, id, reqOpe) {
|
|
65
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
66
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
67
|
-
this.#dbOperations.push(dbOpe.prepareGetById(_schema, _connection, reqOpe, id));
|
|
68
|
-
this.#operations.push(reqOpe);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Get by filter
|
|
73
|
-
getByFilter(tablePath, reqOpe) {
|
|
74
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
75
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
76
|
-
this.#dbOperations.push(dbOpe.prepareGet(_schema, _connection, reqOpe));
|
|
77
|
-
this.#operations.push(reqOpe);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Add
|
|
82
|
-
put(tablePath, reqOpe) {
|
|
83
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
84
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
85
|
-
const _dbOpe = dbOpe.preparePut(_schema, _connection, reqOpe);
|
|
86
|
-
if (_dbOpe.put.sets && _schema.autoId === true) // automatic Id
|
|
87
|
-
if (!Object.keys(_dbOpe.put.sets).find(key => key.toUpperCase() === (dbOpe.idField(_schema)).toUpperCase()))
|
|
88
|
-
_dbOpe.put.sets[dbOpe.idField(_schema)] = uuidv4(); // automatic Id via uuidv4
|
|
89
|
-
this.#dbOperations.push(_dbOpe);
|
|
90
|
-
this.#operations.push(reqOpe);
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Add by Id
|
|
95
|
-
putById(tablePath, reqOpe, id) {
|
|
96
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
97
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
98
|
-
this.#dbOperations.push(dbOpe.preparePutById(_schema, _connection, reqOpe, id));
|
|
99
|
-
this.#operations.push(reqOpe);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Delete by id
|
|
104
|
-
delById(tablePath, id, reqOpe) {
|
|
105
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
106
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
107
|
-
this.#dbOperations.push(dbOpe.prepareDeleteById(_schema, _connection, reqOpe, id));
|
|
108
|
-
this.#operations.push(reqOpe);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Delete by filters
|
|
113
|
-
delByFilter(tablePath, reqOpe) {
|
|
114
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
115
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
116
|
-
this.#dbOperations.push(dbOpe.prepareDelete(_schema, _connection, reqOpe));
|
|
117
|
-
this.#operations.push(reqOpe);
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Patch (update) by id
|
|
122
|
-
patchById(tablePath, reqOpe, id) {
|
|
123
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
124
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
125
|
-
this.#dbOperations.push(dbOpe.preparePatchById(_schema, _connection, reqOpe, id));
|
|
126
|
-
this.#operations.push(reqOpe);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Patch (update) by filters
|
|
131
|
-
patchByFilter(tablePath, reqOpe) {
|
|
132
|
-
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
133
|
-
const _connection = dbOpe.prepareConnection(_schema);
|
|
134
|
-
this.#dbOperations.push(dbOpe.preparePatch(_schema, _connection, reqOpe));
|
|
135
|
-
this.#operations.push(reqOpe);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Import modules
|
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
+
import * as dbOpe from './db-operations.js';
|
|
6
|
+
|
|
7
|
+
export default class apiBatch {
|
|
8
|
+
|
|
9
|
+
#operations = [];
|
|
10
|
+
#dbOperations = [];
|
|
11
|
+
#useTransaction = false;
|
|
12
|
+
|
|
13
|
+
constructor(useTransaction) {
|
|
14
|
+
if (typeof(useTransaction) === "boolean") this.#useTransaction = useTransaction;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get useTransaction() { return this.#useTransaction; }
|
|
18
|
+
set useTransaction(useTransaction) { if (typeof(useTransaction) === "boolean") this.#useTransaction = useTransaction; }
|
|
19
|
+
|
|
20
|
+
// reset Batch
|
|
21
|
+
#resetBatch() {
|
|
22
|
+
this.#operations = [];
|
|
23
|
+
this.#dbOperations = [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// run batch
|
|
27
|
+
async runBatch() {
|
|
28
|
+
|
|
29
|
+
if (this.#dbOperations.length == 0) { throw new Error("runBatch: there are not operations in this batch.") }
|
|
30
|
+
|
|
31
|
+
// prepare transaction
|
|
32
|
+
if (this.#useTransaction) {
|
|
33
|
+
const _reqOpe_begin = {begin: {}};
|
|
34
|
+
const _dbOpe_begin = dbOpe.prepareBegin(this.#dbOperations[0].connection, _reqOpe_begin);
|
|
35
|
+
this.#dbOperations.unshift(_dbOpe_begin);
|
|
36
|
+
this.#operations.unshift(_reqOpe_begin);
|
|
37
|
+
|
|
38
|
+
const _reqOpe_commit = {commit: {}};
|
|
39
|
+
const _dbOpe_commit = dbOpe.prepareCommit(this.#dbOperations[0].connection, _reqOpe_commit);
|
|
40
|
+
this.#dbOperations.push(_dbOpe_commit);
|
|
41
|
+
this.#operations.push(_reqOpe_commit);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// run
|
|
45
|
+
const _return = await dbOpe.runQuery(this.#dbOperations);
|
|
46
|
+
|
|
47
|
+
// reset
|
|
48
|
+
this.#resetBatch();
|
|
49
|
+
|
|
50
|
+
return _return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// execute
|
|
54
|
+
execute(procPath, reqOpe) {
|
|
55
|
+
const _schema = dbOpe.prepareSchema(procPath, dbOpe.objectType.procedure);
|
|
56
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
57
|
+
this.#dbOperations.push(dbOpe.prepareExecute(_schema, _connection, reqOpe));
|
|
58
|
+
this.#operations.push(reqOpe);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Get by id
|
|
63
|
+
// note that getById when executed in a batch, return an array instead of single object
|
|
64
|
+
getById(tablePath, id, reqOpe) {
|
|
65
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
66
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
67
|
+
this.#dbOperations.push(dbOpe.prepareGetById(_schema, _connection, reqOpe, id));
|
|
68
|
+
this.#operations.push(reqOpe);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Get by filter
|
|
73
|
+
getByFilter(tablePath, reqOpe) {
|
|
74
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
75
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
76
|
+
this.#dbOperations.push(dbOpe.prepareGet(_schema, _connection, reqOpe));
|
|
77
|
+
this.#operations.push(reqOpe);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Add
|
|
82
|
+
put(tablePath, reqOpe) {
|
|
83
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
84
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
85
|
+
const _dbOpe = dbOpe.preparePut(_schema, _connection, reqOpe);
|
|
86
|
+
if (_dbOpe.put.sets && _schema.autoId === true) // automatic Id
|
|
87
|
+
if (!Object.keys(_dbOpe.put.sets).find(key => key.toUpperCase() === (dbOpe.idField(_schema)).toUpperCase()))
|
|
88
|
+
_dbOpe.put.sets[dbOpe.idField(_schema)] = uuidv4(); // automatic Id via uuidv4
|
|
89
|
+
this.#dbOperations.push(_dbOpe);
|
|
90
|
+
this.#operations.push(reqOpe);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Add by Id
|
|
95
|
+
putById(tablePath, reqOpe, id) {
|
|
96
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
97
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
98
|
+
this.#dbOperations.push(dbOpe.preparePutById(_schema, _connection, reqOpe, id));
|
|
99
|
+
this.#operations.push(reqOpe);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Delete by id
|
|
104
|
+
delById(tablePath, id, reqOpe) {
|
|
105
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
106
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
107
|
+
this.#dbOperations.push(dbOpe.prepareDeleteById(_schema, _connection, reqOpe, id));
|
|
108
|
+
this.#operations.push(reqOpe);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Delete by filters
|
|
113
|
+
delByFilter(tablePath, reqOpe) {
|
|
114
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
115
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
116
|
+
this.#dbOperations.push(dbOpe.prepareDelete(_schema, _connection, reqOpe));
|
|
117
|
+
this.#operations.push(reqOpe);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Patch (update) by id
|
|
122
|
+
patchById(tablePath, reqOpe, id) {
|
|
123
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
124
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
125
|
+
this.#dbOperations.push(dbOpe.preparePatchById(_schema, _connection, reqOpe, id));
|
|
126
|
+
this.#operations.push(reqOpe);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Patch (update) by filters
|
|
131
|
+
patchByFilter(tablePath, reqOpe) {
|
|
132
|
+
const _schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
133
|
+
const _connection = dbOpe.prepareConnection(_schema);
|
|
134
|
+
this.#dbOperations.push(dbOpe.preparePatch(_schema, _connection, reqOpe));
|
|
135
|
+
this.#operations.push(reqOpe);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
139
|
}
|
package/lib/api-full.js
CHANGED
|
@@ -55,25 +55,23 @@ export default class apiFull {
|
|
|
55
55
|
return _dbOpe.put.sets;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
//
|
|
58
|
+
// Update or Add item by Id
|
|
59
59
|
async putById(reqOpe, id) {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
let _result = undefined;
|
|
61
|
+
// try to update
|
|
62
|
+
const _patchOpe = {
|
|
63
|
+
patch: {sets: reqOpe.put.sets},
|
|
62
64
|
appLog: reqOpe?.hasOwnProperty('appLog') ? reqOpe.appLog : undefined
|
|
63
65
|
};
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return _dbOpePut.put.sets;
|
|
66
|
+
_result = await this.patchById(_patchOpe, id);
|
|
67
|
+
if (typeof _result ==='number' && _result > 0) {
|
|
68
|
+
// return _patchOpe.patch.sets;
|
|
69
|
+
return _result;
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
};
|
|
75
|
-
await this.patchById(_patchOpe, id);
|
|
76
|
-
return _patchOpe.patch.sets;
|
|
71
|
+
// try to insert
|
|
72
|
+
else {
|
|
73
|
+
const _dbOpePut = dbOpe.preparePutById(this.#schema, this.#connection, reqOpe, id);
|
|
74
|
+
return await dbOpe.runQuery(_dbOpePut);
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
|
package/lib/api-session-store.js
CHANGED
|
@@ -54,7 +54,7 @@ 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 (disabilito logging)
|
|
57
|
+
// Salva la sessione nel DB (disabilito logging).
|
|
58
58
|
this.sessions.putById({ put: { sets: _session }, appLog: false }, sid)
|
|
59
59
|
.then(() => {
|
|
60
60
|
callback(null); // Successo
|
package/lib/mssql.js
CHANGED
|
@@ -38,9 +38,7 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
38
38
|
|
|
39
39
|
// if string or uuid
|
|
40
40
|
if (_fieldType == 'string' || _fieldType == 'uuid') {
|
|
41
|
-
|
|
42
|
-
return `\'${value.replace(/'/g, "''")}\'`;
|
|
43
|
-
}
|
|
41
|
+
return `\'${String(value).replace(/'/g, "''")}\'`;
|
|
44
42
|
}
|
|
45
43
|
|
|
46
44
|
// field not in schema
|
|
@@ -49,7 +47,7 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
49
47
|
return `\'${value.toISOString()}\'`;
|
|
50
48
|
if (typeof value == 'boolean')
|
|
51
49
|
return `\'${value}\'`;
|
|
52
|
-
if (typeof value == 'string'
|
|
50
|
+
if (typeof value == 'string')
|
|
53
51
|
return `\'${value.replace(/'/g, "''")}\'`;
|
|
54
52
|
}
|
|
55
53
|
|
package/lib/mysql.js
CHANGED
|
@@ -41,10 +41,7 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
41
41
|
|
|
42
42
|
// if string or uuid
|
|
43
43
|
if (_fieldType == 'string' || _fieldType == 'uuid') {
|
|
44
|
-
|
|
45
|
-
// return `\'${value}\'`;
|
|
46
|
-
return sql.escape(`${value}`);
|
|
47
|
-
}
|
|
44
|
+
return sql.escape(`${value}`);
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
// field not in schema
|
|
@@ -53,7 +50,7 @@ function stringifyValue(fieldName, value, tSchema) {
|
|
|
53
50
|
return `\'${value.toISOString().slice(0, -1)}\'`;
|
|
54
51
|
if (typeof value == 'boolean')
|
|
55
52
|
return `\'${value}\'`;
|
|
56
|
-
if (typeof value == 'string'
|
|
53
|
+
if (typeof value == 'string')
|
|
57
54
|
return sql.escape(`${value}`);
|
|
58
55
|
}
|
|
59
56
|
|
package/lib/schema.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
servers: {
|
|
3
|
-
},
|
|
4
|
-
config: {
|
|
5
|
-
log: {
|
|
6
|
-
level: 0,
|
|
7
|
-
callback: undefined,
|
|
8
|
-
maxAsyncInstance: 50
|
|
9
|
-
},
|
|
10
|
-
session: {
|
|
11
|
-
tablePath: 'sessions'
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
export default {
|
|
2
|
+
servers: {
|
|
3
|
+
},
|
|
4
|
+
config: {
|
|
5
|
+
log: {
|
|
6
|
+
level: 0,
|
|
7
|
+
callback: undefined,
|
|
8
|
+
maxAsyncInstance: 50
|
|
9
|
+
},
|
|
10
|
+
session: {
|
|
11
|
+
tablePath: 'sessions'
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
14
|
}
|