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