@keltoi/hydra 2.3.9 → 2.5.0
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/index.js +108 -20
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -57,6 +57,22 @@ class Result {
|
|
|
57
57
|
.status(this.#code)
|
|
58
58
|
.json(this.#data);
|
|
59
59
|
}
|
|
60
|
+
|
|
61
|
+
static createInstance = (err)=>
|
|
62
|
+
(err instanceof Result)
|
|
63
|
+
? Promise.reject(err)
|
|
64
|
+
: Promise.reject(
|
|
65
|
+
!!err.message
|
|
66
|
+
? new Result({
|
|
67
|
+
code:500,
|
|
68
|
+
message:err.message
|
|
69
|
+
})
|
|
70
|
+
: new Result({
|
|
71
|
+
code:500,
|
|
72
|
+
data:err,
|
|
73
|
+
message:'Server error'
|
|
74
|
+
})
|
|
75
|
+
)
|
|
60
76
|
}
|
|
61
77
|
|
|
62
78
|
class Entity{
|
|
@@ -91,7 +107,7 @@ class Entity{
|
|
|
91
107
|
if(result.isError) return Promise.reject(result)
|
|
92
108
|
}
|
|
93
109
|
|
|
94
|
-
static
|
|
110
|
+
static collectionFromResult(result=new Result()){
|
|
95
111
|
if(result.isError) return Promise.reject(result)
|
|
96
112
|
}
|
|
97
113
|
}
|
|
@@ -188,15 +204,18 @@ class Linking{
|
|
|
188
204
|
}
|
|
189
205
|
|
|
190
206
|
class Traceable extends Entity{
|
|
207
|
+
#createdAt
|
|
191
208
|
constructor({
|
|
192
209
|
key={},
|
|
193
210
|
struct={},
|
|
194
211
|
createdAt=new Date()
|
|
195
212
|
}){
|
|
196
|
-
super(key,
|
|
213
|
+
super(key,struct);
|
|
214
|
+
|
|
215
|
+
this.#createdAt = createdAt;
|
|
197
216
|
}
|
|
198
217
|
|
|
199
|
-
get createdAt(){ return this
|
|
218
|
+
get createdAt(){ return this.#createdAt }
|
|
200
219
|
|
|
201
220
|
static structMe(
|
|
202
221
|
db=knex(),
|
|
@@ -222,7 +241,7 @@ class Traceable extends Entity{
|
|
|
222
241
|
}
|
|
223
242
|
|
|
224
243
|
class Status extends Entity{
|
|
225
|
-
static build=({ id=1,description='' })=>new Status({id,description})
|
|
244
|
+
static build=({ id=1,description='',data={} })=>new Status({id,description,data})
|
|
226
245
|
|
|
227
246
|
static structMe(
|
|
228
247
|
db=knex(),
|
|
@@ -293,6 +312,10 @@ class Thing extends Entity{
|
|
|
293
312
|
}
|
|
294
313
|
|
|
295
314
|
class Changeable extends Entity{
|
|
315
|
+
#createdAt
|
|
316
|
+
#updatedAt
|
|
317
|
+
#active
|
|
318
|
+
|
|
296
319
|
constructor({
|
|
297
320
|
key={},
|
|
298
321
|
struct = {},
|
|
@@ -302,18 +325,23 @@ class Changeable extends Entity{
|
|
|
302
325
|
}){
|
|
303
326
|
super(
|
|
304
327
|
key,
|
|
305
|
-
|
|
306
|
-
...struct,
|
|
307
|
-
createdAt,
|
|
308
|
-
updatedAt,
|
|
309
|
-
active
|
|
310
|
-
}
|
|
328
|
+
struct
|
|
311
329
|
);
|
|
330
|
+
|
|
331
|
+
this.#createdAt = createdAt;
|
|
332
|
+
this.#updatedAt = updatedAt;
|
|
333
|
+
this.#active = active;
|
|
312
334
|
}
|
|
313
335
|
|
|
314
|
-
get createdAt(){ return this.
|
|
315
|
-
get updatedAt(){ return this.
|
|
316
|
-
get active(){ return this.
|
|
336
|
+
get createdAt(){ return this.createdAt }
|
|
337
|
+
get updatedAt(){ return this.updatedAt }
|
|
338
|
+
get active(){ return this.active }
|
|
339
|
+
|
|
340
|
+
get change(){
|
|
341
|
+
return {
|
|
342
|
+
updatedAt: this.updatedAt
|
|
343
|
+
}
|
|
344
|
+
}
|
|
317
345
|
|
|
318
346
|
static structMe(
|
|
319
347
|
db=knex(),
|
|
@@ -530,12 +558,50 @@ class ChangeableRepository extends Repository$1{
|
|
|
530
558
|
super(entity,context);
|
|
531
559
|
}
|
|
532
560
|
|
|
561
|
+
insert = (entity = new Changeable())=>
|
|
562
|
+
this.myContext()
|
|
563
|
+
.insert({
|
|
564
|
+
...entity.$,
|
|
565
|
+
createdAt:entity.createdAt,
|
|
566
|
+
active:true
|
|
567
|
+
})
|
|
568
|
+
.then(()=>new Result({ data:entity }))
|
|
569
|
+
.catch(err=>Promise.reject( new Result({code:500,message:err}) ))
|
|
570
|
+
|
|
571
|
+
create = (entity = new Changeable())=>
|
|
572
|
+
this.myContext()
|
|
573
|
+
.insert({
|
|
574
|
+
...entity.$,
|
|
575
|
+
active:true
|
|
576
|
+
},
|
|
577
|
+
Object.keys(entity.key)
|
|
578
|
+
)
|
|
579
|
+
.then(ids=>{
|
|
580
|
+
entity.key = ids[0];
|
|
581
|
+
|
|
582
|
+
return new Result({ data:entity })
|
|
583
|
+
})
|
|
584
|
+
.catch(err=>Promise.reject( new Result({code:500,message:err}) ))
|
|
585
|
+
|
|
586
|
+
update = (entity = new Changeable())=>
|
|
587
|
+
this.myContext()
|
|
588
|
+
.where(entity.key)
|
|
589
|
+
.update({
|
|
590
|
+
...entity.data,
|
|
591
|
+
updatedAt:new Date()
|
|
592
|
+
})
|
|
593
|
+
.then(affected=> affected > 0
|
|
594
|
+
? new Result({ code:200,data:`${this.name} updated` })
|
|
595
|
+
: new Result({ code:404,message:'Not found' })
|
|
596
|
+
)
|
|
597
|
+
.catch(err=>Promise.reject( new Result({code:500,message:err}) ))
|
|
598
|
+
|
|
533
599
|
reactive = (entity = new Changeable())=>
|
|
534
600
|
this.myContext()
|
|
535
601
|
.where(entity.key)
|
|
536
602
|
.update({
|
|
537
603
|
active:true,
|
|
538
|
-
updatedAt:
|
|
604
|
+
updatedAt:new Date()
|
|
539
605
|
})
|
|
540
606
|
.then(affected=> affected > 0
|
|
541
607
|
? new Result({ code:200,data:`${this.name} updated` })
|
|
@@ -548,7 +614,7 @@ class ChangeableRepository extends Repository$1{
|
|
|
548
614
|
.where(entity.key)
|
|
549
615
|
.update({
|
|
550
616
|
active:false,
|
|
551
|
-
updatedAt:
|
|
617
|
+
updatedAt:new Date()
|
|
552
618
|
})
|
|
553
619
|
.then(affected=> affected > 0
|
|
554
620
|
? new Result({ code:200,data:`${this.name} updated` })
|
|
@@ -640,6 +706,31 @@ class TraceableRepository extends Repository$1{
|
|
|
640
706
|
.then(result => new Result({data:result}))
|
|
641
707
|
.catch(err=>Promise.reject( new Result({code:500,message:err})) )
|
|
642
708
|
|
|
709
|
+
insert = (entity = new Traceable()) =>
|
|
710
|
+
this.myContext()
|
|
711
|
+
.insert({
|
|
712
|
+
...entity.$,
|
|
713
|
+
createdAt:entity.createdAt
|
|
714
|
+
})
|
|
715
|
+
.then(()=>new Result({ data:entity }))
|
|
716
|
+
.catch(err=>Promise.reject( new Result({code:500,message:err})) )
|
|
717
|
+
|
|
718
|
+
create = (entity = new Traceable()) =>
|
|
719
|
+
this.myContext()
|
|
720
|
+
.insert(
|
|
721
|
+
{
|
|
722
|
+
...entity.$,
|
|
723
|
+
createdAt:new Date()
|
|
724
|
+
},
|
|
725
|
+
Object.keys(entity.key)
|
|
726
|
+
)
|
|
727
|
+
.then(ids=>{
|
|
728
|
+
entity.key = ids[0];
|
|
729
|
+
|
|
730
|
+
return new Result({ data:entity })
|
|
731
|
+
})
|
|
732
|
+
.catch(err=>Promise.reject( new Result({code:500,message:err})) )
|
|
733
|
+
|
|
643
734
|
update = () => Promise.reject( new Result({code:400,message:'Cannot update a logged object'}) )
|
|
644
735
|
|
|
645
736
|
delete = () => Promise.reject( new Result({code:400,message:'Cannot delete a logged object'}) )
|
|
@@ -785,8 +876,7 @@ class Service {
|
|
|
785
876
|
|
|
786
877
|
get context(){return this.#context}
|
|
787
878
|
|
|
788
|
-
handleError = (code,message)=>Promise.reject(new Result({code,message}))
|
|
789
|
-
handleFailure = (err)=> this.handleError({code:500,message:err.message})
|
|
879
|
+
handleError = ({code,message})=> Promise.reject(new Result({code,message}))
|
|
790
880
|
}
|
|
791
881
|
|
|
792
882
|
class Handler {
|
|
@@ -800,9 +890,7 @@ class Handler {
|
|
|
800
890
|
}
|
|
801
891
|
|
|
802
892
|
handle(){}
|
|
803
|
-
handleError({ code, message }) {
|
|
804
|
-
return Promise.reject(new Result({ code, message }));
|
|
805
|
-
}
|
|
893
|
+
handleError = ({ code, message }) => Promise.reject(new Result({ code, message }));
|
|
806
894
|
}
|
|
807
895
|
|
|
808
896
|
let Context$1 = class Context{
|