db-crud-api 0.3.10 → 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 +4 -0
- package/README.md +2 -0
- package/lib/api-factory.js +4 -0
- package/lib/db-operations.js +10 -0
- package/lib/mssql.js +36 -16
- package/lib/mysql.js +14 -1
- package/package.json +4 -4
- package/deploy +0 -5
package/CHANGELOG.md
CHANGED
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
|
package/lib/api-factory.js
CHANGED
package/lib/db-operations.js
CHANGED
|
@@ -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)
|
|
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
|
-
|
|
68
|
-
|
|
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 {
|
|
81
|
-
|
|
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
|
});
|
|
@@ -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 += ' '
|
|
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.
|
|
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": "^
|
|
14
|
-
"mysql2": "^3.
|
|
15
|
-
"uuid": "^11.0
|
|
13
|
+
"mssql": "^12.0.0",
|
|
14
|
+
"mysql2": "^3.15.3",
|
|
15
|
+
"uuid": "^11.1.0"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
|
18
18
|
"db",
|