@keltoi/hydra 1.1.8 → 1.2.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 +1 -1
- package/package.json +1 -1
- package/src/helper/runWhenFalse.js +7 -0
- package/src/helper/runWhenTrue.js +7 -0
- package/src/index.js +5 -1
- package/src/model/linking.js +25 -15
- package/src/model/logged.js +9 -4
- package/src/model/status.js +6 -2
- package/src/model/thing.js +8 -3
- package/src/model/traced.js +11 -20
- package/src/repository/db/context.js +3 -7
- package/src/repository/db/index.js +30 -26
- package/src/repository/db/linked.js +39 -28
- package/src/repository/db/loggedRepository.js +47 -45
- package/src/repository/db/thingRepository.js +7 -6
- package/src/repository/db/tracedRepository.js +79 -29
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("knex"),t=require("axios");class Model{#e;constructor(e={}){this.#e=e}get key(){return this.#e}set key(e={}){this.#e=e}get entity(){return{}}static build(e={}){return new Model(e)}}class Logged extends Model{#t=new Date;constructor({key:e={},createdAt:t=new Date}){super(e),this.#t=t}get createdAt(){return this.#t}set createdAt(e=new Date){this.createdAt=e}static makeMe(t=e(),s=Logged,n=(t=new e.TableBuilder)=>{}){return t.schema.createTable(s.name,(e=>{n(e),e.dateTime("createdAt").notNullable().defaultTo(t.fn.now())}))}}class Traced extends Logged{#s=void 0|new Date;#n=!0;constructor({key:e={},createdAt:t=new Date,updatedAt:s=void 0|new Date,active:n=!0}){super({key:e,createdAt:t}),this.#n=n,this.#s=s}get updatedAt(){return this.#s}set updatedAt(e=new Date){this.#s=e}get active(){return this.#n}set active(e=!0){this.#n=e}tickUpdateNow(){this.#s=new Date}static makeMe(t=e(),s=Traced,n=(t=new e.TableBuilder)=>{}){return
|
|
1
|
+
"use strict";var e=require("knex"),t=require("axios");class Model{#e;constructor(e={}){this.#e=e}get key(){return this.#e}set key(e={}){this.#e=e}get entity(){return{}}static build(e={}){return new Model(e)}}var runWhenFalse=(e=Promise.resolve(!0),t=async()=>{})=>e.then((async e=>{e||await t()}));class Logged extends Model{#t=new Date;constructor({key:e={},createdAt:t=new Date}){super(e),this.#t=t}get createdAt(){return this.#t}set createdAt(e=new Date){this.createdAt=e}static makeMe(t=e(),s=Logged,n=(t=new e.TableBuilder)=>{}){return runWhenFalse(t.schema.hasTable(s.name),(()=>t.schema.createTable(s.name,(e=>{n(e),e.dateTime("createdAt").notNullable().defaultTo(t.fn.now())}))))}}class Traced extends Logged{#s=void 0|new Date;#n=!0;constructor({key:e={},createdAt:t=new Date,updatedAt:s=void 0|new Date,active:n=!0}){super({key:e,createdAt:t}),this.#n=n,this.#s=s}get updatedAt(){return this.#s}set updatedAt(e=new Date){this.#s=e}get active(){return this.#n}set active(e=!0){this.#n=e}tickUpdateNow(){this.#s=new Date}static makeMe(t=e(),s=Traced,n=(t=new e.TableBuilder)=>{}){return super.makeMe(t,s,(e=>{n(e),e.dateTime("updatedAt").nullable(),e.boolean("active").defaultTo(!0)}))}}class Linking{#r=new Model;#i=new Model;#a=Model;#o=Model;get Abscissa(){return this.#a}get Ordinate(){return this.#o}constructor(e=Model,t=Model,{abscissa:s=new e,ordinate:n=new t}){this.#a=e,this.#o=t,this.#r=s,this.#i=n}static build=({AbscissaModel:e=Model,OrdinateModel:t=Model,abscissa:s=new Model,ordinate:n=new Model})=>new Linking(e,t,{abscissa:s,ordinate:n});static makeMe(t=e(),s=Model,n=Model,r=(t=new e.TableBuilder)=>{}){const i=`${s.name}${n.name}`;return runWhenFalse(t.schema.hasTable(i),(()=>t.schema.createTable(i,(e=>{r(e)}))))}get abscissaKey(){return{idAbscissa:this.#r.key}}get ordinateKey(){return{idOrdinate:this.#i.key}}get key(){return{...this.abscissaKey,...this.ordinateKey}}get entity(){}get abscissa(){return this.#r}set abscissa(e=this.Abscissa.build()){this.#r=e}get ordinate(){return this.#i}set ordinate(e=this.Ordinate.build()){this.ordinate=e}}class Thing extends Model{#d;constructor({key:e={},name:t=""}){super(e),this.#d=t}get name(){return this.#d}set name(e=""){this.#d=e}get entity(){return{name:this.#d}}static makeMe(t=e(),s=Thing,n=255,r=(t=new e.TableBuilder)=>{}){return runWhenFalse(t.schema.hasTable(s.name),(()=>t.schema.createTable(s.name,(e=>{r(e),e.string(n).notNullable()}))))}}let s=class Context{#c;constructor(t=e()){this.#c=t}get db(){return this.#c}unitOfWork(...e){return this.#c.transaction().then((t=>{e.forEach((e=>e.context=t));const clean=()=>e.forEach((e=>e.resetContext()));return{done:()=>t.commit().then(clean),rollback:()=>t.rollback().then(clean)}}))}static instance(t=e()){return new Context(t)}async terraform(e=[]){const t=e.map((async e=>e.makeMe(this.#c)));console.log("Terraforming..."),await Promise.all(t)}},n=class Repository{#d="";myContext;Entity=Model;static anyOrError(e,t={code:0,message:""}){if(e)return e;throw t}static setOrEmpty(e=[],t=e=>e){return e.length?e.map(t):[]}constructor(e=Model,t=new s){this.#d=e.name,this.myContext=()=>t.db(this.#d),this.resetContext=()=>{this.myContext=()=>t.db(this.#d)},this.Entity=e}set context(t=e()){this.myContext=()=>t(this.#d)}create=(e=new Model)=>this.myContext().insert({...e.key,...e.entity}).then((()=>e));insert=(e=new Model)=>this.myContext().insert(e.entity,Object.keys(e.key)).then((t=>(e.key=t[0],e)));update=(e=new Model)=>this.myContext().where(e.key).update(e.entity).then((e=>e>0));delete=(e=new Model)=>this.myContext().where(e.key).del().then((e=>e>0));get=(e=new Model)=>this.myContext().where(e.key).first().then((e=>Repository.anyOrError(e,{code:404,message:"Not found"}))).then(this.Entity.build);list=()=>this.myContext().select().then((e=>Repository.setOrEmpty(e,this.Entity.build)))};class Context{#h;constructor(e=t.create({})){this.#h=e}get http(){return this.#h}}class Repository{#y;constructor(e=Model,t=new Context){this.#y=t,this.modelInstance=(t={})=>new e(t)}static queryString(e={}){return"?"+Object.entries(e).map((([e,t])=>`${e}=${t}`)).join("&")}get context(){return this.#y}}exports.ApiContext=Context,exports.ApiRepository=Repository,exports.ApiRestRepository=class RestRepository extends Repository{constructor(e=Model,t=new Context){super(e,t)}#l=(e="",t=void 0|{})=>this.context.http.get(t?`${e}${Repository.queryString(t)}`:e);get=(e="",t=void 0|{})=>this.#l(e,t).then((e=>this.modelInstance(e)));list=(e="",t=void 0|{})=>this.#l(e,t).then(((e=[])=>e.map((e=>this.modelInstance(e)))));create=(e="",t=new Model)=>this.context.http.post(e,t.entity);update=(e="",t=new Model)=>this.context.http.put(e,t.entity);change=(e="",t=new Model)=>this.context.http.patch(e,t.entity);delete=(e,t=new Model)=>this.context.http.delete(`/${e}`)},exports.DbContext=s,exports.DbLinked=class DbLinked{#m;#d="";myContext;constructor(e=Linking,t=new s){this.#d=e.name,this.myContext=()=>t.db(this.#d),this.#m=e}createBatch=(e=[new Linking])=>this.myContext().insert(e.map((e=>({...e.key,...e.entity})))).then((()=>e));create=(e=new Linking)=>this.myContext().insert({...e.key,...e.entity}).then((()=>e));insertOrdinatesByAbscissa=({abscissa:e=new Model,ordinates:t=[new Model]})=>{const s=t.map((t=>this.#m.build({abscissa:e,ordinate:t})));return this.createBatch(s)};insertAbscissasByOrdinate=({ordinate:e=new Model,abscissas:t=[new Model]})=>{const s=t.map((t=>this.#m.build({abscissa:t,ordinate:e})));return this.createBatch(s)};delete=(e=new Linking)=>this.myContext().where(e.key).del().then((e=>e>0));getAbscissasByOrdinate(e=new Linking){const t=e.Abscissa,s=e.Ordinate,n=Object.keys(e.abscissaKey),r=Object.keys(e.abscissa.key),i=n.map((e=>`${t.name}${s.name}.${e}`)),a=r.map((e=>`${t.name}.${e}`));return this.myContext().where(e.ordinateKey).select(`${t.name}.*`).join(t.name,(e=>i.forEach(((t,s)=>{e.on(t,a[s])})))).then((e=>e.map((e=>t.build(e)))))}getOrdinatesByAbscissa(e=new Linking){const t=e.Ordinate,s=e.Abscissa,n=Object.keys(e.ordinateKey),r=Object.keys(e.ordinate.key),i=n.map((e=>`${s.name}${t.name}.${e}`)),a=r.map((e=>`${t.name}.${e}`));return this.myContext().where(e.abscissaKey).select(`${t.name}.*`).join(t.name,(e=>i.forEach(((t,s)=>{e.on(t,a[s])})))).then((e=>e.map((e=>t.build(e)))))}},exports.DbLoggedRepository=class LoggedRepository extends n{constructor(e=Logged,t=new s){super(e,t)}get=(e=new Logged)=>this.myContext().where({...e.key}).first().then((e=>n.anyOrError(e,{code:404,message:"Not found"}))).then(this.Entity.build);before=(e=new Date,t="asc")=>this.myContext().where("createdAt","<",e.toISOString()).select().orderBy("createdAt",t).then((e=>n.setOrEmpty(e,this.Entity.build)));after=(e=new Date,t="asc")=>this.myContext().where("createdAt",">",e.toISOString()).select().orderBy("createdAt",t).then((e=>n.setOrEmpty(e,this.Entity.build)));list=(e="asc")=>this.myContext().select().orderBy("createdAt",e).then((e=>n.setOrEmpty(e,this.Entity.build)));insert=(e=new Logged)=>this.myContext().insert({...e.entity,createdAt:(new Date).toISOString()},Object.keys(e.key)).then((t=>e.key=t[0])).then((()=>e));create=(e=new Logged)=>this.myContext().insert({...e.entity,createdAt:(new Date).toISOString()}).then((()=>e));update=()=>Promise.reject(new Error("Cannot update a logged object"));delete=()=>Promise.reject(new Error("Cannot delete a logged object"))},exports.DbRepository=n,exports.DbThingRepository=class ThingRepository extends n{constructor(e=Thing,t=new s){super(e,t)}getByName=(e="")=>this.myContext().where({name:e}).select().then((e=>n.setOrEmpty(e,this.Entity.build)))},exports.DbTracedRepository=class TracedRepository extends n{constructor(e=Traced,t=new s){super(e,t)}deceased=(e="asc")=>this.myContext().where({active:!1}).select().orderBy(["createdAt","updatedAt"],e).then((e=>n.setOrEmpty(e,this.Entity.build)));before=(e=new Date,t="asc")=>this.myContext().where("createdAt","<",e.toISOString()).where({active:!0}).select().orderBy(["createdAt","updatedAt"],t).then((e=>n.setOrEmpty(e,this.Entity.build)));after=(e=new Date,t="asc")=>this.myContext().where("createdAt",">",e.toISOString()).where({active:!0}).select().orderBy(["createdAt","updatedAt"],t).then((e=>n.setOrEmpty(e,this.Entity.build)));list=(e="asc")=>this.myContext().where({active:!0}).select().orderBy(["createdAt","updatedAt"],e).then((e=>n.setOrEmpty(e,this.Entity.build)));last=()=>this.myContext().where({active:!0}).first().orderBy("createdAt","desc").then((e=>n.anyOrError(e,{code:404,message:"Not found"})));first=()=>this.myContext().where({active:!0}).first().orderBy("createdAt","asc").then((e=>n.anyOrError(e,{code:404,message:"Not found"})));insert=(e=new Traced)=>this.myContext().insert({...e.entity,createdAt:(new Date).toISOString()},Object.keys(e.key)).then((t=>e.key=t[0])).then((()=>e));create=(e=new Traced)=>this.myContext().insert({...e.key,...e.entity,createdAt:(new Date).toISOString()}).then((()=>e));update=(e=new Traced)=>this.myContext().where(e.key).update({...e.entity,updatedAt:(new Date).toISOString()}).then((e=>e>0));delete=(e=new Traced)=>this.myContext().where(e.key).update({active:!1,updatedAt:(new Date).toISOString()}).then((e=>e>0))},exports.Handler=class Handler{#y;constructor({context:e=new s}){this.#y=e}get context(){return this.#y}handle(){}handleError({code:e,message:t}){return Promise.reject({code:e,message:t})}},exports.Linking=Linking,exports.Logged=Logged,exports.Model=Model,exports.Service=class Service{#y;constructor(e=new s){this.#y=e}get context(){return this.#y}handleError=(e,t)=>Promise.reject({code:e,message:t});handleFailure=e=>this.handleError({code:500,message:e.message})},exports.Thing=Thing,exports.Traced=Traced,exports.runWhenFalse=runWhenFalse,exports.runWhenTrue=(e=Promise.resolve(!0),t=async()=>{})=>e.then((async e=>{e&&await t()}));
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -14,6 +14,8 @@ import ApiRepository from './repository/api/index.js'
|
|
|
14
14
|
import ApiRestRepository from './repository/api/restRepository.js'
|
|
15
15
|
import Service from './service/index.js'
|
|
16
16
|
import Handler from './handler/index.js'
|
|
17
|
+
import runWhenTrue from './helper/runWhenTrue.js'
|
|
18
|
+
import runWhenFalse from './helper/runWhenFalse.js'
|
|
17
19
|
|
|
18
20
|
export {
|
|
19
21
|
Model,
|
|
@@ -31,5 +33,7 @@ export {
|
|
|
31
33
|
ApiRepository,
|
|
32
34
|
ApiRestRepository,
|
|
33
35
|
Service,
|
|
34
|
-
Handler
|
|
36
|
+
Handler,
|
|
37
|
+
runWhenTrue,
|
|
38
|
+
runWhenFalse
|
|
35
39
|
}
|
package/src/model/linking.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import knex, { TableBuilder } from "knex"
|
|
2
2
|
import Model from "./index"
|
|
3
|
+
import runWhenFalse from "../helper/runWhenFalse"
|
|
3
4
|
|
|
4
5
|
export default class Linking{
|
|
5
6
|
#abscissa=new Model()
|
|
@@ -16,17 +17,12 @@ export default class Linking{
|
|
|
16
17
|
return this.#OrdinateModel
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
static build=({
|
|
20
|
-
abscissa=new Model(),
|
|
21
|
-
ordinate=new Model()
|
|
22
|
-
})=>new Linking(Model,Model,{abscissa,ordinate})
|
|
23
|
-
|
|
24
20
|
constructor(
|
|
25
21
|
AbscissaModel=Model,
|
|
26
22
|
OrdinateModel=Model,
|
|
27
23
|
{
|
|
28
|
-
abscissa = AbscissaModel
|
|
29
|
-
ordinate = OrdinateModel
|
|
24
|
+
abscissa = new AbscissaModel(),
|
|
25
|
+
ordinate = new OrdinateModel()
|
|
30
26
|
})
|
|
31
27
|
{
|
|
32
28
|
this.#AbscissaModel = AbscissaModel
|
|
@@ -36,27 +32,39 @@ export default class Linking{
|
|
|
36
32
|
this.#ordinate = ordinate
|
|
37
33
|
}
|
|
38
34
|
|
|
35
|
+
static build=({
|
|
36
|
+
AbscissaModel=Model,
|
|
37
|
+
OrdinateModel=Model,
|
|
38
|
+
abscissa=new Model(),
|
|
39
|
+
ordinate=new Model()
|
|
40
|
+
})=>new Linking(AbscissaModel,OrdinateModel,{abscissa,ordinate})
|
|
41
|
+
|
|
39
42
|
static makeMe(
|
|
40
43
|
db=knex(),
|
|
41
44
|
abscissa=Model,
|
|
42
45
|
ordinate=Model,
|
|
43
|
-
schema=(t=new
|
|
46
|
+
schema=(t=new TableBuilder())=>{}
|
|
44
47
|
){
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
const tableName = `${abscissa.name}${ordinate.name}`
|
|
49
|
+
|
|
50
|
+
return runWhenFalse(
|
|
51
|
+
db.schema.hasTable(tableName),
|
|
52
|
+
() => db.schema.createTable(
|
|
53
|
+
tableName,
|
|
48
54
|
table=>{
|
|
49
55
|
schema(table)
|
|
50
56
|
}
|
|
51
57
|
)
|
|
58
|
+
)
|
|
52
59
|
}
|
|
53
60
|
|
|
61
|
+
|
|
54
62
|
get abscissaKey(){
|
|
55
|
-
return { idAbscissa: this.#abscissa.key
|
|
63
|
+
return { idAbscissa: this.#abscissa.key }
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
get ordinateKey(){
|
|
59
|
-
return { idOrdinate: this.#ordinate.key
|
|
67
|
+
return { idOrdinate: this.#ordinate.key }
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
get key(){
|
|
@@ -66,10 +74,12 @@ export default class Linking{
|
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
get entity(){}
|
|
78
|
+
|
|
69
79
|
get abscissa(){ return this.#abscissa }
|
|
70
|
-
set abscissa(value =
|
|
80
|
+
set abscissa(value = this.Abscissa.build()){ this.#abscissa = value }
|
|
71
81
|
|
|
72
82
|
get ordinate(){ return this.#ordinate }
|
|
73
|
-
set ordinate(value =
|
|
83
|
+
set ordinate(value = this.Ordinate.build()){ this.ordinate = value }
|
|
74
84
|
|
|
75
85
|
}
|
package/src/model/logged.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex"
|
|
1
|
+
import knex, { Knex, TableBuilder } from "knex"
|
|
2
2
|
import Model from "./index.js"
|
|
3
|
+
import createTableIfNotExists from "../helper/runWhenTrue.js"
|
|
4
|
+
import runWhenTrue from "../helper/runWhenTrue.js"
|
|
5
|
+
import runWhenFalse from "../helper/runWhenFalse.js"
|
|
3
6
|
|
|
4
7
|
export default class Logged extends Model{
|
|
5
8
|
#createdAt=new Date()
|
|
@@ -10,7 +13,7 @@ export default class Logged extends Model{
|
|
|
10
13
|
}){
|
|
11
14
|
super(key)
|
|
12
15
|
this.#createdAt = createdAt
|
|
13
|
-
}
|
|
16
|
+
}
|
|
14
17
|
|
|
15
18
|
get createdAt(){ return this.#createdAt }
|
|
16
19
|
set createdAt(value = new Date()){ this.createdAt = value }
|
|
@@ -20,8 +23,9 @@ export default class Logged extends Model{
|
|
|
20
23
|
model=Logged,
|
|
21
24
|
schema=(t=new TableBuilder())=>{}
|
|
22
25
|
){
|
|
23
|
-
return
|
|
24
|
-
.
|
|
26
|
+
return runWhenFalse(
|
|
27
|
+
db.schema.hasTable(model.name),
|
|
28
|
+
()=>db.schema.createTable(
|
|
25
29
|
model.name,
|
|
26
30
|
table=>{
|
|
27
31
|
schema(table)
|
|
@@ -31,5 +35,6 @@ export default class Logged extends Model{
|
|
|
31
35
|
.defaultTo(db.fn.now())
|
|
32
36
|
}
|
|
33
37
|
)
|
|
38
|
+
)
|
|
34
39
|
}
|
|
35
40
|
}
|
package/src/model/status.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import knex, { TableBuilder } from "knex"
|
|
2
2
|
import Model from "."
|
|
3
|
+
import runWhenTrue from "../helper/runWhenTrue"
|
|
4
|
+
import runWhenFalse from "../helper/runWhenFalse"
|
|
3
5
|
|
|
4
6
|
export default class Status extends Model{
|
|
5
7
|
#description=''
|
|
@@ -11,8 +13,9 @@ export default class Status extends Model{
|
|
|
11
13
|
model=Status,
|
|
12
14
|
schema=(t=new TableBuilder())=>{}
|
|
13
15
|
){
|
|
14
|
-
return
|
|
15
|
-
.
|
|
16
|
+
return runWhenFalse(
|
|
17
|
+
db.schema.hasTable(model.name),
|
|
18
|
+
()=>db.schema.createTable(
|
|
16
19
|
model.name,
|
|
17
20
|
table=>{
|
|
18
21
|
table.increments()
|
|
@@ -25,6 +28,7 @@ export default class Status extends Model{
|
|
|
25
28
|
schema(table)
|
|
26
29
|
}
|
|
27
30
|
)
|
|
31
|
+
)
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
constructor({ id=1, description=''}){
|
package/src/model/thing.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import knex, { TableBuilder } from "knex";
|
|
2
2
|
import Model from "./index.js";
|
|
3
|
+
import runWhenFalse from "../helper/runWhenFalse.js";
|
|
4
|
+
import runWhenTrue from "../helper/runWhenTrue.js";
|
|
3
5
|
|
|
4
6
|
|
|
5
7
|
export default class Thing extends Model{
|
|
@@ -22,17 +24,20 @@ export default class Thing extends Model{
|
|
|
22
24
|
static makeMe(
|
|
23
25
|
db=knex(),
|
|
24
26
|
thing=Thing,
|
|
27
|
+
size = 255,
|
|
25
28
|
schema=(t=new TableBuilder())=>{}
|
|
26
29
|
){
|
|
27
|
-
return
|
|
28
|
-
.
|
|
30
|
+
return runWhenFalse(
|
|
31
|
+
db.schema.hasTable(thing.name),
|
|
32
|
+
()=>db.schema.createTable(
|
|
29
33
|
thing.name,
|
|
30
34
|
table=>{
|
|
31
35
|
schema(table)
|
|
32
36
|
|
|
33
|
-
table.string(
|
|
37
|
+
table.string(size)
|
|
34
38
|
.notNullable()
|
|
35
39
|
}
|
|
36
40
|
)
|
|
41
|
+
)
|
|
37
42
|
}
|
|
38
43
|
}
|
package/src/model/traced.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import knex, { TableBuilder } from "knex"
|
|
2
|
-
import Model from "./index.js"
|
|
3
2
|
import Logged from "./logged.js"
|
|
4
3
|
|
|
5
4
|
export default class Traced extends Logged{
|
|
@@ -10,8 +9,8 @@ export default class Traced extends Logged{
|
|
|
10
9
|
key={},
|
|
11
10
|
createdAt=new Date(),
|
|
12
11
|
updatedAt=new Date()|undefined,
|
|
13
|
-
active=true
|
|
14
|
-
){
|
|
12
|
+
active=true
|
|
13
|
+
}){
|
|
15
14
|
super({ key,createdAt })
|
|
16
15
|
this.#active=active
|
|
17
16
|
this.#updatedAt = updatedAt
|
|
@@ -32,22 +31,14 @@ export default class Traced extends Logged{
|
|
|
32
31
|
model=Traced,
|
|
33
32
|
schema=(t=new TableBuilder())=>{}
|
|
34
33
|
){
|
|
35
|
-
return db
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
table.dateTime('updatedAt')
|
|
46
|
-
.nullable()
|
|
47
|
-
|
|
48
|
-
table.boolean('active')
|
|
49
|
-
.defaultTo(true)
|
|
50
|
-
}
|
|
51
|
-
)
|
|
34
|
+
return super.makeMe(db,model,table=>{
|
|
35
|
+
schema(table)
|
|
36
|
+
|
|
37
|
+
table.dateTime('updatedAt')
|
|
38
|
+
.nullable()
|
|
39
|
+
|
|
40
|
+
table.boolean('active')
|
|
41
|
+
.defaultTo(true)
|
|
42
|
+
})
|
|
52
43
|
}
|
|
53
44
|
}
|
|
@@ -27,13 +27,9 @@ export default class Context{
|
|
|
27
27
|
static instance(database=knex()){ return new Context(database) }
|
|
28
28
|
|
|
29
29
|
async terraform(models=[]){
|
|
30
|
-
const promises = models.map(async model=>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.hasTable(model.name)
|
|
34
|
-
|
|
35
|
-
if (!exists) await model.makeMe(this.#db)
|
|
36
|
-
})
|
|
30
|
+
const promises = models.map(async model=>model.makeMe(this.#db))
|
|
31
|
+
|
|
32
|
+
console.log('Terraforming...')
|
|
37
33
|
|
|
38
34
|
await Promise.all(promises)
|
|
39
35
|
}
|
|
@@ -8,6 +8,8 @@ export default class Repository{
|
|
|
8
8
|
|
|
9
9
|
myContext
|
|
10
10
|
|
|
11
|
+
Entity = Model
|
|
12
|
+
|
|
11
13
|
static anyOrError(model,err={code:0,message:''}){
|
|
12
14
|
if (!!model) return model
|
|
13
15
|
|
|
@@ -28,46 +30,48 @@ export default class Repository{
|
|
|
28
30
|
this.myContext=()=>context.db(this.#name)
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
this.
|
|
33
|
+
this.Entity = model
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
set context(value=knex()){ this.myContext=()=>value(this.#name) }
|
|
35
37
|
|
|
36
|
-
create = (model=new Model())=>
|
|
38
|
+
create = (model = new Model())=>
|
|
37
39
|
this.myContext()
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
.insert({
|
|
41
|
+
...model.key,
|
|
42
|
+
...model.entity
|
|
43
|
+
})
|
|
44
|
+
.then(()=>model)
|
|
43
45
|
|
|
44
|
-
insert = (model=new Model())=>
|
|
46
|
+
insert = (model = new Model())=>
|
|
45
47
|
this.myContext()
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
.insert(model.entity,Object.keys(model.key))
|
|
49
|
+
.then(ids=>{
|
|
50
|
+
model.key = ids[0]
|
|
51
|
+
return model
|
|
52
|
+
})
|
|
49
53
|
|
|
50
|
-
update = (model=new Model())=>
|
|
54
|
+
update = (model = new Model())=>
|
|
51
55
|
this.myContext()
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
.where(model.key)
|
|
57
|
+
.update(model.entity)
|
|
58
|
+
.then(affected=>affected > 0)
|
|
55
59
|
|
|
56
|
-
delete = (
|
|
60
|
+
delete = (model = new Model())=>
|
|
57
61
|
this.myContext()
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
.where(model.key)
|
|
63
|
+
.del()
|
|
64
|
+
.then(affected=>affected > 0)
|
|
61
65
|
|
|
62
|
-
get = (
|
|
66
|
+
get = (model = new Model())=>
|
|
63
67
|
this.myContext()
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
.where(model.key)
|
|
69
|
+
.first()
|
|
70
|
+
.then(model=>Repository.anyOrError(model,{code:404,message:'Not found'}))
|
|
71
|
+
.then(this.Entity.build)
|
|
68
72
|
|
|
69
73
|
list = ()=>
|
|
70
74
|
this.myContext()
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
.select()
|
|
76
|
+
.then(result=>Repository.setOrEmpty(result,this.Entity.build))
|
|
73
77
|
}
|
|
@@ -12,58 +12,67 @@ export default class DbLinked {
|
|
|
12
12
|
this.#name = link.name
|
|
13
13
|
this.myContext = () => context.db(this.#name)
|
|
14
14
|
this.#linking = link
|
|
15
|
+
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
createBatch = (linkings=[ new Linking() ])=>
|
|
18
19
|
this.myContext()
|
|
19
20
|
.insert(
|
|
20
|
-
linkings.map(l =>
|
|
21
|
+
linkings.map(l => (
|
|
22
|
+
{
|
|
23
|
+
...l.key,
|
|
24
|
+
...l.entity
|
|
25
|
+
})
|
|
26
|
+
)
|
|
21
27
|
)
|
|
22
28
|
.then(()=>linkings)
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
create = (linked=new Linking())=>
|
|
31
|
+
this.myContext()
|
|
32
|
+
.insert({
|
|
33
|
+
...linked.key,
|
|
34
|
+
...linked.entity
|
|
35
|
+
})
|
|
36
|
+
.then(()=>linked);
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
insertOrdinatesByAbscissa = ({ abscissa=new Model(),ordinates=[new Model()] })=>{
|
|
33
40
|
const batch = ordinates
|
|
34
41
|
.map(o=>this.#linking
|
|
35
|
-
.build({abscissa,o})
|
|
42
|
+
.build({abscissa,ordinate:o})
|
|
36
43
|
)
|
|
37
44
|
|
|
38
|
-
return this
|
|
45
|
+
return this.createBatch(batch);
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
insertAbscissasByOrdinate=(ordinate=new Model(),abscissas=[new Model()])=>{
|
|
48
|
+
insertAbscissasByOrdinate = ({ ordinate=new Model(),abscissas=[new Model()] })=>{
|
|
42
49
|
const batch = abscissas
|
|
43
50
|
.map(a=>this.#linking
|
|
44
|
-
.build({a,ordinate})
|
|
51
|
+
.build({abscissa:a,ordinate})
|
|
45
52
|
)
|
|
46
53
|
|
|
47
|
-
return this
|
|
54
|
+
return this.createBatch(batch)
|
|
48
55
|
}
|
|
49
56
|
|
|
50
|
-
delete=(linked=new Linking())=>
|
|
51
|
-
|
|
52
|
-
.where(linked.key
|
|
57
|
+
delete = (linked=new Linking())=>
|
|
58
|
+
this.myContext()
|
|
59
|
+
.where(linked.key)
|
|
53
60
|
.del()
|
|
54
61
|
.then(affected=>affected > 0)
|
|
55
|
-
|
|
62
|
+
|
|
56
63
|
|
|
57
|
-
|
|
64
|
+
getAbscissasByOrdinate(linked=new Linking()){
|
|
58
65
|
const Abscissa = linked.Abscissa
|
|
66
|
+
const Ordinate = linked.Ordinate
|
|
59
67
|
const keys = Object.keys(linked.abscissaKey)
|
|
68
|
+
const abscissaKey = Object.keys(linked.abscissa.key)
|
|
60
69
|
|
|
61
|
-
const fields = keys.map(key=>`${
|
|
62
|
-
const abscissas =
|
|
70
|
+
const fields = keys.map(key=>`${Abscissa.name}${Ordinate.name}.${key}`)
|
|
71
|
+
const abscissas = abscissaKey.map(key=>`${Abscissa.name}.${key}`)
|
|
63
72
|
|
|
64
73
|
return this.myContext()
|
|
65
74
|
.where(linked.ordinateKey)
|
|
66
|
-
.select(
|
|
75
|
+
.select(`${Abscissa.name}.*`)
|
|
67
76
|
.join(Abscissa.name,clause=>
|
|
68
77
|
fields.forEach((field,i)=>{
|
|
69
78
|
clause.on(field,abscissas[i])
|
|
@@ -74,16 +83,18 @@ export default class DbLinked {
|
|
|
74
83
|
)
|
|
75
84
|
}
|
|
76
85
|
|
|
77
|
-
|
|
86
|
+
getOrdinatesByAbscissa(linked=new Linking()){
|
|
78
87
|
const Ordinate = linked.Ordinate
|
|
88
|
+
const Abscissa = linked.Abscissa
|
|
79
89
|
const keys = Object.keys(linked.ordinateKey)
|
|
90
|
+
const ordinateKeys = Object.keys(linked.ordinate.key)
|
|
80
91
|
|
|
81
|
-
const fields = keys.map(key=>`${
|
|
82
|
-
const ordinates =
|
|
92
|
+
const fields = keys.map(key=>`${Abscissa.name}${Ordinate.name}.${key}`)
|
|
93
|
+
const ordinates = ordinateKeys.map(key=>`${Ordinate.name}.${key}`)
|
|
83
94
|
|
|
84
95
|
return this.myContext()
|
|
85
96
|
.where(linked.abscissaKey)
|
|
86
|
-
.select(
|
|
97
|
+
.select(`${Ordinate.name}.*`)
|
|
87
98
|
.join(Ordinate.name,clause=>
|
|
88
99
|
fields.forEach((field,i)=>{
|
|
89
100
|
clause.on(field,ordinates[i])
|
|
@@ -1,56 +1,58 @@
|
|
|
1
1
|
import Logged from '../../model/logged.js';
|
|
2
|
-
import Traced from '../../model/traced.js';
|
|
3
2
|
import Context from './context.js';
|
|
4
3
|
import Repository from './index.js';
|
|
5
4
|
|
|
6
|
-
export default class
|
|
7
|
-
constructor(model=
|
|
5
|
+
export default class LoggedRepository extends Repository{
|
|
6
|
+
constructor(model=Logged,context=new Context()){
|
|
8
7
|
super(model,context)
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
.where({active:false})
|
|
13
|
-
.select()
|
|
14
|
-
.then(result => Repository.setOrEmpty(result,this.modelInstance))
|
|
15
|
-
|
|
16
|
-
get=(key={})=>this.myContext()
|
|
17
|
-
.where({...key,active:true})
|
|
18
|
-
.first()
|
|
19
|
-
.then(model => Repository.anyOrError(model,{code:404,message:'Not found'}))
|
|
20
|
-
.then(this.modelInstance)
|
|
21
|
-
|
|
22
|
-
before=(date=new Date())=>this.myContext()
|
|
23
|
-
.where('createdAt','<',date.toISOString())
|
|
24
|
-
.select()
|
|
25
|
-
.then(result => Repository.setOrEmpty(result,this.modelInstance))
|
|
26
|
-
|
|
27
|
-
after=(date=new Date())=>this.myContext()
|
|
28
|
-
.where('createdAt','>',date.toISOString())
|
|
29
|
-
.select()
|
|
30
|
-
.then(result => Repository.setOrEmpty(result,this.modelInstance))
|
|
31
|
-
|
|
32
|
-
list=()=>this.myContext()
|
|
33
|
-
.select()
|
|
34
|
-
.orderBy('createdAt','updatedAt')
|
|
35
|
-
.where({active:true})
|
|
36
|
-
.then(result => Repository.setOrEmpty(result,this.modelInstance))
|
|
37
|
-
|
|
38
|
-
update=(model=new Logged())=>
|
|
10
|
+
get = (logged = new Logged())=>
|
|
39
11
|
this.myContext()
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
12
|
+
.where({...logged.key})
|
|
13
|
+
.first()
|
|
14
|
+
.then(model => Repository.anyOrError(model,{code:404,message:'Not found'}))
|
|
15
|
+
.then(this.Entity.build)
|
|
16
|
+
|
|
17
|
+
before = (date = new Date(), order = 'asc')=>
|
|
18
|
+
this.myContext()
|
|
19
|
+
.where('createdAt','<',date.toISOString())
|
|
20
|
+
.select()
|
|
21
|
+
.orderBy('createdAt',order)
|
|
22
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
23
|
+
|
|
24
|
+
after = (date = new Date(), order = 'asc')=>
|
|
25
|
+
this.myContext()
|
|
26
|
+
.where('createdAt','>',date.toISOString())
|
|
27
|
+
.select()
|
|
28
|
+
.orderBy('createdAt',order)
|
|
29
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
30
|
+
|
|
31
|
+
list = (order='asc')=>
|
|
48
32
|
this.myContext()
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.
|
|
33
|
+
.select()
|
|
34
|
+
.orderBy('createdAt',order)
|
|
35
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
36
|
+
|
|
37
|
+
insert = (logged = new Logged())=>
|
|
38
|
+
this.myContext()
|
|
39
|
+
.insert({
|
|
40
|
+
...logged.entity,
|
|
41
|
+
createdAt:new Date().toISOString()
|
|
42
|
+
},Object.keys(logged.key))
|
|
43
|
+
.then(ids => logged.key = ids[0])
|
|
44
|
+
.then(() => logged)
|
|
45
|
+
|
|
46
|
+
create = (logged = new Logged())=>
|
|
47
|
+
this.myContext()
|
|
48
|
+
.insert({
|
|
49
|
+
...logged.entity,
|
|
50
|
+
createdAt:new Date().toISOString()
|
|
51
|
+
})
|
|
52
|
+
.then(() => logged)
|
|
53
|
+
|
|
54
|
+
update = ()=> Promise.reject(new Error('Cannot update a logged object'))
|
|
55
|
+
|
|
56
|
+
delete = ()=> Promise.reject(new Error('Cannot delete a logged object'))
|
|
55
57
|
|
|
56
58
|
}
|
|
@@ -3,12 +3,13 @@ import Context from "./context.js";
|
|
|
3
3
|
import Repository from "./index.js";
|
|
4
4
|
|
|
5
5
|
export default class ThingRepository extends Repository{
|
|
6
|
-
constructor(context=new Context()){
|
|
7
|
-
super(model
|
|
6
|
+
constructor(model = Thing,context=new Context()){
|
|
7
|
+
super(model,context)
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
getByName=(name='')=>
|
|
11
|
-
.
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
getByName=(name='')=>
|
|
11
|
+
this.myContext()
|
|
12
|
+
.where({name})
|
|
13
|
+
.select()
|
|
14
|
+
.then(result=>Repository.setOrEmpty(result,this.Entity.build))
|
|
14
15
|
}
|
|
@@ -7,33 +7,83 @@ export default class TracedRepository extends Repository{
|
|
|
7
7
|
super(model,context)
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
10
|
+
deceased = (order = 'asc')=>
|
|
11
|
+
this.myContext()
|
|
12
|
+
.where({active:false})
|
|
13
|
+
.select()
|
|
14
|
+
.orderBy(['createdAt','updatedAt'],order)
|
|
15
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
16
|
+
|
|
17
|
+
before = (date = new Date(), order = 'asc')=>
|
|
18
|
+
this.myContext()
|
|
19
|
+
.where('createdAt','<',date.toISOString())
|
|
20
|
+
.where({active:true})
|
|
21
|
+
.select()
|
|
22
|
+
.orderBy(['createdAt','updatedAt'],order)
|
|
23
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
24
|
+
|
|
25
|
+
after = (date=new Date(), order = 'asc')=>
|
|
26
|
+
this.myContext()
|
|
27
|
+
.where('createdAt','>',date.toISOString())
|
|
28
|
+
.where({active:true})
|
|
29
|
+
.select()
|
|
30
|
+
.orderBy(['createdAt','updatedAt'],order)
|
|
31
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
32
|
+
|
|
33
|
+
list = (order = 'asc')=>
|
|
34
|
+
this.myContext()
|
|
35
|
+
.where({active:true})
|
|
36
|
+
.select()
|
|
37
|
+
.orderBy(['createdAt','updatedAt'],order)
|
|
38
|
+
.then(result => Repository.setOrEmpty(result,this.Entity.build))
|
|
39
|
+
|
|
40
|
+
last = ()=>
|
|
41
|
+
this.myContext()
|
|
42
|
+
.where({active:true})
|
|
43
|
+
.first()
|
|
44
|
+
.orderBy('createdAt',"desc")
|
|
45
|
+
.then(result => Repository.anyOrError(result,{code:404,message:'Not found'}))
|
|
46
|
+
|
|
47
|
+
first = ()=>
|
|
48
|
+
this.myContext()
|
|
49
|
+
.where({active:true})
|
|
50
|
+
.first()
|
|
51
|
+
.orderBy('createdAt',"asc")
|
|
52
|
+
.then(result => Repository.anyOrError(result,{code:404,message:'Not found'}))
|
|
53
|
+
|
|
54
|
+
insert = (traced=new Traced())=>
|
|
55
|
+
this.myContext()
|
|
56
|
+
.insert({
|
|
57
|
+
...traced.entity,
|
|
58
|
+
createdAt:new Date().toISOString()
|
|
59
|
+
}, Object.keys(traced.key))
|
|
60
|
+
.then(ids=>traced.key = ids[0])
|
|
61
|
+
.then(()=> traced)
|
|
62
|
+
|
|
63
|
+
create = (traced=new Traced())=>
|
|
64
|
+
this.myContext()
|
|
65
|
+
.insert({
|
|
66
|
+
...traced.key,
|
|
67
|
+
...traced.entity,
|
|
68
|
+
createdAt:new Date().toISOString()
|
|
69
|
+
})
|
|
70
|
+
.then(()=> traced)
|
|
71
|
+
|
|
72
|
+
update = (traced=new Traced())=>
|
|
73
|
+
this.myContext()
|
|
74
|
+
.where(traced.key)
|
|
75
|
+
.update({
|
|
76
|
+
...traced.entity,
|
|
77
|
+
updatedAt:new Date().toISOString()
|
|
78
|
+
})
|
|
79
|
+
.then(affected=>affected > 0)
|
|
80
|
+
|
|
81
|
+
delete = (traced=new Traced())=>
|
|
82
|
+
this.myContext()
|
|
83
|
+
.where(traced.key)
|
|
84
|
+
.update({
|
|
85
|
+
active:false,
|
|
86
|
+
updatedAt:new Date().toISOString()
|
|
87
|
+
})
|
|
88
|
+
.then(affected=>affected > 0)
|
|
39
89
|
}
|