db-crud-api 0.3.16 → 0.3.19

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.
@@ -1,427 +1,473 @@
1
- 'use strict';
2
-
3
- // import modules
4
- import schema from './schema.js'
5
- import * as mssql from './mssql.js';
6
- import * as mysql from './mysql.js';
7
-
8
- // Common
9
- const defautlFields = ['*'];
10
- export const serverType = Object.freeze({ mssql: 'ms-sql', mysql: 'my-sql', mongodb: 'mongo-db' });
11
- export const objectType = Object.freeze({ table: 'T', procedure: 'P' });
12
-
13
- // Exported functions
14
- export function idField(tSchema) {
15
- return tSchema.table?.idField ?? 'id';
16
- }
17
-
18
- export function autoId(tSchema) {
19
- return tSchema.table?.autoId;
20
- }
21
-
22
- export async function runQuery(dbOpes) {
23
-
24
- // all connections must have same id
25
- const _connection = Array.isArray(dbOpes) ? dbOpes[0].connection : dbOpes.connection;
26
- if (Array.isArray(dbOpes)) {
27
- for (let i = 1; i < dbOpes.length; i++) {
28
- if (dbOpes[i].connection?.id !== _connection.id) throw new TypeError('runQuery: for multiple operations, connection must be the same');
29
- }
30
- }
31
-
32
- // MSSQL
33
- if (_connection.serverType === serverType.mssql) {
34
- return await mssql.query(_connection, dbOpes);
35
- }
36
-
37
- // MySQL
38
- else if (_connection.serverType === serverType.mysql) {
39
- return await mysql.query(_connection, dbOpes);
40
- }
41
-
42
- // server type not supported
43
- else throw new TypeError('server type not supported');
44
-
45
- }
46
-
47
- export function prepareConnection(schema) {
48
- // MySQL
49
- if (schema.server.type === serverType.mysql) {
50
- const _return = mysql.prepareConnection(schema);
51
- _return.serverType = serverType.mysql; // append 'serverType' to connection
52
- return _return;
53
- }
54
- // MSSQL is also the default
55
- if (!schema.server.type || schema.server.type === serverType.mssql) {
56
- const _return = mssql.prepareConnection(schema);
57
- _return.serverType = serverType.mssql; // append 'serverType' to connection
58
- return _return;
59
- }
60
- throw new TypeError('server type not supported');
61
- }
62
-
63
- export function closeConnection(connection) {
64
- // MSSQL
65
- if (connection.serverType === serverType.mssql) { return mssql.closeConnection(connection); }
66
- // MySQL
67
- if (connection.serverType === serverType.mysql) { return mysql.closeConnection(connection); }
68
- }
69
-
70
- export function closeAllConnections() {
71
- // MSSQL
72
- mssql.closeAllConnections();
73
- // MySQL
74
- mysql.closeAllConnections();
75
-
76
- return;
77
- }
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
-
89
- export function toStringValue(value) {
90
- if (!value) return 'null';
91
- if (value.trimStart().charAt(0) === '\'') return value;
92
- return `\'${value}\'`;
93
- }
94
-
95
- export function prepareSchema(obj, objType) {
96
- // objType = 'objectType.' (Table or StoreProcedure)
97
- // obj = 'systemName.dbName.objName' or 'dbName.objName' or 'objName' or 'systemNme..objName'
98
-
99
- if (obj == undefined || typeof obj !== 'string') throw new TypeError('prepareSchema: wrong obj');
100
- if (objType !== undefined && objType !== objectType.table && objType !== objectType.procedure) throw new TypeError(`prepareSchema: wrong objType, must be ${objectType.join(', ')}`);
101
- const objPath = obj.trimStart().split(' ')[0];
102
-
103
- // split objPath to serverName,dbName,objName
104
- let serverName = undefined;
105
- let dbName = undefined;
106
- let objName = undefined;
107
- const objPathArray = objPath.split('.');
108
- if (objPathArray.length > 2) {
109
- serverName = objPathArray[0]
110
- dbName = objPathArray[1]
111
- objName = objPathArray[2]
112
- }
113
- else if (objPathArray.length > 1) {
114
- dbName = objPathArray[0]
115
- objName = objPathArray[1]
116
- }
117
- else if (objPathArray.length > 0) {
118
- objName = objPathArray[0]
119
- }
120
-
121
- // server
122
- if (!serverName || serverName.length == 0) {
123
- if (!schema.servers || !Object.keys(schema.servers)[0]) throw new TypeError('missing default server in dbSchema');
124
- serverName = Object.keys(schema.servers)[0];
125
- }
126
- else { // add server to schema
127
- if (!schema.servers) { schema.servers = {} };
128
- if (!schema.servers[serverName]) { schema.servers[serverName] = {} };
129
- }
130
- if (!schema.servers[serverName].realName) { schema.servers[serverName].realName = serverName } // add realName
131
-
132
- // database
133
- if (!dbName || dbName.length == 0) {
134
- if (!schema.servers[serverName].databases || !Object.keys(schema.servers[serverName].databases)[0]) throw new TypeError('missing default database in dbSchema');
135
- dbName = Object.keys(schema.servers[serverName].databases)[0];
136
- }
137
- else { // add db to schema
138
- if (!schema.servers[serverName].databases) { schema.servers[serverName].databases = {} };
139
- if (!schema.servers[serverName].databases[dbName]) { schema.servers[serverName].databases[dbName] = {} };
140
- }
141
- if (!schema.servers[serverName].databases[dbName].realName) { schema.servers[serverName].databases[dbName].realName = dbName } // add realName
142
-
143
- // table
144
- if (objType === objectType.table || objType == undefined) {
145
- if (!objName || objName.length == 0) {
146
- if (!schema.servers[serverName].databases[dbName] || !Object.keys(schema.servers[serverName].databases[dbName].tables)[0]) throw new TypeError('missing default table in dbSchema');
147
- objName = Object.keys(schema.servers[serverName].databases[dbName].tables)[0];
148
- }
149
- else { // add table to schema
150
- if (!schema.servers[serverName].databases[dbName].tables) { schema.servers[serverName].databases[dbName].tables = {} };
151
- if (!schema.servers[serverName].databases[dbName].tables[objName]) { schema.servers[serverName].databases[dbName].tables[objName] = {} }
152
- }
153
- if (!schema.servers[serverName].databases[dbName].tables[objName].realName) { schema.servers[serverName].databases[dbName].tables[objName].realName = objName } // add realName
154
- }
155
-
156
- // store procedure
157
- if (objType === objectType.procedure) {
158
- if (!objName || objName.length == 0) {
159
- if (!schema.servers[serverName].databases[dbName] || !Object.keys(schema.servers[serverName].databases[dbName].procedures)[0]) throw new TypeError('missing default procedure in dbSchema');
160
- objName = Object.keys(schema.servers[serverName].databases[dbName].procedures)[0];
161
- }
162
- else { // add storeprocedure to schema
163
- if (!schema.servers[serverName].databases[dbName].procedures) { schema.servers[serverName].databases[dbName].procedures = {} };
164
- if (!schema.servers[serverName].databases[dbName].procedures[objName]) { schema.servers[serverName].databases[dbName].procedures[objName] = {command: obj} }
165
- }
166
- if (!schema.servers[serverName].databases[dbName].procedures[objName].realName) { schema.servers[serverName].databases[dbName].procedures[objName].realName = objName } // add realName
167
- }
168
-
169
- // return Schema
170
- return {
171
- table: (objType == undefined || objType === objectType.table) ? schema.servers[serverName].databases[dbName].tables[objName] : undefined,
172
- procedure: (objType === objectType.procedure) ? schema.servers[serverName].databases[dbName].procedures[objName] : undefined,
173
- database: schema.servers[serverName].databases[dbName],
174
- server: schema.servers[serverName]
175
- }
176
-
177
- }
178
-
179
- export function prepareRun(tSchema, connection, reqOpe) {
180
- const _reqOpe = [];
181
- const _result = [];
182
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
183
- else _reqOpe.push(reqOpe);
184
- _reqOpe.forEach(function (_ope) {
185
- if (_ope?.get) _result.push(prepareGet(tSchema, connection, _ope));
186
- else if (_ope?.patch) _result.push(preparePatch(tSchema, connection, _ope));
187
- else if (_ope?.put) _result.push(preparePut(tSchema, connection, _ope));
188
- else if (_ope?.delete) _result.push(prepareDelete(tSchema, connection, _ope));
189
- else if (_ope?.execute) _result.push(prepareExecute(connection, _ope));
190
- else if (_ope?.begin) _result.push(prepareBegin(connection, _ope));
191
- else if (_ope?.commit) _result.push(prepareCommit(connection, _ope));
192
- else if (_ope?.rollback) _result.push(prepareRollback(connection, _ope));
193
- else if (_ope?.passthrough) _result.push(preparePassthrough(connection, _ope));
194
- else throw new Error('Request sintax error, missing property get/patch/put/delete/...');
195
- });
196
- if (_result.length > 1) return _result;
197
- if (_result.length = 1) return _result[0];
198
- return undefined;
199
- }
200
-
201
- export function prepareRunById(tSchema, connection, reqOpe, idValue) {
202
- if (reqOpe?.get) return prepareGetById(tSchema, connection, reqOpe, idValue);
203
- if (reqOpe?.patch) return preparePatchById(tSchema, connection, reqOpe, idValue);
204
- if (reqOpe?.put) return preparePutById(tSchema, connection, reqOpe, idValue);
205
- if (reqOpe?.delete) return prepareDeleteById(tSchema, connection, reqOpe, idValue);
206
- else throw new Error('Request sintax error, missing property get/patch/put/delete...');
207
- }
208
-
209
- export function prepareGet(tSchema, connection, reqOpe) {
210
- const _reqOpe = [];
211
- const _result = [];
212
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
213
- else _reqOpe.push(reqOpe);
214
- _reqOpe.forEach(function (_ope) {
215
- _result.push({
216
- get: {
217
- schema: tSchema,
218
- options: _ope?.get?.options,
219
- fields: _ope?.get?.fields ?? defautlFields,
220
- filters: _ope?.get?.filters,
221
- groups: _ope?.get?.groups,
222
- groupsFilters: _ope?.get?.groupsFilters,
223
- orderBy: _ope?.get?.orderBy,
224
- params: _ope?.get?.params
225
- },
226
- connection: connection
227
- });
228
- });
229
- if (_result.length > 1) return _result;
230
- if (_result.length = 1) return _result[0];
231
- return undefined;
232
- };
233
-
234
- export function prepareGetById(tSchema, connection, reqOpe, idValue) {
235
- const _filters = [idField(tSchema) + ' = ' + toStringValue(idValue)];
236
- if (reqOpe?.get?.filters && reqOpe?.get?.filters.length > 0) {
237
- _filters.push('and');
238
- Array.isArray(reqOpe.get.filters) ? _filters.push(...reqOpe.get.filters) : _filters.push(reqOpe.get.filters);
239
- }
240
- return {
241
- get: {
242
- schema: tSchema,
243
- options: reqOpe?.get?.options,
244
- fields: reqOpe?.get?.fields ?? defautlFields,
245
- filters: _filters
246
- },
247
- connection: connection
248
- }
249
- };
250
-
251
- export function preparePatch(tSchema, connection, reqOpe) {
252
- const _reqOpe = [];
253
- const _result = [];
254
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
255
- else _reqOpe.push(reqOpe);
256
- _reqOpe.forEach(function (_ope) {
257
- if (_ope?.patch) {
258
- if (!_ope.patch.sets) throw new Error('Request sintax error, missing "patch.sets" property.')
259
- _result.push({
260
- patch: {
261
- schema: tSchema,
262
- sets: _ope.patch.sets,
263
- filters: _ope.patch.filters,
264
- params: _ope.patch.params
265
- },
266
- connection: connection
267
- });
268
- }
269
- else throw new Error('Request sintax error, missing "patch" property.');
270
- });
271
- if (_result.length > 1) return _result;
272
- if (_result.length = 1) return _result[0];
273
- return undefined;
274
- };
275
-
276
- export function preparePatchById(tSchema, connection, reqOpe, idValue) {
277
- if (!reqOpe?.patch) throw new Error('Request sintax error, missing "patch" property.')
278
- if (!reqOpe.patch.sets) throw new Error('Missing "patch.sets" in operation.')
279
- const _filters = [idField(tSchema) + ' = ' + toStringValue(idValue)];
280
- if (reqOpe?.patch?.filters && reqOpe?.patch?.filters.length > 0) {
281
- _filters.push('and');
282
- Array.isArray(reqOpe.patch.filters) ? _filters.push(...reqOpe.patch.filters) : _filters.push(reqOpe.patch.filters);
283
- }
284
- return {
285
- patch: {
286
- schema: tSchema,
287
- sets: reqOpe.patch.sets,
288
- filters: _filters
289
- },
290
- connection: connection
291
- }
292
- };
293
-
294
- export function preparePut(tSchema, connection, reqOpe) {
295
- const _reqOpe = [];
296
- const _result = [];
297
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
298
- else _reqOpe.push(reqOpe);
299
- _reqOpe.forEach(function (_ope) {
300
- if (_ope?.put) {
301
- if (!_ope.put.sets) throw new Error('Request sintax error, missing "put.sets" property.')
302
- _result.push({
303
- put: {
304
- schema: tSchema,
305
- sets: _ope.put.sets,
306
- params: _ope.put.params
307
- },
308
- connection: connection
309
- });
310
- }
311
- else throw new Error('Request sintax error, missing "put" property.');
312
- });
313
- if (_result.length > 1) return _result;
314
- if (_result.length = 1) return _result[0];
315
- return undefined;
316
- };
317
-
318
- export function preparePutById(tSchema, connection, reqOpe, idValue) {
319
- if (!reqOpe?.put) throw new Error('Request sintax error, missing "put" property.')
320
- if (!reqOpe.put.sets) throw new Error('Missing "put.sets" in operation.')
321
- reqOpe.put.sets[idField(tSchema)] = idValue;
322
- return {
323
- put: {
324
- schema: tSchema,
325
- sets: reqOpe.put.sets,
326
- params: reqOpe.put.params
327
- },
328
- connection: connection
329
- }
330
- };
331
-
332
- export function prepareDelete(tSchema, connection, reqOpe) {
333
- const _reqOpe = [];
334
- const _result = [];
335
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
336
- else _reqOpe.push(reqOpe);
337
- _reqOpe.forEach(function (_ope) {
338
- if (_ope?.delete) {
339
- _result.push({
340
- delete: {
341
- schema: tSchema,
342
- filters: _ope.delete.filters,
343
- params: _ope.delete.params
344
- },
345
- connection: connection
346
- });
347
- }
348
- else throw new Error('Request sintax error, missing "delete" property.');
349
- });
350
- if (_result.length > 1) return _result;
351
- if (_result.length = 1) return _result[0];
352
- return undefined;
353
- };
354
-
355
- export function prepareDeleteById(tSchema, connection, idValue) {
356
- return {
357
- delete: {
358
- schema: tSchema,
359
- filters: [idField(tSchema) + ' = ' + toStringValue(idValue)]
360
- },
361
- connection: connection
362
- }
363
- };
364
-
365
- export function prepareExecute(pSchema, connection, reqOpe) {
366
- const _reqOpe = [];
367
- const _result = [];
368
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
369
- else _reqOpe.push(reqOpe);
370
- _reqOpe.forEach(function (_ope) {
371
- _result.push({
372
- execute: {
373
- schema: pSchema,
374
- arguments: _ope.execute?.arguments,
375
- params: _ope.execute?.params
376
- },
377
- connection: connection
378
- });
379
- });
380
- if (_result.length > 1) return _result;
381
- if (_result.length = 1) return _result[0];
382
- return undefined;
383
- };
384
-
385
- export function preparePassthrough(connection, reqOpe) {
386
- const _reqOpe = [];
387
- const _result = [];
388
- if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
389
- else _reqOpe.push(reqOpe);
390
- _reqOpe.forEach(function (_ope) {
391
- if (_ope?.passthrough) {
392
- if (!_ope.passthrough.command) throw new Error('Request sintax error, missing "passthrough.command" property.')
393
- _result.push({
394
- passthrough: {
395
- command: _ope.passthrough.command,
396
- params: _ope.passthrough.params
397
- },
398
- connection: connection
399
- });
400
- }
401
- else throw new Error('Request sintax error, missing "passthrough" property.');
402
- });
403
- if (_result.length > 1) return _result;
404
- if (_result.length = 1) return _result[0];
405
- return undefined;
406
- };
407
-
408
- export function prepareBegin(connection, reqOpe) {
409
- return {
410
- begin: {},
411
- connection: connection
412
- }
413
- };
414
-
415
- export function prepareCommit(connection, reqOpe) {
416
- return {
417
- commit: {},
418
- connection: connection
419
- }
420
- };
421
-
422
- export function prepareRollback(connection, reqOpe) {
423
- return {
424
- rollback: {},
425
- connection: connection
426
- }
427
- };
1
+ 'use strict';
2
+
3
+ // import modules
4
+ import schema from './schema.js'
5
+ import * as mssql from './mssql.js';
6
+ import * as mysql from './mysql.js';
7
+
8
+ // Common
9
+ const defautlFields = ['*'];
10
+ export const serverType = Object.freeze({ mssql: 'ms-sql', mysql: 'my-sql', mongodb: 'mongo-db' });
11
+ export const objectType = Object.freeze({ table: 'T', procedure: 'P' });
12
+
13
+ // Exported functions
14
+ export function idField(tSchema) {
15
+ return tSchema.table?.idField ?? 'id';
16
+ }
17
+
18
+ export function autoId(tSchema) {
19
+ return tSchema.table?.autoId;
20
+ }
21
+
22
+ export function getProperty(obj, keyToFind) {
23
+ if (typeof obj !== 'object' || obj === null || !keyToFind) {
24
+ return undefined;
25
+ }
26
+
27
+ const lowerKeyToFind = keyToFind.toLowerCase();
28
+
29
+ for (const key in obj) {
30
+ // Usa hasOwnProperty per escludere le proprietà ereditate
31
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
32
+
33
+ if (key.toLowerCase() === lowerKeyToFind) {
34
+ return obj[key];
35
+ }
36
+ }
37
+ }
38
+
39
+ return undefined;
40
+ }
41
+
42
+ export function upsertProperty(obj, keyToUpdate, newValue) {
43
+ if (typeof obj !== 'object' || obj === null) {
44
+ // Non è un oggetto valido
45
+ return false;
46
+ }
47
+
48
+ const lowerKeyToUpdate = keyToUpdate.toLowerCase();
49
+ let propertyFound = false;
50
+
51
+ for (const key in obj) {
52
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
53
+ if (key.toLowerCase() === lowerKeyToUpdate) {
54
+ obj[key] = newValue;
55
+ propertyFound = true;
56
+ break;
57
+ }
58
+ }
59
+ }
60
+
61
+ if (!propertyFound) {
62
+ obj[keyToUpdate] = newValue;
63
+ }
64
+
65
+ return true;
66
+ }
67
+
68
+ export async function runQuery(dbOpes) {
69
+
70
+ // all connections must have same id
71
+ const _connection = Array.isArray(dbOpes) ? dbOpes[0].connection : dbOpes.connection;
72
+ if (Array.isArray(dbOpes)) {
73
+ for (let i = 1; i < dbOpes.length; i++) {
74
+ if (dbOpes[i].connection?.id !== _connection.id) throw new TypeError('runQuery: for multiple operations, connection must be the same');
75
+ }
76
+ }
77
+
78
+ // MSSQL
79
+ if (_connection.serverType === serverType.mssql) {
80
+ return await mssql.query(_connection, dbOpes);
81
+ }
82
+
83
+ // MySQL
84
+ else if (_connection.serverType === serverType.mysql) {
85
+ return await mysql.query(_connection, dbOpes);
86
+ }
87
+
88
+ // server type not supported
89
+ else throw new TypeError('server type not supported');
90
+
91
+ }
92
+
93
+ export function prepareConnection(schema) {
94
+ // MySQL
95
+ if (schema.server.type === serverType.mysql) {
96
+ const _return = mysql.prepareConnection(schema);
97
+ _return.serverType = serverType.mysql; // append 'serverType' to connection
98
+ return _return;
99
+ }
100
+ // MSSQL is also the default
101
+ if (!schema.server.type || schema.server.type === serverType.mssql) {
102
+ const _return = mssql.prepareConnection(schema);
103
+ _return.serverType = serverType.mssql; // append 'serverType' to connection
104
+ return _return;
105
+ }
106
+ throw new TypeError('server type not supported');
107
+ }
108
+
109
+ export function closeConnection(connection) {
110
+ // MSSQL
111
+ if (connection.serverType === serverType.mssql) { return mssql.closeConnection(connection); }
112
+ // MySQL
113
+ if (connection.serverType === serverType.mysql) { return mysql.closeConnection(connection); }
114
+ }
115
+
116
+ export function closeAllConnections() {
117
+ // MSSQL
118
+ mssql.closeAllConnections();
119
+ // MySQL
120
+ mysql.closeAllConnections();
121
+
122
+ return;
123
+ }
124
+
125
+ export function testConnection() {
126
+ const _connection = prepareConnection(prepareSchema("test", objectType.procedure));
127
+ // MSSQL
128
+ if (_connection.serverType === serverType.mssql) { return mssql.testConnection(_connection); }
129
+ // MySQL
130
+ if (_connection.serverType === serverType.mysql) { return mysql.testConnection(_connection); }
131
+ // server type not supported
132
+ throw new TypeError('server type not supported');
133
+ }
134
+
135
+ export function toStringValue(value) {
136
+ if (!value) return 'null';
137
+ if (value.trimStart().charAt(0) === '\'') return value;
138
+ return `\'${value}\'`;
139
+ }
140
+
141
+ export function prepareSchema(obj, objType) {
142
+ // objType = 'objectType.' (Table or StoreProcedure)
143
+ // obj = 'systemName.dbName.objName' or 'dbName.objName' or 'objName' or 'systemNme..objName'
144
+
145
+ if (obj == undefined || typeof obj !== 'string') throw new TypeError('prepareSchema: wrong obj');
146
+ if (objType !== undefined && objType !== objectType.table && objType !== objectType.procedure) throw new TypeError(`prepareSchema: wrong objType, must be ${objectType.join(', ')}`);
147
+ const objPath = obj.trimStart().split(' ')[0];
148
+
149
+ // split objPath to serverName,dbName,objName
150
+ let serverName = undefined;
151
+ let dbName = undefined;
152
+ let objName = undefined;
153
+ const objPathArray = objPath.split('.');
154
+ if (objPathArray.length > 2) {
155
+ serverName = objPathArray[0]
156
+ dbName = objPathArray[1]
157
+ objName = objPathArray[2]
158
+ }
159
+ else if (objPathArray.length > 1) {
160
+ dbName = objPathArray[0]
161
+ objName = objPathArray[1]
162
+ }
163
+ else if (objPathArray.length > 0) {
164
+ objName = objPathArray[0]
165
+ }
166
+
167
+ // server
168
+ if (!serverName || serverName.length == 0) {
169
+ if (!schema.servers || !Object.keys(schema.servers)[0]) throw new TypeError('missing default server in dbSchema');
170
+ serverName = Object.keys(schema.servers)[0];
171
+ }
172
+ else { // add server to schema
173
+ if (!schema.servers) { schema.servers = {} };
174
+ if (!schema.servers[serverName]) { schema.servers[serverName] = {} };
175
+ }
176
+ if (!schema.servers[serverName].realName) { schema.servers[serverName].realName = serverName } // add realName
177
+
178
+ // database
179
+ if (!dbName || dbName.length == 0) {
180
+ if (!schema.servers[serverName].databases || !Object.keys(schema.servers[serverName].databases)[0]) throw new TypeError('missing default database in dbSchema');
181
+ dbName = Object.keys(schema.servers[serverName].databases)[0];
182
+ }
183
+ else { // add db to schema
184
+ if (!schema.servers[serverName].databases) { schema.servers[serverName].databases = {} };
185
+ if (!schema.servers[serverName].databases[dbName]) { schema.servers[serverName].databases[dbName] = {} };
186
+ }
187
+ if (!schema.servers[serverName].databases[dbName].realName) { schema.servers[serverName].databases[dbName].realName = dbName } // add realName
188
+
189
+ // table
190
+ if (objType === objectType.table || objType == undefined) {
191
+ if (!objName || objName.length == 0) {
192
+ if (!schema.servers[serverName].databases[dbName] || !Object.keys(schema.servers[serverName].databases[dbName].tables)[0]) throw new TypeError('missing default table in dbSchema');
193
+ objName = Object.keys(schema.servers[serverName].databases[dbName].tables)[0];
194
+ }
195
+ else { // add table to schema
196
+ if (!schema.servers[serverName].databases[dbName].tables) { schema.servers[serverName].databases[dbName].tables = {} };
197
+ if (!schema.servers[serverName].databases[dbName].tables[objName]) { schema.servers[serverName].databases[dbName].tables[objName] = {} }
198
+ }
199
+ if (!schema.servers[serverName].databases[dbName].tables[objName].realName) { schema.servers[serverName].databases[dbName].tables[objName].realName = objName } // add realName
200
+ }
201
+
202
+ // store procedure
203
+ if (objType === objectType.procedure) {
204
+ if (!objName || objName.length == 0) {
205
+ if (!schema.servers[serverName].databases[dbName] || !Object.keys(schema.servers[serverName].databases[dbName].procedures)[0]) throw new TypeError('missing default procedure in dbSchema');
206
+ objName = Object.keys(schema.servers[serverName].databases[dbName].procedures)[0];
207
+ }
208
+ else { // add storeprocedure to schema
209
+ if (!schema.servers[serverName].databases[dbName].procedures) { schema.servers[serverName].databases[dbName].procedures = {} };
210
+ if (!schema.servers[serverName].databases[dbName].procedures[objName]) { schema.servers[serverName].databases[dbName].procedures[objName] = {command: obj} }
211
+ }
212
+ if (!schema.servers[serverName].databases[dbName].procedures[objName].realName) { schema.servers[serverName].databases[dbName].procedures[objName].realName = objName } // add realName
213
+ }
214
+
215
+ // return Schema
216
+ return {
217
+ table: (objType == undefined || objType === objectType.table) ? schema.servers[serverName].databases[dbName].tables[objName] : undefined,
218
+ procedure: (objType === objectType.procedure) ? schema.servers[serverName].databases[dbName].procedures[objName] : undefined,
219
+ database: schema.servers[serverName].databases[dbName],
220
+ server: schema.servers[serverName]
221
+ }
222
+
223
+ }
224
+
225
+ export function prepareRun(tSchema, connection, reqOpe) {
226
+ const _reqOpe = [];
227
+ const _result = [];
228
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
229
+ else _reqOpe.push(reqOpe);
230
+ _reqOpe.forEach(function (_ope) {
231
+ if (_ope?.get) _result.push(prepareGet(tSchema, connection, _ope));
232
+ else if (_ope?.patch) _result.push(preparePatch(tSchema, connection, _ope));
233
+ else if (_ope?.put) _result.push(preparePut(tSchema, connection, _ope));
234
+ else if (_ope?.delete) _result.push(prepareDelete(tSchema, connection, _ope));
235
+ else if (_ope?.execute) _result.push(prepareExecute(connection, _ope));
236
+ else if (_ope?.begin) _result.push(prepareBegin(connection, _ope));
237
+ else if (_ope?.commit) _result.push(prepareCommit(connection, _ope));
238
+ else if (_ope?.rollback) _result.push(prepareRollback(connection, _ope));
239
+ else if (_ope?.passthrough) _result.push(preparePassthrough(connection, _ope));
240
+ else throw new Error('Request sintax error, missing property get/patch/put/delete/...');
241
+ });
242
+ if (_result.length > 1) return _result;
243
+ if (_result.length = 1) return _result[0];
244
+ return undefined;
245
+ }
246
+
247
+ export function prepareRunById(tSchema, connection, reqOpe, idValue) {
248
+ if (reqOpe?.get) return prepareGetById(tSchema, connection, reqOpe, idValue);
249
+ if (reqOpe?.patch) return preparePatchById(tSchema, connection, reqOpe, idValue);
250
+ if (reqOpe?.put) return preparePutById(tSchema, connection, reqOpe, idValue);
251
+ if (reqOpe?.delete) return prepareDeleteById(tSchema, connection, reqOpe, idValue);
252
+ else throw new Error('Request sintax error, missing property get/patch/put/delete...');
253
+ }
254
+
255
+ export function prepareGet(tSchema, connection, reqOpe) {
256
+ const _reqOpe = [];
257
+ const _result = [];
258
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
259
+ else _reqOpe.push(reqOpe);
260
+ _reqOpe.forEach(function (_ope) {
261
+ _result.push({
262
+ get: {
263
+ schema: tSchema,
264
+ options: _ope?.get?.options,
265
+ fields: _ope?.get?.fields ?? defautlFields,
266
+ filters: _ope?.get?.filters,
267
+ groups: _ope?.get?.groups,
268
+ groupsFilters: _ope?.get?.groupsFilters,
269
+ orderBy: _ope?.get?.orderBy,
270
+ params: _ope?.get?.params
271
+ },
272
+ connection: connection
273
+ });
274
+ });
275
+ if (_result.length > 1) return _result;
276
+ if (_result.length = 1) return _result[0];
277
+ return undefined;
278
+ };
279
+
280
+ export function prepareGetById(tSchema, connection, reqOpe, idValue) {
281
+ const _filters = [idField(tSchema) + ' = ' + toStringValue(idValue)];
282
+ if (reqOpe?.get?.filters && reqOpe?.get?.filters.length > 0) {
283
+ _filters.push('and');
284
+ Array.isArray(reqOpe.get.filters) ? _filters.push(...reqOpe.get.filters) : _filters.push(reqOpe.get.filters);
285
+ }
286
+ return {
287
+ get: {
288
+ schema: tSchema,
289
+ options: reqOpe?.get?.options,
290
+ fields: reqOpe?.get?.fields ?? defautlFields,
291
+ filters: _filters
292
+ },
293
+ connection: connection
294
+ }
295
+ };
296
+
297
+ export function preparePatch(tSchema, connection, reqOpe) {
298
+ const _reqOpe = [];
299
+ const _result = [];
300
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
301
+ else _reqOpe.push(reqOpe);
302
+ _reqOpe.forEach(function (_ope) {
303
+ if (_ope?.patch) {
304
+ if (!_ope.patch.sets) throw new Error('Request sintax error, missing "patch.sets" property.')
305
+ _result.push({
306
+ patch: {
307
+ schema: tSchema,
308
+ sets: _ope.patch.sets,
309
+ filters: _ope.patch.filters,
310
+ params: _ope.patch.params
311
+ },
312
+ connection: connection
313
+ });
314
+ }
315
+ else throw new Error('Request sintax error, missing "patch" property.');
316
+ });
317
+ if (_result.length > 1) return _result;
318
+ if (_result.length = 1) return _result[0];
319
+ return undefined;
320
+ };
321
+
322
+ export function preparePatchById(tSchema, connection, reqOpe, idValue) {
323
+ if (!reqOpe?.patch) throw new Error('Request sintax error, missing "patch" property.')
324
+ if (!reqOpe.patch.sets) throw new Error('Missing "patch.sets" in operation.')
325
+ const _filters = [idField(tSchema) + ' = ' + toStringValue(idValue)];
326
+ if (reqOpe?.patch?.filters && reqOpe?.patch?.filters.length > 0) {
327
+ _filters.push('and');
328
+ Array.isArray(reqOpe.patch.filters) ? _filters.push(...reqOpe.patch.filters) : _filters.push(reqOpe.patch.filters);
329
+ }
330
+ return {
331
+ patch: {
332
+ schema: tSchema,
333
+ sets: reqOpe.patch.sets,
334
+ filters: _filters
335
+ },
336
+ connection: connection
337
+ }
338
+ };
339
+
340
+ export function preparePut(tSchema, connection, reqOpe) {
341
+ const _reqOpe = [];
342
+ const _result = [];
343
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
344
+ else _reqOpe.push(reqOpe);
345
+ _reqOpe.forEach(function (_ope) {
346
+ if (_ope?.put) {
347
+ if (!_ope.put.sets) throw new Error('Request sintax error, missing "put.sets" property.')
348
+ _result.push({
349
+ put: {
350
+ schema: tSchema,
351
+ sets: _ope.put.sets,
352
+ params: _ope.put.params
353
+ },
354
+ connection: connection
355
+ });
356
+ }
357
+ else throw new Error('Request sintax error, missing "put" property.');
358
+ });
359
+ if (_result.length > 1) return _result;
360
+ if (_result.length = 1) return _result[0];
361
+ return undefined;
362
+ };
363
+
364
+ export function preparePutById(tSchema, connection, reqOpe, idValue) {
365
+ if (!reqOpe?.put) throw new Error('Request sintax error, missing "put" property.')
366
+ if (!reqOpe.put.sets) throw new Error('Missing "put.sets" in operation.')
367
+ upsertProperty(reqOpe.put.sets, idField(tSchema), idValue);
368
+ return {
369
+ put: {
370
+ schema: tSchema,
371
+ sets: reqOpe.put.sets,
372
+ params: reqOpe.put.params
373
+ },
374
+ connection: connection
375
+ }
376
+ };
377
+
378
+ export function prepareDelete(tSchema, connection, reqOpe) {
379
+ const _reqOpe = [];
380
+ const _result = [];
381
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
382
+ else _reqOpe.push(reqOpe);
383
+ _reqOpe.forEach(function (_ope) {
384
+ if (_ope?.delete) {
385
+ _result.push({
386
+ delete: {
387
+ schema: tSchema,
388
+ filters: _ope.delete.filters,
389
+ params: _ope.delete.params
390
+ },
391
+ connection: connection
392
+ });
393
+ }
394
+ else throw new Error('Request sintax error, missing "delete" property.');
395
+ });
396
+ if (_result.length > 1) return _result;
397
+ if (_result.length = 1) return _result[0];
398
+ return undefined;
399
+ };
400
+
401
+ export function prepareDeleteById(tSchema, connection, idValue) {
402
+ return {
403
+ delete: {
404
+ schema: tSchema,
405
+ filters: [idField(tSchema) + ' = ' + toStringValue(idValue)]
406
+ },
407
+ connection: connection
408
+ }
409
+ };
410
+
411
+ export function prepareExecute(pSchema, connection, reqOpe) {
412
+ const _reqOpe = [];
413
+ const _result = [];
414
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
415
+ else _reqOpe.push(reqOpe);
416
+ _reqOpe.forEach(function (_ope) {
417
+ _result.push({
418
+ execute: {
419
+ schema: pSchema,
420
+ arguments: _ope.execute?.arguments,
421
+ params: _ope.execute?.params
422
+ },
423
+ connection: connection
424
+ });
425
+ });
426
+ if (_result.length > 1) return _result;
427
+ if (_result.length = 1) return _result[0];
428
+ return undefined;
429
+ };
430
+
431
+ export function preparePassthrough(connection, reqOpe) {
432
+ const _reqOpe = [];
433
+ const _result = [];
434
+ if (Array.isArray(reqOpe)) _reqOpe.push(...reqOpe);
435
+ else _reqOpe.push(reqOpe);
436
+ _reqOpe.forEach(function (_ope) {
437
+ if (_ope?.passthrough) {
438
+ if (!_ope.passthrough.command) throw new Error('Request sintax error, missing "passthrough.command" property.')
439
+ _result.push({
440
+ passthrough: {
441
+ command: _ope.passthrough.command,
442
+ params: _ope.passthrough.params
443
+ },
444
+ connection: connection
445
+ });
446
+ }
447
+ else throw new Error('Request sintax error, missing "passthrough" property.');
448
+ });
449
+ if (_result.length > 1) return _result;
450
+ if (_result.length = 1) return _result[0];
451
+ return undefined;
452
+ };
453
+
454
+ export function prepareBegin(connection, reqOpe) {
455
+ return {
456
+ begin: {},
457
+ connection: connection
458
+ }
459
+ };
460
+
461
+ export function prepareCommit(connection, reqOpe) {
462
+ return {
463
+ commit: {},
464
+ connection: connection
465
+ }
466
+ };
467
+
468
+ export function prepareRollback(connection, reqOpe) {
469
+ return {
470
+ rollback: {},
471
+ connection: connection
472
+ }
473
+ };