@keltoi/hydra 1.1.4 → 1.1.6

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;#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;
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)}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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keltoi/hydra",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Boilerplate to start your API from zero using Repository Pattern",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,17 @@
1
+ import Context from "../repository/db/context";
2
+
3
+ export default class Handler {
4
+ #context
5
+ constructor({ context = new Context }) {
6
+ this.#context = context;
7
+ }
8
+
9
+ get context(){
10
+ return this.#context
11
+ }
12
+
13
+ handle(){}
14
+ handleError({ code, message }) {
15
+ return Promise.reject({ code, message });
16
+ }
17
+ }
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import Traced from './model/traced.js'
1
2
  import Logged from './model/logged.js'
2
3
  import Model from './model/index.js'
3
4
  import Linking from './model/linking.js'
@@ -5,23 +6,30 @@ import Thing from './model/thing.js'
5
6
  import DbContext from './repository/db/context.js'
6
7
  import DbRepository from './repository/db/index.js'
7
8
  import DbLinked from './repository/db/linked.js'
8
- import DbLoggedRepository from './repository/db/loggedRepository.js'
9
+ import DbLoggedRepository from './repository/db/loggedRepository.js'
10
+ import DbTracedRepository from './repository/db/tracedRepository.js'
9
11
  import DbThingRepository from './repository/db/thingRepository.js'
10
12
  import ApiContext from './repository/api/context.js'
11
13
  import ApiRepository from './repository/api/index.js'
12
14
  import ApiRestRepository from './repository/api/restRepository.js'
15
+ import Service from './service/index.js'
16
+ import Handler from './handler/index.js'
13
17
 
14
18
  export {
15
19
  Model,
16
20
  Thing,
21
+ Traced,
17
22
  Logged,
18
23
  Linking,
19
24
  DbContext,
20
25
  DbRepository,
21
26
  DbLinked,
27
+ DbTracedRepository,
22
28
  DbLoggedRepository,
23
29
  DbThingRepository,
24
30
  ApiContext,
25
31
  ApiRepository,
26
- ApiRestRepository
32
+ ApiRestRepository,
33
+ Service,
34
+ Handler
27
35
  }
@@ -1,36 +1,24 @@
1
- import knex from "knex"
1
+ import knex, { TableBuilder } from "knex"
2
2
  import Model from "./index.js"
3
3
 
4
4
  export default class Logged extends Model{
5
5
  #createdAt=new Date()
6
- #updatedAt=new Date()|undefined
7
- #active=true
8
6
 
9
7
  constructor({
10
8
  key={},
11
- createdAt=new Date(),
12
- updatedAt=new Date()|undefined,
13
- active=true}
14
- ){
9
+ createdAt=new Date()
10
+ }){
15
11
  super(key)
16
- this.#active=active
17
- this.#updatedAt = updatedAt
18
12
  this.#createdAt = createdAt
19
13
  }
20
14
 
