db-crud-api 0.1.6 → 0.1.7

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