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.
- package/README.md +96 -93
- package/index.js +10 -10
- package/lib/api-execute.js +25 -0
- package/lib/api-factory.js +21 -20
- package/lib/api-full.js +13 -13
- package/lib/api-ro.js +5 -5
- package/lib/db-operations.js +439 -354
- package/lib/mssql.js +267 -267
- package/lib/schema.js +3 -3
- package/package.json +3 -2
package/lib/db-operations.js
CHANGED
|
@@ -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
|
|
51
|
-
// eg.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!schema.servers
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (!schema.servers[serverName].databases
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (!
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
return
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
export function
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
+
};
|