21
15
  get createdAt(){ return this.#createdAt }
22
16
  set createdAt(value = new Date()){ this.createdAt = value }
23
17
 
24
- get updatedAt(){ return this.#updatedAt }
25
- set updatedAt(value = new Date()){ this.#updatedAt = value }
26
-
27
- get active(){ return this.#active }
28
- set active(value = true){ this.#active = value }
29
-
30
18
  static makeMe(
31
19
  db=knex(),
32
- model=Model,
33
- schema=(t=new knex.TableBuilder())=>{}
20
+ model=Logged,
21
+ schema=(t=new TableBuilder())=>{}
34
22
  ){
35
23
  return db.schema
36
24
  .createTable(
@@ -41,12 +29,6 @@ export default class Logged extends Model{
41
29
  table.dateTime('createdAt')
42
30
  .notNullable()
43
31
  .defaultTo(db.fn.now())
44
-
45
- table.dateTime('updatedAt')
46
- .nullable()
47
-
48
- table.boolean('active')
49
- .defaultTo(true)
50
32
  }
51
33
  )
52
34
  }
@@ -0,0 +1,43 @@
1
+ import knex, { TableBuilder } from "knex"
2
+ import Model from "."
3
+
4
+ export default class Status extends Model{
5
+ #description=''
6
+
7
+ static build=({ id=1,description='' })=>new Status({id,description})
8
+
9
+ static makeMe(
10
+ db=knex(),
11
+ model=Status,
12
+ schema=(t=new TableBuilder())=>{}
13
+ ){
14
+ return db.schema
15
+ .createTable(
16
+ model.name,
17
+ table=>{
18
+ table.increments()
19
+
20
+ table
21
+ .string('status',50)
22
+ .notNullable()
23
+ .unique()
24
+
25
+ schema(table)
26
+ }
27
+ )
28
+ }
29
+
30
+ constructor({ id=1, description=''}){
31
+ super({id})
32
+ this.#description = description
33
+ }
34
+
35
+ get description(){ return this.#description }
36
+ set description( value='' ){ this.#description = value }
37
+
38
+ get entity(){
39
+ return {
40
+ description:this.#description
41
+ }
42
+ }
43
+ }
@@ -1,4 +1,4 @@
1
- import knex from "knex";
1
+ import knex, { TableBuilder } from "knex";
2
2
  import Model from "./index.js";
3
3
 
4
4
 
@@ -22,7 +22,7 @@ export default class Thing extends Model{
22
22
  static makeMe(
23
23
  db=knex(),
24
24
  thing=Thing,
25
- schema=(t=new knex.TableBuilder())=>{}
25
+ schema=(t=new TableBuilder())=>{}
26
26
  ){
27
27
  return db.schema
28
28
  .createTable(
@@ -0,0 +1,53 @@
1
+ import knex, { TableBuilder } from "knex"
2
+ import Model from "./index.js"
3
+ import Logged from "./logged.js"
4
+
5
+ export default class Traced extends Logged{
6
+ #updatedAt=new Date()|undefined
7
+ #active=true
8
+
9
+ constructor({
10
+ key={},
11
+ createdAt=new Date(),
12
+ updatedAt=new Date()|undefined,
13
+ active=true}
14
+ ){
15
+ super({ key,createdAt })
16
+ this.#active=active
17
+ this.#updatedAt = updatedAt
18
+ }
19
+
20
+ get updatedAt(){ return this.#updatedAt }
21
+ set updatedAt(value = new Date()){ this.#updatedAt = value }
22
+
23
+ get active(){ return this.#active }
24
+ set active(value = true){ this.#active = value }
25
+
26
+ tickUpdateNow(){
27
+ this.#updatedAt = new Date()
28
+ }
29
+
30
+ static makeMe(
31
+ db=knex(),
32
+ model=Traced,
33
+ schema=(t=new TableBuilder())=>{}
34
+ ){
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
+ )
52
+ }
53
+ }
@@ -1,55 +1,56 @@
1
1
  import Logged from '../../model/logged.js';
2
+ import Traced from '../../model/traced.js';
2
3
  import Context from './context.js';
3
4
  import Repository from './index.js';
4
5
 
5
- export default class LoggedRepository extends Repository{
6
- constructor(model=Logged,context=new Context()){
6
+ export default class TracedRepository extends Repository{
7
+ constructor(model=Traced,context=new Context()){
7
8
  super(model,context)
8
9
  }
9
10
 
10
11
  deceased=()=>this.myContext()
11
12
  .where({active:false})
12
13
  .select()
13
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
14
+ .then(result => Repository.setOrEmpty(result,this.modelInstance))
14
15
 
15
16
  get=(key={})=>this.myContext()
16
17
  .where({...key,active:true})
17
18
  .first()
18
- .then(model=>Repository.anyOrError(model,{code:404,message:'Not found'}))
19
+ .then(model => Repository.anyOrError(model,{code:404,message:'Not found'}))
19
20
  .then(this.modelInstance)
20
21
 
21
22
  before=(date=new Date())=>this.myContext()
22
23
  .where('createdAt','<',date.toISOString())
23
24
  .select()
24
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
25
+ .then(result => Repository.setOrEmpty(result,this.modelInstance))
25
26
 
26
27
  after=(date=new Date())=>this.myContext()
27
28
  .where('createdAt','>',date.toISOString())
28
29
  .select()
29
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
30
+ .then(result => Repository.setOrEmpty(result,this.modelInstance))
30
31
 
31
32
  list=()=>this.myContext()
32
33
  .select()
33
34
  .orderBy('createdAt','updatedAt')
34
35
  .where({active:true})
35
- .then(result=>Repository.setOrEmpty(result,this.modelInstance))
36
+ .then(result => Repository.setOrEmpty(result,this.modelInstance))
36
37
 
37
38
  update=(model=new Logged())=>
38
39
  this.myContext()
39
40
  .where(model.key)
40
41
  .update({
41
42
  ...model.entity,
42
- updatedAt:new Date()
43
+ updatedAt:new Date().toISOString()
43
44
  })
44
- .then(affected=> affected > 0)
45
+ .then(affected => affected > 0)
45
46
 
46
47
  delete=(key={})=>
47
48
  this.myContext()
48
49
  .where(key)
49
50
  .update({
50
- updatedAt:new Date().toISOString(),
51
- active:false
51
+ active:false,
52
+ updatedAt:new Date().toISOString()
52
53
  })
53
- .then(affected=> affected > 0)
54
+ .then(affected => affected > 0)
54
55
 
55
56
  }
@@ -0,0 +1,39 @@
1
+ import Traced from '../../model/traced.js';
2
+ import Context from './context.js';
3
+ import Repository from './index.js';
4
+
5
+ export default class TracedRepository extends Repository{
6
+ constructor(model=Traced,context=new Context()){
7
+ super(model,context)
8
+ }
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)
39
+ }
@@ -0,0 +1,13 @@
1
+ import Context from "../repository/db/context"
2
+
3
+ export default class Service {
4
+ #context
5
+ constructor(context=new Context()){
6
+ this.#context = context
7
+ }
8
+
9
+ get context(){return this.#context}
10
+
11
+ handleError = (code,message)=>Promise.reject({code,message})
12
+ handleFailure = (err)=> this.handleError({code:500,message:err.message})
13
+ }