@keltoi/hydra 1.1.3 → 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
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;#
|
|
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
|
@@ -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()
|