@bee.js/node 0.0.28 → 0.0.32
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/beeRequest.js +42 -42
- package/beehive.js +454 -451
- package/index.js +14 -14
- package/lib/DBA/beeDBA.js +119 -119
- package/lib/DEV/beeDEV.js +41 -41
- package/lib/JWT/beeJWT.js +66 -61
- package/lib/ORM/beeORM.js +314 -308
- package/lib/WEB/freeRoute.js +4 -4
- package/lib/WEB/route.js +27 -27
- package/lib/beeHive/create.js +51 -51
- package/lib/beeHive/headers.js +9 -9
- package/lib/beeHive/load.js +19 -19
- package/lib/beeHive/log.js +2 -2
- package/lib/beeHive/routes.js +34 -34
- package/lib/beeHive/start.js +29 -29
- package/package.json +40 -40
- package/security/index.js +8 -8
- package/services/CRON.js +8 -8
- package/services/EMAIL.js +2 -2
- package/services/HTTP.js +32 -32
- package/services/HTTPS.js +31 -31
- package/services/LOGS.js +7 -7
- package/services/SCRIPT.js +13 -13
- package/services/SHELL.js +2 -2
- package/services/index.js +9 -9
- package/tools/beeTools.js +18 -18
- package/tools/guid.js +14 -14
- package/tools/hash.js +12 -12
- package/tools/model.js +20 -20
- package/tools/slug.js +16 -16
- package/tools/string.js +55 -55
package/beehive.js
CHANGED
|
@@ -1,452 +1,455 @@
|
|
|
1
|
-
const log = require('./lib/beeHive/log')
|
|
2
|
-
const headers = require('./lib/beeHive/headers')
|
|
3
|
-
const beeORM = require('./lib/ORM/beeORM')
|
|
4
|
-
const beeDBA = require('./lib/DBA/beeDBA')
|
|
5
|
-
const beeJWT = require('./lib/JWT/beeJWT')
|
|
6
|
-
const beeTools = require('./tools/beeTools')
|
|
7
|
-
|
|
8
|
-
module.exports = function hive(req = {}, res = {}, model = null) {
|
|
9
|
-
|
|
10
|
-
model = model && typeof(model)==="string"
|
|
11
|
-
? global.configs.models[model]
|
|
12
|
-
: model
|
|
13
|
-
|
|
14
|
-
let script = [{ main: true, model: model, ids: [], select: [], where: [], binds: [], orderBy: [], limit: [], fields: [] }]
|
|
15
|
-
let data = res.data || {}
|
|
16
|
-
let counters = res.counters || {}
|
|
17
|
-
let inserts = res.inserts || []
|
|
18
|
-
let debug = res.debug || []
|
|
19
|
-
let error = res.error || false
|
|
20
|
-
let errorCode = res.errorCode || 0
|
|
21
|
-
let mainContainer = null
|
|
22
|
-
let models = global.models
|
|
23
|
-
let relWhere = {}
|
|
24
|
-
|
|
25
|
-
const dbExec = async function(SQL, binds = [], params = {}) {
|
|
26
|
-
|
|
27
|
-
let result = []
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
let
|
|
82
|
-
let
|
|
83
|
-
let
|
|
84
|
-
let
|
|
85
|
-
let
|
|
86
|
-
let
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
ids
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if(condition
|
|
238
|
-
script[0]['
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
fields
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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
|
-
let
|
|
324
|
-
let
|
|
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
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
sql.
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if(
|
|
417
|
-
out.
|
|
418
|
-
|
|
419
|
-
if(
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
case '
|
|
427
|
-
res.status(errorCode ||
|
|
428
|
-
break
|
|
429
|
-
case '
|
|
430
|
-
res.status(errorCode ||
|
|
431
|
-
break
|
|
432
|
-
|
|
433
|
-
res.status(errorCode || 200).json(out)
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
1
|
+
const log = require('./lib/beeHive/log')
|
|
2
|
+
const headers = require('./lib/beeHive/headers')
|
|
3
|
+
const beeORM = require('./lib/ORM/beeORM')
|
|
4
|
+
const beeDBA = require('./lib/DBA/beeDBA')
|
|
5
|
+
const beeJWT = require('./lib/JWT/beeJWT')
|
|
6
|
+
const beeTools = require('./tools/beeTools')
|
|
7
|
+
|
|
8
|
+
module.exports = function hive(req = {}, res = {}, model = null) {
|
|
9
|
+
|
|
10
|
+
model = model && typeof(model)==="string"
|
|
11
|
+
? global.configs.models[model]
|
|
12
|
+
: model
|
|
13
|
+
|
|
14
|
+
let script = [{ main: true, model: model, ids: [], select: [], where: [], binds: [], orderBy: [], limit: [], fields: [] }]
|
|
15
|
+
let data = res.data || {}
|
|
16
|
+
let counters = res.counters || {}
|
|
17
|
+
let inserts = res.inserts || []
|
|
18
|
+
let debug = res.debug || []
|
|
19
|
+
let error = res.error || false
|
|
20
|
+
let errorCode = res.errorCode || 0
|
|
21
|
+
let mainContainer = null
|
|
22
|
+
let models = global.models
|
|
23
|
+
let relWhere = {}
|
|
24
|
+
|
|
25
|
+
const dbExec = async function(SQL, binds = [], params = {}) {
|
|
26
|
+
|
|
27
|
+
let result = []
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
if(global.configs.debug)
|
|
31
|
+
console.log(SQL)
|
|
32
|
+
|
|
33
|
+
global.beeDBPool = global.beeDBPool || beeORM.createPool(global.configs)
|
|
34
|
+
result = await global.beeDBPool.query(SQL, binds)
|
|
35
|
+
|
|
36
|
+
} catch(e) {
|
|
37
|
+
log("####### DB ERROR:")
|
|
38
|
+
log(e)
|
|
39
|
+
error = e
|
|
40
|
+
errorCode = 502
|
|
41
|
+
} finally {
|
|
42
|
+
if(global.configs.debug) debug.push(SQL)
|
|
43
|
+
if(global.configs.debug && binds) debug.push(binds)
|
|
44
|
+
|
|
45
|
+
if(params.container) {
|
|
46
|
+
data[params.container] = result[0]
|
|
47
|
+
|
|
48
|
+
if(counters)
|
|
49
|
+
counters[params.container] = (result[0] || []).length
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return result[0]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return Object.assign(
|
|
57
|
+
{},
|
|
58
|
+
res, {
|
|
59
|
+
data,
|
|
60
|
+
counters,
|
|
61
|
+
inserts,
|
|
62
|
+
debug,
|
|
63
|
+
error,
|
|
64
|
+
errorCode,
|
|
65
|
+
script,
|
|
66
|
+
models,
|
|
67
|
+
beeDBA,
|
|
68
|
+
token: beeJWT,
|
|
69
|
+
configs: this.config,
|
|
70
|
+
headers,
|
|
71
|
+
dbExec,
|
|
72
|
+
|
|
73
|
+
dbEngine: async function() {
|
|
74
|
+
|
|
75
|
+
error = !script[0].model
|
|
76
|
+
? `MODEL NOT FOUND` // TODO refazer
|
|
77
|
+
: error
|
|
78
|
+
|
|
79
|
+
if(error) return this
|
|
80
|
+
|
|
81
|
+
let sqlSelect = beeORM.sqlSelect
|
|
82
|
+
let q = beeORM.quote
|
|
83
|
+
let relTables = []
|
|
84
|
+
let relIds = []
|
|
85
|
+
let relNow = null
|
|
86
|
+
let relPrev = null
|
|
87
|
+
let relJoin = {}
|
|
88
|
+
let parentTable = null
|
|
89
|
+
let i = -1
|
|
90
|
+
|
|
91
|
+
for(let _script of script) {
|
|
92
|
+
i++
|
|
93
|
+
|
|
94
|
+
let model = _script.model
|
|
95
|
+
let binds = _script.binds || []
|
|
96
|
+
let modelName = model.name || model.table
|
|
97
|
+
data[modelName] = null
|
|
98
|
+
|
|
99
|
+
if(i === 0) {
|
|
100
|
+
mainContainer = modelName
|
|
101
|
+
ids = model.ids
|
|
102
|
+
} else {
|
|
103
|
+
ids = relTables.filter((a) => a.table == model.table)[0]//['idsFromFieldB']
|
|
104
|
+
ids = ids ? ids.idsFromFieldB : []
|
|
105
|
+
|
|
106
|
+
parentTable = script[i-1].model.table
|
|
107
|
+
|
|
108
|
+
// rels
|
|
109
|
+
for(let _relField in model.relations) {
|
|
110
|
+
let array = model.relations[_relField].split('.')
|
|
111
|
+
let _parentTable = array[0]
|
|
112
|
+
let _parentField = array[1].split(' ')[0]
|
|
113
|
+
|
|
114
|
+
binds = binds.length
|
|
115
|
+
? binds
|
|
116
|
+
: script[0].binds
|
|
117
|
+
|
|
118
|
+
if(_parentTable !== parentTable) continue
|
|
119
|
+
|
|
120
|
+
relJoin[_parentTable] = !relJoin[_parentTable]
|
|
121
|
+
? `INNER JOIN ${q(_parentTable)} ON ${q(_parentTable)}.${_parentField} = ${models[modelName].table}.${_relField} `
|
|
122
|
+
: relJoin[_parentTable] + `AND ${q(_parentTable)}.${_parentField} = ${models[modelName].table}.${_relField} `
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let fields = script[0]['fields'].concat((req.onlyFields||{})[model.table] || [])
|
|
128
|
+
|
|
129
|
+
let SQL = sqlSelect(
|
|
130
|
+
model,
|
|
131
|
+
ids,
|
|
132
|
+
{
|
|
133
|
+
select: script[0]['select'],
|
|
134
|
+
where: script[0]['where'],
|
|
135
|
+
orderBy: script[0]['orderBy'],
|
|
136
|
+
limit: script[0]['limit'],
|
|
137
|
+
joins: '',
|
|
138
|
+
fields,
|
|
139
|
+
middlewareParams: req.middleware,
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
if(i > 0)
|
|
144
|
+
for(let ii = i-1; ii>=0; ii--)
|
|
145
|
+
SQL[2] += relJoin[script[ii].model.table]
|
|
146
|
+
|
|
147
|
+
relWhere[model.table] = SQL[3]
|
|
148
|
+
|
|
149
|
+
await dbExec(SQL.join('').trim()+';', binds, {container: modelName})
|
|
150
|
+
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
relations: function(rels, _default = '') {
|
|
158
|
+
rels = rels || _default
|
|
159
|
+
|
|
160
|
+
if(!rels) return this
|
|
161
|
+
|
|
162
|
+
rels = rels.split('.')
|
|
163
|
+
|
|
164
|
+
for(let model of rels)
|
|
165
|
+
if(models[model])
|
|
166
|
+
script.push( { model: models[model], relation: true } )
|
|
167
|
+
else
|
|
168
|
+
(error = `RELATION ${model} NOT FOUND`) && (errorCode = 500)
|
|
169
|
+
|
|
170
|
+
return this
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
select: function(model, ids) {
|
|
174
|
+
|
|
175
|
+
model = (typeof(model)==="string") ? models[model] : model
|
|
176
|
+
|
|
177
|
+
script.push( { model: model, ids: ids } )
|
|
178
|
+
|
|
179
|
+
return this
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
get: async function() {
|
|
183
|
+
|
|
184
|
+
await this.dbEngine()
|
|
185
|
+
|
|
186
|
+
return this
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
find: async function(...values) {
|
|
190
|
+
if(values.length)
|
|
191
|
+
(model.indexes.keys || Object.keys(model.schema)[0])
|
|
192
|
+
.split(",")
|
|
193
|
+
.map((pk, i) => {
|
|
194
|
+
console.log(pk)
|
|
195
|
+
script[0].where.push(`${pk.trim()} = ?`)
|
|
196
|
+
script[0].binds.push(values[i])
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
await this.dbEngine()
|
|
200
|
+
|
|
201
|
+
data[mainContainer] = data[mainContainer][0]
|
|
202
|
+
|
|
203
|
+
return this
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
first: async function() { //TODO auditar
|
|
207
|
+
|
|
208
|
+
await this.dbEngine()
|
|
209
|
+
|
|
210
|
+
if((mainContainer && data[mainContainer]))
|
|
211
|
+
data[mainContainer] = data[mainContainer][0]
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
return this
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
last: async function() {
|
|
218
|
+
|
|
219
|
+
await this.dbEngine()
|
|
220
|
+
|
|
221
|
+
data[mainContainer] = data[mainContainer][data[mainContainer].length-1]
|
|
222
|
+
|
|
223
|
+
return this
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
where: function(where, binds) {
|
|
227
|
+
|
|
228
|
+
script[0]['where'].push(where)
|
|
229
|
+
|
|
230
|
+
if(binds)
|
|
231
|
+
script[0]['binds'] = script[0]['binds'].concat(binds)
|
|
232
|
+
|
|
233
|
+
return this
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
whereIf: function(condition, where, binds) {
|
|
237
|
+
if(condition)
|
|
238
|
+
script[0]['where'].push(where)
|
|
239
|
+
|
|
240
|
+
if(condition && binds)
|
|
241
|
+
script[0]['binds'].push(typeof(binds) === "function" ? binds() : binds)
|
|
242
|
+
|
|
243
|
+
return this
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
binds: function(...params) {
|
|
247
|
+
|
|
248
|
+
params.map(bind=> script[0]['binds'].push(bind))
|
|
249
|
+
|
|
250
|
+
return this
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
orderBy: function(...fields) {
|
|
254
|
+
|
|
255
|
+
script[0]['orderBy'].push(fields.join(', '))
|
|
256
|
+
|
|
257
|
+
return this
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
limit: function(...params) {
|
|
261
|
+
|
|
262
|
+
script[0]['limit'].push(params.join(","))
|
|
263
|
+
|
|
264
|
+
return this
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
fields: function(...fields) {
|
|
268
|
+
|
|
269
|
+
script[0]['fields'] = fields
|
|
270
|
+
|
|
271
|
+
return this
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
fieldsIf: function(condition, fields) {
|
|
275
|
+
|
|
276
|
+
if(condition)
|
|
277
|
+
script[0]['fields'] = fields
|
|
278
|
+
|
|
279
|
+
return this
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
search: function(string = "", fields = [], relevance = [1, 2, 3, 4, 5]) {
|
|
283
|
+
if(!string) return this
|
|
284
|
+
|
|
285
|
+
let where = []
|
|
286
|
+
let orderBy = []
|
|
287
|
+
let words = string.split(" ")
|
|
288
|
+
fields = beeORM.parseArray(fields)
|
|
289
|
+
fields = fields.length ? fields : Object.keys(model.schema)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
fields
|
|
293
|
+
.map((field)=> {
|
|
294
|
+
|
|
295
|
+
orderBy.push(`WHEN ${field} = '${string}' THEN ${relevance[0]} WHEN ${field} LIKE '${string}%' THEN ${relevance[1]} WHEN ${field} LIKE '%${string}%' THEN ${relevance[2]} WHEN ${field} LIKE '%${string.replace(/ /g, "%")}%' THEN ${relevance[3]}`)
|
|
296
|
+
|
|
297
|
+
words
|
|
298
|
+
.map((word, i)=> {
|
|
299
|
+
|
|
300
|
+
where.push(`${field} LIKE '%${word}%'`)
|
|
301
|
+
|
|
302
|
+
if(words.length > 1)
|
|
303
|
+
orderBy.push(`WHEN ${field} = '${word}' THEN ${(relevance[4] * (i+1))} WHEN ${field} LIKE '${word}%' THEN ${(relevance[4] * (i+1)) + relevance[1]} WHEN ${field} LIKE '%${word}%' THEN ${(relevance[4] * (i+1)) + relevance[2]}`)
|
|
304
|
+
})
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
script[0]['where'].push(`(${where.join(' OR ')})`)
|
|
308
|
+
script[0]['orderBy'].unshift(`CASE ${orderBy.join(' ')} ELSE ${100000} END ASC`)
|
|
309
|
+
|
|
310
|
+
return this
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
insert: async function(_data, params = {}) {
|
|
314
|
+
|
|
315
|
+
//data = beeORM.forceData(data, req)
|
|
316
|
+
|
|
317
|
+
if(model.relations && params.relations)
|
|
318
|
+
await Promise
|
|
319
|
+
.all(
|
|
320
|
+
Object
|
|
321
|
+
.keys(model.relations)
|
|
322
|
+
.map(async field => {
|
|
323
|
+
let where = []
|
|
324
|
+
let array = model.relations[field].split(" ")[0].split(".")
|
|
325
|
+
let parentTable = array[0]
|
|
326
|
+
let parentField = array[1]
|
|
327
|
+
let parentRels = global.configs.models[parentTable].relations
|
|
328
|
+
|
|
329
|
+
Object
|
|
330
|
+
.keys(parentRels)
|
|
331
|
+
.map(fieldRel => where.push(`${fieldRel} = '${params.relations[parentTable][fieldRel]}'`))
|
|
332
|
+
|
|
333
|
+
let SQL = `SELECT ${parentField} FROM ${parentTable} WHERE ${where.join(" AND ")}`
|
|
334
|
+
|
|
335
|
+
SQL = await dbExec(SQL)
|
|
336
|
+
|
|
337
|
+
Array.isArray(_data)
|
|
338
|
+
? _data.map(a => a[parentField] = SQL[0][parentField])
|
|
339
|
+
: _data[parentField] = SQL[0][parentField]
|
|
340
|
+
})
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
let onlyFields = script[0].fields.concat((req.onlyFields||{})[model.table] || [])
|
|
344
|
+
let sql = beeORM.sqlInsertUpdate(model, _data, onlyFields, 'INSERT')
|
|
345
|
+
|
|
346
|
+
_data = await dbExec(sql.SQL)
|
|
347
|
+
|
|
348
|
+
inserts.push({..._data, model: model.table})
|
|
349
|
+
this.insertId = _data.insertId
|
|
350
|
+
|
|
351
|
+
return {...this, result: {...sql.result,..._data}}
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
update: async function(data, ids = []) { // TODO criar auditoria
|
|
355
|
+
|
|
356
|
+
data = beeORM.forceData(data, req)
|
|
357
|
+
|
|
358
|
+
let onlyFields = script[0].fields.concat((req.onlyFields||{})[model.table] || [])
|
|
359
|
+
let sql = beeORM.sqlInsertUpdate(model, data, onlyFields, 'UPDATE')
|
|
360
|
+
|
|
361
|
+
sql.SQL += beeORM.modelSqlWhere(model, ids, req.middleware)
|
|
362
|
+
sql.SQL += script[0]['where'].length
|
|
363
|
+
? (!ids.length ? ' WHERE ' : ' AND ') + script[0]['where'].join(" AND ")
|
|
364
|
+
: ''
|
|
365
|
+
|
|
366
|
+
data = await dbExec(sql.SQL, script[0]['binds'])
|
|
367
|
+
|
|
368
|
+
return {...this, result: {...sql.result, ...data}}
|
|
369
|
+
},
|
|
370
|
+
|
|
371
|
+
delete: async function(ids) {
|
|
372
|
+
|
|
373
|
+
const q = beeORM.quote
|
|
374
|
+
|
|
375
|
+
ids = ids && typeof(ids) !== 'object'
|
|
376
|
+
? ids.toString().split(',')
|
|
377
|
+
: ids
|
|
378
|
+
|
|
379
|
+
SQL = 'DELETE FROM ' + q(model.table)
|
|
380
|
+
SQL += beeORM.modelSqlWhere(model, ids, req.middleware)
|
|
381
|
+
|
|
382
|
+
data = await dbExec(SQL)
|
|
383
|
+
|
|
384
|
+
return this
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
table: async function(table, where) {
|
|
388
|
+
|
|
389
|
+
const q = beeORM.quote
|
|
390
|
+
|
|
391
|
+
SQL = 'SELECT * FROM ' + q(table)
|
|
392
|
+
SQL += where ? ` WHERE ${where};` : ''
|
|
393
|
+
|
|
394
|
+
let result = await dbExec(SQL)
|
|
395
|
+
|
|
396
|
+
data[table] = result
|
|
397
|
+
counters[table] = result.length
|
|
398
|
+
mainContainer = table;
|
|
399
|
+
|
|
400
|
+
return this
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
query: async function(SQL, binds = [], container = 'query') {
|
|
404
|
+
|
|
405
|
+
let result = await dbExec(SQL, binds, {container: container})
|
|
406
|
+
|
|
407
|
+
counters[container] = (result) ? result.length : 0
|
|
408
|
+
|
|
409
|
+
return this
|
|
410
|
+
},
|
|
411
|
+
|
|
412
|
+
response: function(sendData = data, action = null, status) {
|
|
413
|
+
|
|
414
|
+
let out = {data: sendData, counters, action, error}
|
|
415
|
+
|
|
416
|
+
if(inserts.length)
|
|
417
|
+
out.inserts = inserts
|
|
418
|
+
|
|
419
|
+
if(global.configs.debug)
|
|
420
|
+
out.debug = debug
|
|
421
|
+
|
|
422
|
+
if(status)
|
|
423
|
+
res.status(status).send(out)
|
|
424
|
+
else
|
|
425
|
+
switch(req.method) {
|
|
426
|
+
case 'DELETE' :
|
|
427
|
+
res.status(action ? errorCode || 200 : 204).json(out)
|
|
428
|
+
break
|
|
429
|
+
case 'POST':
|
|
430
|
+
res.status(errorCode || 201).json(out)
|
|
431
|
+
break
|
|
432
|
+
case 'PUT':
|
|
433
|
+
res.status(errorCode || 200).json(out) // TODO deletar props irrelevantes no output
|
|
434
|
+
break
|
|
435
|
+
default:
|
|
436
|
+
res.status(errorCode || 200).json(out)
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
|
|
440
|
+
responseError: function(error, errorCode) {
|
|
441
|
+
|
|
442
|
+
let out = {error: {message: error || 'an error has occurred'}}
|
|
443
|
+
|
|
444
|
+
if(global.configs.debug)
|
|
445
|
+
out.debug = debug
|
|
446
|
+
|
|
447
|
+
res.status(errorCode || 500).send(out)
|
|
448
|
+
},
|
|
449
|
+
|
|
450
|
+
ifNull(param) {
|
|
451
|
+
return !this.data ? param : this
|
|
452
|
+
},
|
|
453
|
+
}
|
|
454
|
+
)
|
|
452
455
|
}
|