@keltoi/hydra 1.1.8 → 1.2.1

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;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 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{#r=new Model;#i=new Model;#a=Model;#o=Model;get Abscissa(){return this.#a}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.#a=e,this.#o=t,this.#r=s,this.#i=n}static makeMe(t=e(),s=Model,n=Model,r=(t=new e.TableBuilder)=>{}){return t.schema.createTable(`${s.name}${n.name}`,(e=>{r(e)}))}get abscissaKey(){return{idAbscissa:this.#r.key.id}}get ordinateKey(){return{idOrdinate:this.#i.key.id}}get key(){return{...this.abscissaKey,...this.ordinateKey}}get abscissa(){return this.#r}set abscissa(e=new Model){this.#r=e}get ordinate(){return this.#i}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{#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=>{await this.#c.schema.hasTable(e.name)||await e.makeMe(this.#c)}));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)}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])).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{#h;constructor(e=t.create({})){this.#h=e}get http(){return this.#h}}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}`)),r=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,r[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}`)),r=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,r[s])})))).then((e=>e.map((e=>t.build(e)))))}},exports.DbLoggedRepository=class TracedRepository extends n{constructor(e=Traced,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).toISOString()}).then((e=>e>0));delete=(e={})=>this.myContext().where(e).update({active:!1,updatedAt:(new Date).toISOString()}).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.DbTracedRepository=class TracedRepository extends n{constructor(e=Traced,t=new s){super(e,t)}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").then((e=>n.setOrEmpty(e,this.modelInstance)));last=()=>this.myContext().first().orderBy("createdBy",order="desc").then((e=>n.anyOrError(e,{code:400,message:"Not found"})));first=()=>this.myContext().first().orderBy("createdBy",order="asc").then((e=>n.anyOrError(e,{code:400,message:"Not found"})));create=(e=new Traced)=>this.myContext().insert({...e.entity,createdAt:(new Date).toISOString()},Object.keys(model.key)).then((e=>model.key=e[0])).then((()=>model))},exports.Handler=class Handler{#l;constructor({context:e=new s}){this.#l=e}get context(){return this.#l}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{#l;constructor(e=new s){this.#l=e}get context(){return this.#l}handleError=(e,t)=>Promise.reject({code:e,message:t});handleFailure=e=>this.handleError({code:500,message:e.message})},exports.Thing=Thing,exports.Traced=Traced;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keltoi/hydra",
3
- "version": "1.1.8",
3
+ "version": "1.2.1",
4
4
  "description": "Boilerplate to start your API from zero using Repository Pattern",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,3 +1,4 @@
1
+ import AppError from "../model/error";
1
2
  import Context from "../repository/db/context";
2
3
 
3
4
  export default class Handler {
@@ -12,6 +13,6 @@ export default class Handler {
12
13
 
13
14
  handle(){}
14
15
  handleError({ code, message }) {
15
- return Promise.reject({ code, message });
16
+ return Promise.reject(new AppError({ code, message }));
16
17
  }
17
18
  }
@@ -0,0 +1,7 @@
1
+ export default (
2
+ cond = Promise.resolve(true),
3
+ execute = async () => {},
4
+ )=>cond
5
+ .then(async isTrue=>{
6
+ if (!isTrue) await execute()
7
+ })
@@ -0,0 +1,7 @@
1
+ export default (
2
+ cond = Promise.resolve(true),
3
+ execute = async () => {},
4
+ )=>cond
5
+ .then(async isTrue=>{
6
+ if (isTrue) await execute()
7
+ })
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import AppError from './model/error.js'
1
2
  import Traced from './model/traced.js'
2
3
  import Logged from './model/logged.js'
3
4
  import Model from './model/index.js'
@@ -14,8 +15,11 @@ import ApiRepository from './repository/api/index.js'
14
15
  import ApiRestRepository from './repository/api/restRepository.js'
15
16
  import Service from './service/index.js'
16
17
  import Handler from './handler/index.js'
18
+ import runWhenTrue from './helper/runWhenTrue.js'
19
+ import runWhenFalse from './helper/runWhenFalse.js'
17
20
 
18
21
  export {
22
+ AppError,
19
23
  Model,
20
24
  Thing,
21
25
  Traced,
@@ -31,5 +35,7 @@ export {
31
35
  ApiRepository,
32
36
  ApiRestRepository,
33
37
  Service,
34
- Handler
38
+ Handler,
39
+ runWhenTrue,
40
+ runWhenFalse
35
41
  }
@@ -0,0 +1,23 @@
1
+ class AppError {
2
+ #code
3
+ #message
4
+
5
+ constructor({ code=0, message='' }) {
6
+ this.#code = code
7
+ this.#message = message
8
+ }
9
+
10
+ get view() {
11
+ return {
12
+ code: this.code,
13
+ message: this.message
14
+ }
15
+ }
16
+
17
+ get code() { return this.#code }
18
+ get message() { return this.#message }
19
+
20
+
21
+ }
22
+
23
+ export default AppError
@@ -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.build(),
29
- ordinate = OrdinateModel.build()
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 knex.TableBuilder())=>{}
46
+ schema=(t=new TableBuilder())=>{}
44
47
  ){
45
- return db.schema
46
- .createTable(
47
- `${abscissa.name}${ordinate.name}`,
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['id'] }
63
+ return { idAbscissa: this.#abscissa.key }
56
64
  }
57
65
 
58
66
  get ordinateKey(){
59
- return { idOrdinate: this.#ordinate.key['id'] }
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 = new Model()){ this.#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 = new Model()){ this.ordinate = value }
83
+ set ordinate(value = this.Ordinate.build()){ this.ordinate = value }
74
84
 
75
85
  }
@@ -1,5 +1,6 @@
1
1
  import knex, { TableBuilder } from "knex"
2
2
  import Model from "./index.js"
3
+ import runWhenFalse from "../helper/runWhenFalse.js"
3
4
 
4
5
  export default class Logged extends Model{
5
6
  #createdAt=new Date()
@@ -10,7 +11,7 @@ export default class Logged extends Model{
10
11
  }){
11
12
  super(key)
12
13
  this.#createdAt = createdAt
13
- }
14
+ }
14
15
 
15
16
  get createdAt(){ return this.#createdAt }
16
17
  set createdAt(value = new Date()){ this.createdAt = value }
@@ -20,8 +21,9 @@ export default class Logged extends Model{
20
21
  model=Logged,
21
22
  schema=(t=new TableBuilder())=>{}
22
23
  ){
23
- return db.schema
24
- .createTable(
24
+ return runWhenFalse(
25
+ db.schema.hasTable(model.name),
26
+ ()=>db.schema.createTable(
25
27
  model.name,
26
28
  table=>{
27
29
  schema(table)
@@ -31,5 +33,6 @@ export default class Logged extends Model{
31
33
  .defaultTo(db.fn.now())
32
34
  }
33
35
  )
36
+ )
34
37
  }
35
38
  }
@@ -1,5 +1,6 @@
1
1
  import knex, { TableBuilder } from "knex"
2
2
  import Model from "."
3
+ import runWhenFalse from "../helper/runWhenFalse"
3
4
 
4
5
  export default class Status extends Model{
5
6
  #description=''
@@ -11,8 +12,9 @@ export default class Status extends Model{
11
12
  model=Status,
12
13
  schema=(t=new TableBuilder())=>{}
13
14
  ){
14
- return db.schema
15
- .createTable(
15
+ return runWhenFalse(
16
+ db.schema.hasTable(model.name),
17
+ ()=>db.schema.createTable(
16
18
  model.name,
17
19
  table=>{
18
20
  table.increments()
@@ -25,6 +27,7 @@ export default class Status extends Model{
25
27
  schema(table)
26
28
  }
27
29
  )
30
+ )
28
31
  }
29
32
 
30
33
  constructor({ id=1, description=''}){
@@ -1,5 +1,6 @@
1
1
  import knex, { TableBuilder } from "knex";
2
2
  import Model from "./index.js";
3
+ import runWhenFalse from "../helper/runWhenFalse.js";
3
4
 
4
5
 
5
6
  export default class Thing extends Model{
@@ -22,17 +23,20 @@ export default class Thing extends Model{
22
23
  static makeMe(
23
24
  db=knex(),
24
25
  thing=Thing,
26
+ size = 255,
25
27
  schema=(t=new TableBuilder())=>{}
26
28
  ){
27
- return db.schema
28
- .createTable(
29
+ return runWhenFalse(
30
+ db.schema.hasTable(thing.name),
31
+ ()=>db.schema.createTable(
29
32
  thing.name,
30
33
  table=>{
31
34
  schema(table)
32
35
 
33
- table.string(255)
36
+ table.string(size)
34
37
  .notNullable()
35
38
  }
36
39
  )
40
+ )
37
41
  }
38
42
  }
@@ -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.schema
36
- .createTable(
37
- model.name,
38
- table=>{
39
- schema(table)
40
-
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
- )
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,7 @@ 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
- const exists = await this.#db
32
- .schema
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))
37
31
 
38
32
  await Promise.all(promises)
39
33
  }
@@ -1,6 +1,7 @@
1
1
  import knex from "knex";
2
2
  import Model from "../../model/index.js";
3
3
  import Context from "./context.js";
4
+ import AppError from "../../model/error.js";
4
5
 
5
6
 
6
7
  export default class Repository{
@@ -8,10 +9,12 @@ export default class Repository{
8
9
 
9
10
  myContext
10
11
 
12
+ Entity = Model
13
+
11
14
  static anyOrError(model,err={code:0,message:''}){
12
15
  if (!!model) return model
13
16
 
14
- throw err;
17
+ throw new AppError(err);
15
18
  }
16
19
 
17
20
  static setOrEmpty(array=[],modeling=(e)=>e){
@@ -28,46 +31,52 @@ export default class Repository{
28
31
  this.myContext=()=>context.db(this.#name)
29
32
  }
30
33
 
31
- this.modelInstance=(m={})=>model.build(m)
34
+ this.Entity = model
32
35
  }
33
36
 
34
37
  set context(value=knex()){ this.myContext=()=>value(this.#name) }
35
38
 
36
- create = (model=new Model())=>
39
+ create = (model = new Model())=>
37
40
  this.myContext()
38
- .insert({
39
- ...model.key,
40
- ...model.entity
41
- })
42
- .then(()=>model)
41
+ .insert({
42
+ ...model.key,
43
+ ...model.entity
44
+ })
45
+ .then(()=>model)
43
46
 
44
- insert = (model=new Model())=>
47
+ insert = (model = new Model())=>
45
48
  this.myContext()
46
- .insert(model.entity,Object.keys(model.key))
47
- .then(ids=>model.key = ids[0])
48
- .then(()=>model)
49
+ .insert(model.entity,Object.keys(model.key))
50
+ .then(ids=>{
51
+ model.key = ids[0]
52
+ return model
53
+ })
49
54
 
50
- update = (model=new Model())=>
55
+ update = (model = new Model())=>
51
56
  this.myContext()
52
- .where(model.key)
53
- .update(model.entity)
54
- .then(affected=>affected > 0)
57
+ .where(model.key)
58
+ .update(model.entity)
59
+ .then(affected=>affected > 0)
55
60
 
56
- delete = (key={})=>
61
+ delete = (model = new Model())=>
57
62
  this.myContext()
58
- .where(key)
59
- .del()
60
- .then(affected=>affected > 0)
63
+ .where(model.key)
64
+ .del()
65
+ .then(affected=>affected > 0)
61
66
 
62
- get = (key={})=>
67
+ get = (model = new Model())=>
63
68
  this.myContext()
64
- .where(key)
65
- .first()
66
- .then(model=>Repository.anyOrError(model,{code:404,message:'Not found'}))
67
- .then(this.modelInstance)
69
+ .where(model.key)
70
+ .first()
71
+ .then(model=>Repository
72
+ .anyOrError(model,{code:404,message:'Not found'})
73
+ )
74
+ .then(this.Entity.build)
68
75
 
69
76
  list = ()=>
70
77
  this.myContext()
71
- .select()
72
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
78
+ .select()
79
+ .then(result=>Repository
80
+ .setOrEmpty(result,this.Entity.build)
81
+ )
73
82
  }
@@ -14,56 +14,64 @@ export default class DbLinked {
14
14
  this.#linking = link
15
15
  }
16
16
 
17
- #insertBatch=(linkings=[new Linking()])=>
17
+ createBatch = (linkings=[ new Linking() ])=>
18
18
  this.myContext()
19
19
  .insert(
20
- linkings.map(l => l.key())
20
+ linkings.map(l => (
21
+ {
22
+ ...l.key,
23
+ ...l.entity
24
+ })
25
+ )
21
26
  )
22
27
  .then(()=>linkings)
23
28
 
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()])=>{
29
+ create = (linked=new Linking())=>
30
+ this.myContext()
31
+ .insert({
32
+ ...linked.key,
33
+ ...linked.entity
34
+ })
35
+ .then(()=>linked);
36
+
37
+
38
+ insertOrdinatesByAbscissa = ({ abscissa=new Model(),ordinates=[new Model()] })=>{
33
39
  const batch = ordinates
34
40
  .map(o=>this.#linking
35
- .build({abscissa,o})
41
+ .build({abscissa,ordinate:o})
36
42
  )
37
43
 
38
- return this.#insertBatch(batch);
44
+ return this.createBatch(batch);
39
45
  }
40
46
 
41
- insertAbscissasByOrdinate=(ordinate=new Model(),abscissas=[new Model()])=>{
47
+ insertAbscissasByOrdinate = ({ ordinate=new Model(),abscissas=[new Model()] })=>{
42
48
  const batch = abscissas
43
49
  .map(a=>this.#linking
44
- .build({a,ordinate})
50
+ .build({abscissa:a,ordinate})
45
51
  )
46
52
 
47
- return this.#insertBatch(batch)
53
+ return this.createBatch(batch)
48
54
  }
49
55
 
50
- delete=(linked=new Linking())=>{
51
- return this.myContext()
52
- .where(linked.key())
56
+ delete = (linked=new Linking())=>
57
+ this.myContext()
58
+ .where(linked.key)
53
59
  .del()
54
60
  .then(affected=>affected > 0)
55
- }
61
+
56
62
 
57
- abscissasByOrdinate=(linked=new Linking())=>{
63
+ getAbscissasByOrdinate(linked=new Linking()){
58
64
  const Abscissa = linked.Abscissa
65
+ const Ordinate = linked.Ordinate
59
66
  const keys = Object.keys(linked.abscissaKey)
67
+ const abscissaKey = Object.keys(linked.abscissa.key)
60
68
 
61
- const fields = keys.map(key=>`${this.#name}.${key}`)
62
- const abscissas = keys.map(key=>`${Abscissa.name}.${key}`)
69
+ const fields = keys.map(key=>`${Abscissa.name}${Ordinate.name}.${key}`)
70
+ const abscissas = abscissaKey.map(key=>`${Abscissa.name}.${key}`)
63
71
 
64
72
  return this.myContext()
65
73
  .where(linked.ordinateKey)
66
- .select(fields)
74
+ .select(`${Abscissa.name}.*`)
67
75
  .join(Abscissa.name,clause=>
68
76
  fields.forEach((field,i)=>{
69
77
  clause.on(field,abscissas[i])
@@ -74,16 +82,18 @@ export default class DbLinked {
74
82
  )
75
83
  }
76
84
 
77
- ordinatesByAbscissa=(linked=new Linking())=>{
85
+ getOrdinatesByAbscissa(linked=new Linking()){
78
86
  const Ordinate = linked.Ordinate
87
+ const Abscissa = linked.Abscissa
79
88
  const keys = Object.keys(linked.ordinateKey)
89
+ const ordinateKeys = Object.keys(linked.ordinate.key)
80
90
 
81
- const fields = keys.map(key=>`${this.#name}.${key}`)
82
- const ordinates = keys.map(key=>`${Ordinate.name}.${key}`)
91
+ const fields = keys.map(key=>`${Abscissa.name}${Ordinate.name}.${key}`)
92
+ const ordinates = ordinateKeys.map(key=>`${Ordinate.name}.${key}`)
83
93
 
84
94
  return this.myContext()
85
95
  .where(linked.abscissaKey)
86
- .select(fields)
96
+ .select(`${Ordinate.name}.*`)
87
97
  .join(Ordinate.name,clause=>
88
98
  fields.forEach((field,i)=>{
89
99
  clause.on(field,ordinates[i])
@@ -1,56 +1,59 @@
1
+ import AbstractError from '../../model/error.js';
1
2
  import Logged from '../../model/logged.js';
2
- import Traced from '../../model/traced.js';
3
3
  import Context from './context.js';
4
4
  import Repository from './index.js';
5
5
 
6
- export default class TracedRepository extends Repository{
7
- constructor(model=Traced,context=new Context()){
6
+ export default class LoggedRepository extends Repository{
7
+ constructor(model=Logged,context=new Context()){
8
8
  super(model,context)
9
9
  }
10
10
 
11
- deceased=()=>this.myContext()
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())=>
11
+ get = (logged = new Logged()) =>
39
12
  this.myContext()
40
- .where(model.key)
41
- .update({
42
- ...model.entity,
43
- updatedAt:new Date().toISOString()
44
- })
45
- .then(affected => affected > 0)
46
-
47
- delete=(key={})=>
13
+ .where({...logged.key})
14
+ .first()
15
+ .then(model => Repository.anyOrError(model,{code:404,message:'Not found'}))
16
+ .then(this.Entity.build)
17
+
18
+ before = (date = new Date(), order = 'asc') =>
19
+ this.myContext()
20
+ .where('createdAt','<',date.toISOString())
21
+ .select()
22
+ .orderBy('createdAt',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
+ .select()
29
+ .orderBy('createdAt',order)
30
+ .then(result => Repository.setOrEmpty(result,this.Entity.build))
31
+
32
+ list = (order='asc') =>
48
33
  this.myContext()
49
- .where(key)
50
- .update({
51
- active:false,
52
- updatedAt:new Date().toISOString()
53
- })
54
- .then(affected => affected > 0)
34
+ .select()
35
+ .orderBy('createdAt',order)
36
+ .then(result => Repository.setOrEmpty(result,this.Entity.build))
37
+
38
+ insert = (logged = new Logged()) =>
39
+ this.myContext()
40
+ .insert({
41
+ ...logged.entity,
42
+ createdAt:new Date().toISOString()
43
+ },Object.keys(logged.key))
44
+ .then(ids => logged.key = ids[0])
45
+ .then(() => logged)
46
+
47
+ create = (logged = new Logged()) =>
48
+ this.myContext()
49
+ .insert({
50
+ ...logged.entity,
51
+ createdAt:new Date().toISOString()
52
+ })
53
+ .then(() => logged)
54
+
55
+ update = () => Promise.reject(new AbstractError({code:400,message:'Cannot update a logged object'}))
56
+
57
+ delete = () => Promise.reject(new AbstractError({code:400,message:'Cannot delete a logged object'}))
55
58
 
56
59
  }
@@ -3,12 +3,15 @@ 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=Thing,context)
6
+ constructor(model = Thing,context=new Context()){
7
+ super(model,context)
8
8
  }
9
9
 
10
- getByName=(name='')=>this.myContext()
11
- .where({name})
12
- .select()
13
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
10
+ getByName = (name='') =>
11
+ this.myContext()
12
+ .where({name})
13
+ .select()
14
+ .then(result=>Repository
15
+ .setOrEmpty(result,this.Entity.build)
16
+ )
14
17
  }
@@ -7,33 +7,95 @@ export default class TracedRepository extends Repository{
7
7
  super(model,context)
8
8
  }
9
9
 
10
- before=(date=new Date())=>this.myContext()
11
- .where('createdAt','<',date.toISOString())
12
- .select()
13
- .then(result => Repository.setOrEmpty(result,this.modelInstance))
14
-
15
- after=(date=new Date())=>this.myContext()
16
- .where('createdAt','>',date.toISOString())
17
- .select()
18
- .then(result => Repository.setOrEmpty(result,this.modelInstance))
19
-
20
- list=()=>this.myContext()
21
- .select()
22
- .orderBy('createdAt')
23
- .then(result => Repository.setOrEmpty(result,this.modelInstance))
24
-
25
- last=()=>this.myContext()
26
- .first()
27
- .orderBy('createdBy',order="desc")
28
- .then(result => Repository.anyOrError(result,{code:400,message:'Not found'}))
29
-
30
- first=()=>this.myContext()
31
- .first()
32
- .orderBy('createdBy',order="asc")
33
- .then(result => Repository.anyOrError(result,{code:400,message:'Not found'}))
34
-
35
- create=(traced=new Traced())=>this.myContext()
36
- .insert({ ...traced.entity, createdAt:new Date().toISOString() }, Object.keys(model.key))
37
- .then(ids=>model.key = ids[0])
38
- .then(()=> model)
10
+ deceased = (order = 'asc') =>
11
+ this.myContext()
12
+ .where({active:false})
13
+ .select()
14
+ .orderBy(['createdAt','updatedAt'],order)
15
+ .then(result => Repository
16
+ .setOrEmpty(result,this.Entity.build)
17
+ )
18
+
19
+ before = (date = new Date(), order = 'asc') =>
20
+ this.myContext()
21
+ .where('createdAt','<',date.toISOString())
22
+ .where({active:true})
23
+ .select()
24
+ .orderBy(['createdAt','updatedAt'],order)
25
+ .then(result => Repository
26
+ .setOrEmpty(result,this.Entity.build)
27
+ )
28
+
29
+ after = (date=new Date(), order = 'asc') =>
30
+ this.myContext()
31
+ .where('createdAt','>',date.toISOString())
32
+ .where({active:true})
33
+ .select()
34
+ .orderBy(['createdAt','updatedAt'],order)
35
+ .then(result => Repository
36
+ .setOrEmpty(result,this.Entity.build)
37
+ )
38
+
39
+ list = (order = 'asc') =>
40
+ this.myContext()
41
+ .where({active:true})
42
+ .select()
43
+ .orderBy(['createdAt','updatedAt'],order)
44
+ .then(result => Repository
45
+ .setOrEmpty(result,this.Entity.build)
46
+ )
47
+
48
+ last = () =>
49
+ this.myContext()
50
+ .where({active:true})
51
+ .first()
52
+ .orderBy('createdAt',"desc")
53
+ .then(result => Repository
54
+ .anyOrError(result,{code:404,message:'Not found'})
55
+ )
56
+
57
+ first = () =>
58
+ this.myContext()
59
+ .where({active:true})
60
+ .first()
61
+ .orderBy('createdAt',"asc")
62
+ .then(result => Repository
63
+ .anyOrError(result,{code:404,message:'Not found'})
64
+ )
65
+
66
+ insert = (traced=new Traced()) =>
67
+ this.myContext()
68
+ .insert({
69
+ ...traced.entity,
70
+ createdAt:new Date().toISOString()
71
+ }, Object.keys(traced.key))
72
+ .then(ids=>traced.key = ids[0])
73
+ .then(()=> traced)
74
+
75
+ create = (traced=new Traced())=>
76
+ this.myContext()
77
+ .insert({
78
+ ...traced.key,
79
+ ...traced.entity,
80
+ createdAt:new Date().toISOString()
81
+ })
82
+ .then(()=> traced)
83
+
84
+ update = (traced=new Traced())=>
85
+ this.myContext()
86
+ .where(traced.key)
87
+ .update({
88
+ ...traced.entity,
89
+ updatedAt:new Date().toISOString()
90
+ })
91
+ .then(affected=>affected > 0)
92
+
93
+ delete = (traced=new Traced())=>
94
+ this.myContext()
95
+ .where(traced.key)
96
+ .update({
97
+ active:false,
98
+ updatedAt:new Date().toISOString()
99
+ })
100
+ .then(affected=>affected > 0)
39
101
  }
@@ -1,3 +1,4 @@
1
+ import AppError from "../model/error"
1
2
  import Context from "../repository/db/context"
2
3
 
3
4
  export default class Service {
@@ -8,6 +9,6 @@ export default class Service {
8
9
 
9
10
  get context(){return this.#context}
10
11
 
11
- handleError = (code,message)=>Promise.reject({code,message})
12
+ handleError = (code,message)=>Promise.reject(new AppError({code,message}))
12
13
  handleFailure = (err)=> this.handleError({code:500,message:err.message})
13
14
  }