db-crud-api 0.3.14 → 0.3.16
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 +6 -1
- package/index.js +18 -11
- package/lib/api-factory.js +19 -0
- package/lib/log.js +21 -0
- package/lib/mssql.js +9 -2
- package/lib/mysql.js +10 -3
- package/lib/schema.js +10 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import schema from "./lib/schema.js";
|
|
4
|
-
import * as apiFactory from "./lib/api-factory.js";
|
|
5
|
-
|
|
6
|
-
export default function(customSchema) {
|
|
7
|
-
if (customSchema && customSchema.servers) {
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import schema from "./lib/schema.js";
|
|
4
|
+
import * as apiFactory from "./lib/api-factory.js";
|
|
5
|
+
|
|
6
|
+
export default function(customSchema, config) {
|
|
7
|
+
if (customSchema && customSchema.servers) {
|
|
8
|
+
apiFactory.setSchemaServers(customSchema.servers);
|
|
9
|
+
}
|
|
10
|
+
if (config) {
|
|
11
|
+
if (config.log) {
|
|
12
|
+
if (config.log.level) { apiFactory.setLogLevel(config.log.level) }
|
|
13
|
+
if (config.log.callback) { apiFactory.setLogCallback(config.log.callback) }
|
|
14
|
+
if (config.log.maxAsyncInstance) { apiFactory.setLogMaxAsyncInstances(config.log.maxAsyncInstance) }
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return apiFactory;
|
|
18
|
+
}
|
package/lib/api-factory.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
import schema from "./schema.js";
|
|
3
4
|
import * as dbOpe from "./db-operations.js"
|
|
4
5
|
import apiFull from "./api-full.js"
|
|
5
6
|
import apiRO from "./api-ro.js"
|
|
@@ -28,4 +29,22 @@ export function closeAllDbConnections() {
|
|
|
28
29
|
|
|
29
30
|
export function testDbConnection() {
|
|
30
31
|
return dbOpe.testConnection();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function setLogLevel(level) {
|
|
35
|
+
if (typeof level === 'number') { schema.config.log.level = level }
|
|
36
|
+
else if (typeof level === 'string' && Number.parseInt(level) != NaN) { schema.config.log.level = Number.parseInt(level) }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function setLogCallback(callback) {
|
|
40
|
+
if (typeof callback === 'function') { schema.config.log.callback = callback }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function setLogMaxAsyncInstances(maxAsyncInstance) {
|
|
44
|
+
if (typeof maxAsyncInstance === 'number') { schema.config.log.maxAsyncInstance = maxAsyncInstance }
|
|
45
|
+
else if (typeof maxAsyncInstance === 'string' && Number.parseInt(maxAsyncInstance) != NaN) { schema.config.log.maxAsyncInstance = Number.parseInt(maxAsyncInstance) }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function setSchemaServers(servers) {
|
|
49
|
+
schema.servers = servers;
|
|
31
50
|
}
|
package/lib/log.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import schema from './schema.js';
|
|
4
|
+
let numberOfAsyncInstances = 0;
|
|
5
|
+
|
|
6
|
+
export default function log(msg, level = 1) {
|
|
7
|
+
if (schema.config.log.level >= level && schema.config.log.callback) {
|
|
8
|
+
try {
|
|
9
|
+
const _param = {level: level, text: msg};
|
|
10
|
+
const _result = schema.config.log.callback(_param);
|
|
11
|
+
if (_result && typeof _result.then === 'function') {
|
|
12
|
+
// È una Promise (o un oggetto thenable)
|
|
13
|
+
if (numberOfAsyncInstances <= schema.config.log.maxAsyncInstance) {
|
|
14
|
+
numberOfAsyncInstances++;
|
|
15
|
+
_result.then((_param) => {}).catch(error => {}).finally(() => {numberOfAsyncInstances--;});
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch {}
|
|
20
|
+
}
|
|
21
|
+
};
|
package/lib/mssql.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
import sql from 'mssql';
|
|
4
|
+
import log from './log.js'
|
|
4
5
|
|
|
5
6
|
// Regex
|
|
6
7
|
const _match_LIMIT_n = /\bLIMIT\b +\d+/ig
|
|
@@ -111,14 +112,20 @@ export async function query(connection, dbOpes) {
|
|
|
111
112
|
|
|
112
113
|
sqlString = normalizeSpecialName(sqlString);
|
|
113
114
|
|
|
115
|
+
// Log
|
|
116
|
+
log(sqlString, 50);
|
|
117
|
+
log(JSON.stringify(dbOpes), 60);
|
|
118
|
+
|
|
114
119
|
// Run query
|
|
115
120
|
const sqlresult = await sqlRequest.query(sqlString);
|
|
116
121
|
|
|
122
|
+
// Log
|
|
123
|
+
log(`Query result: ${sqlresult.recordset ? sqlresult.recordset.length : 0} rows.`, 50);
|
|
124
|
+
|
|
117
125
|
// normalize return object
|
|
118
126
|
if (sqlresult.recordset == undefined) return;
|
|
119
127
|
if (sqlresult.recordset.length === 0) return;
|
|
120
|
-
|
|
121
|
-
// else
|
|
128
|
+
|
|
122
129
|
return sqlresult.recordset;
|
|
123
130
|
}
|
|
124
131
|
|
package/lib/mysql.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
import sql from 'mysql2/promise';
|
|
4
|
+
import log from './log.js'
|
|
4
5
|
|
|
5
6
|
// Regex
|
|
6
7
|
const _match_LIMIT_n = /\bLIMIT\b +\d+/ig
|
|
@@ -102,6 +103,10 @@ export async function query(connection, dbOpes) {
|
|
|
102
103
|
|
|
103
104
|
sqlString = normalizeSpecialName(sqlString);
|
|
104
105
|
|
|
106
|
+
// Log
|
|
107
|
+
log(JSON.stringify(dbOpes), 60);
|
|
108
|
+
log(sqlString, 50);
|
|
109
|
+
|
|
105
110
|
// Run query
|
|
106
111
|
let sqlresult = undefined;
|
|
107
112
|
let sqlconn = undefined;
|
|
@@ -112,12 +117,14 @@ export async function query(connection, dbOpes) {
|
|
|
112
117
|
catch (err) { throw (err); } // using original error
|
|
113
118
|
finally { if (sqlconn) sqlconn.release(); }
|
|
114
119
|
|
|
115
|
-
//
|
|
120
|
+
// Log
|
|
121
|
+
log(`Query result: ${(sqlresult && sqlresult.length > 0) ? sqlresult[0].length : 0} rows.`, 50);
|
|
122
|
+
|
|
123
|
+
// normalize return object
|
|
116
124
|
if (sqlresult == undefined) return;
|
|
117
125
|
if (sqlresult.length === 0) return;
|
|
118
126
|
if (sqlresult[0].length === 0) return;
|
|
119
|
-
|
|
120
|
-
// else
|
|
127
|
+
|
|
121
128
|
return sqlresult[0];
|
|
122
129
|
}
|
|
123
130
|
|
package/lib/schema.js
CHANGED