@keltoi/hydra 1.1.0 → 1.1.3

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 CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("knex"),e=require("axios");class Model{#t;constructor(t={}){this.#t=t}get key(){return this.#t}set key(t={}){this.#t=t}get entity(){return{}}}class Logged extends Model{#e=new Date;#n=void 0|new Date;#s=!0;constructor({key:t={},createdAt:e=new Date,updatedAt:n=void 0|new Date,active:s=!0}){super(t),this.#s=s,this.#n=n,this.#e=e}get createdAt(){return this.#e}set createdAt(t=new Date){this.createdAt=t}get updatedAt(){return this.#n}set updatedAt(t=new Date){this.#n=t}get active(){return this.#s}set active(t=!0){this.#s=t}static makeMe(e=t(),n=Model,s=(e=new t.TableBuilder)=>{}){return e.schema.createTable(n.name,(t=>{s(t),t.dateTime("createdAt").notNullable().defaultTo(e.fn.now()),t.dateTime("updatedAt").nullable(),t.boolean("active").defaultTo(!0)}))}}class Thing extends Model{#o;constructor({key:t={},name:e=""}){super(t),this.#o=e}get name(){return this.#o}set name(t=""){this.#o=t}get entity(){return{name:this.#o}}static makeMe(e=t(),n=Thing,s=(e=new t.TableBuilder)=>{}){return e.schema.createTable(n.name,(t=>{s(t),t.string(255).notNullable()}))}}let n=class Context{#r;constructor(e=t()){this.#r=e}get db(){return this.#r}unitOfWork(...t){return this.#r.transaction().then((e=>(t.forEach((t=>t.context=e)),{done:()=>e.commit().then((()=>t.forEach((t=>t.resetContext())))),rollback:()=>e.rollback().then((()=>t.forEach((t=>t.resetContext()))))})))}static instance(e=t()){return new Context(e)}async terraform(t=[]){const e=t.map((async t=>{await this.#r.schema.hasTable(t.name)||await t.makeMe(this.#r)}));await Promise.all(e)}},s=class Repository{#o="";myContext;static anyOrError(t,e={code:0,message:""}){if(t)return t;throw e}static setOrEmpty(t=[],e=t=>t){return t.length?t.map(e):[]}constructor(t=Model,e=new n){this.#o=t.name,this.myContext=()=>e.db(this.#o),this.resetContext=()=>{this.myContext=()=>e.db(this.#o)},this.modelInstance=(e={})=>new t(e)}set context(e=t()){this.myContext=()=>e(this.#o)}insert=(t=new Model)=>this.myContext().insert(t.entity,Object.keys(t.key)).then((e=>t.key=e[0])).then((()=>t));update=(t=new Model)=>this.myContext().where(t.key).update(t.entity).then((t=>t>0));delete=(t={})=>this.myContext().where(t).del().then((t=>t>0));get=(t={})=>this.myContext().where(t).first().then(Repository.anyOrError).then(this.modelInstance);list=()=>this.myContext().select().then((t=>Repository.setOrEmpty(t,this.modelInstance)))};class Context{#a;constructor(t=e.create({})){this.#a=t}get http(){return this.#a}}class Repository{#i;constructor(t=Model,e=new Context){this.#i=e,this.modelInstance=(e={})=>new t(e)}static queryString(t={}){return"?"+Object.entries(t).map((([t,e])=>`${t}=${e}`)).join("&")}get context(){return this.#i}}exports.ApiContext=Context,exports.ApiRepository=Repository,exports.ApiRestRepository=class RestRepository extends Repository{constructor(t=Model,e=new Context){super(t,e)}#h=(t="",e=void 0|{})=>this.context.http.get(e?`${t}${Repository.queryString(e)}`:t);get=(t="",e=void 0|{})=>this.#h(t,e).then((t=>this.modelInstance(t)));list=(t="",e=void 0|{})=>this.#h(t,e).then(((t=[])=>t.map((t=>this.modelInstance(t)))));create=(t="",e=new Model)=>this.context.http.post(t,e.entity);update=(t="",e=new Model)=>this.context.http.put(t,e.entity);change=(t="",e=new Model)=>this.context.http.patch(t,e.entity);delete=(t,e=new Model)=>this.context.http.delete(`/${t}`)},exports.DbContext=n,exports.DbLoggedRepository=class LoggedRepository extends s{constructor(t=Logged,e=new n){super(t,e)}deceased=()=>this.myContext().where({active:!1}).select().then((t=>t.map(this.modelInstance)));get=(t={})=>this.myContext().where({...t,active:!0}).first().then(this.modelInstance);before=(t=new Date)=>this.myContext().where("createdAt","<",t.toISOString()).select().then((t=>t.map(this.modelInstance)));after=(t=new Date)=>this.myContext().where("createdAt",">",t.toISOString()).select().then((t=>t.map(this.modelInstance)));list=()=>this.myContext().select().orderBy("createdAt","updatedAt").where({active:!0}).then((t=>t.map(this.modelInstance)));update=(t=new Logged)=>this.myContext().where(t.key).update({...t.entity,updatedAt:new Date}).then((t=>t>0));delete=(t={})=>this.myContext().where(t).update({updatedAt:(new Date).toISOString(),active:!1}).then((t=>t>0))},exports.DbRepository=s,exports.DbThingRepository=class ThingRepository extends s{constructor(t=new n){super(model=Thing,t)}getByName=(t="")=>this.myContext().where({name:t}).select().then((t=>this.modelInstance(t)))},exports.Logged=Logged,exports.Model=Model,exports.Thing=Thing;
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;#n=void 0|new Date;#s=!0;constructor({key:e={},createdAt:t=new Date,updatedAt:n=void 0|new Date,active:s=!0}){super(e),this.#s=s,this.#n=n,this.#t=t}get createdAt(){return this.#t}set createdAt(e=new Date){this.createdAt=e}get updatedAt(){return this.#n}set updatedAt(e=new Date){this.#n=e}get active(){return this.#s}set active(e=!0){this.#s=e}static makeMe(t=e(),n=Model,s=(t=new e.TableBuilder)=>{}){return t.schema.createTable(n.name,(e=>{s(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;#o=Model;#r=Model;get Abscissa(){return this.#o}get Ordinate(){return this.#r}static build=({abscissa:e=new Model,ordinate:t=new Model})=>new Linking(Model,Model,{abscissa:e,ordinate:t});constructor(e=Model,t=Model,{abscissa:n=e.build(),ordinate:s=t.build()}){this.#o=e,this.#r=t,this.#i=n,this.#a=s}static makeMe(t=e(),n=Model,s=Model,i=(t=new e.TableBuilder)=>{}){return t.schema.createTable(`${n.name}${s.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(),n=Thing,s=(t=new e.TableBuilder)=>{}){return t.schema.createTable(n.name,(e=>{s(e),e.string(255).notNullable()}))}}let n=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)}},s=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 n){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=n,exports.DbLinked=class DbLinked{#u;#d="";myContext;constructor(e=Linking,t=new n){this.#d=e.name,this.myContext=()=>t.db(this.#d),this.#u=e}#y=(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 n=t.map((t=>this.#u.build({abscissa:e,o:t})));return this.#y(n)};insertAbscissasByOrdinate=(e=new Model,t=[new Model])=>{const n=t.map((t=>this.#u.build({a:t,ordinate:e})));return this.#y(n)};delete=(e=new Linking)=>this.myContext().where(e.key()).del().then((e=>e>0));abscissasByOrdinate=(e=new Linking)=>{const t=e.Abscissa,n=Object.keys(e.abscissaKey),s=n.map((e=>`${this.#d}.${e}`)),i=n.map((e=>`${t.name}.${e}`));return this.myContext().where(e.ordinateKey).select(s).join(t.name,(e=>s.forEach(((t,n)=>{e.on(t,i[n])})))).then((e=>e.map((e=>t.build(e)))))};ordinatesByAbscissa=(e=new Linking)=>{const t=e.Ordinate,n=Object.keys(e.ordinateKey),s=n.map((e=>`${this.#d}.${e}`)),i=n.map((e=>`${t.name}.${e}`));return this.myContext().where(e.abscissaKey).select(s).join(t.name,(e=>s.forEach(((t,n)=>{e.on(t,i[n])})))).then((e=>e.map((e=>t.build(e)))))}},exports.DbLoggedRepository=class LoggedRepository extends s{constructor(e=Logged,t=new n){super(e,t)}deceased=()=>this.myContext().where({active:!1}).select().then((e=>e.map(this.modelInstance)));get=(e={})=>this.myContext().where({...e,active:!0}).first().then(this.modelInstance);before=(e=new Date)=>this.myContext().where("createdAt","<",e.toISOString()).select().then((e=>e.map(this.modelInstance)));after=(e=new Date)=>this.myContext().where("createdAt",">",e.toISOString()).select().then((e=>e.map(this.modelInstance)));list=()=>this.myContext().select().orderBy("createdAt","updatedAt").where({active:!0}).then((e=>e.map(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=s,exports.DbThingRepository=class ThingRepository extends s{constructor(e=new n){super(model=Thing,e)}getByName=(e="")=>this.myContext().where({name:e}).select().then((e=>this.modelInstance(e)))},exports.Linking=Linking,exports.Logged=Logged,exports.Model=Model,exports.Thing=Thing;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keltoi/hydra",
3
- "version": "1.1.0",
3
+ "version": "1.1.3",
4
4
  "description": "Boilerplate to start your API from zero using Repository Pattern",
5
5
  "main": "index.js",
6
6
  "scripts": {
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,
@@ -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
+ }
@@ -6,29 +6,48 @@ export default class Logged extends Model{
6
6
  #updatedAt=new Date()|undefined
7
7
  #active=true
8
8
 
9
- constructor({key={},createdAt=new Date(),updatedAt=new Date()|undefined,active=true}){
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(db=knex(),model=Model,schema=(t=new knex.TableBuilder())=>{}){
26
- return db.schema.createTable(model.name,table=>{
27
- schema(table)
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
- table.dateTime('createdAt').notNullable().defaultTo(db.fn.now())
30
- table.dateTime('updatedAt').nullable()
31
- table.boolean('active').defaultTo(true)
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
  }
@@ -4,23 +4,35 @@ import Model from "./index.js";
4
4
 
5
5
  export default class Thing extends Model{
6
6
  #name
7
- constructor({key={},name=''}){
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(db=knex(),thing=Thing,schema=(t=new knex.TableBuilder())=>{}){
20
- return db.schema.createTable(thing.name,table=>{
21
- schema(table)
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
- table.string(255).notNullable()
24
- })
33
+ table.string(255)
34
+ .notNullable()
35
+ }
36
+ )
25
37
  }
26
38
  }
@@ -2,9 +2,10 @@ import axios from "axios"
2
2
 
3
3
  export default class Context{
4
4
  #http
5
+
5
6
  constructor(config=axios.create({})){
6
7
  this.#http = config
7
8
  }
8
9
 
9
- get http(){return this.#http}
10
+ get http(){ return this.#http }
10
11
  }
@@ -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).map(([k,v])=>`${k}=${v}`).join('&')
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).then(value=>this.modelInstance(value))
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).then((values=[])=>values.map(value=>this.modelInstance(value)))
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.http.post(route,model.entity)
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.http.put(route,model.entity)
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.http.patch(route,model.entity)
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.http.delete(`/${id}`)
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(()=>repositories.forEach(repo=>repo.resetContext())),
18
- rollback:()=>trx.rollback().then(()=>repositories.forEach(repo=>repo.resetContext()))
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.schema.hasTable(model.name)
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
+ }