@keltoi/hydra 1.3.0 → 2.0.0
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 +878 -1
- package/package.json +11 -2
- package/.github/workflows/npm-publish.yml +0 -33
- package/rollup.config.mjs +0 -17
- package/src/handler/index.js +0 -18
- package/src/helper/runWhenFalse.js +0 -7
- package/src/helper/runWhenTrue.js +0 -7
- package/src/index.js +0 -41
- package/src/model/index.js +0 -17
- package/src/model/linking.js +0 -85
- package/src/model/logged.js +0 -38
- package/src/model/result.js +0 -39
- package/src/model/status.js +0 -46
- package/src/model/thing.js +0 -42
- package/src/model/traced.js +0 -44
- package/src/repository/api/context.js +0 -11
- package/src/repository/api/index.js +0 -19
- package/src/repository/api/restRepository.js +0 -36
- package/src/repository/db/context.js +0 -34
- package/src/repository/db/index.js +0 -82
- package/src/repository/db/linked.js +0 -107
- package/src/repository/db/loggedRepository.js +0 -59
- package/src/repository/db/thingRepository.js +0 -17
- package/src/repository/db/tracedRepository.js +0 -101
- package/src/service/index.js +0 -14
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keltoi/hydra",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Boilerplate to start your API from zero using Repository Pattern",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
6
11
|
"scripts": {
|
|
7
12
|
"test": "npm run build & npm link",
|
|
8
13
|
"build": "rollup -c"
|
|
@@ -25,6 +30,10 @@
|
|
|
25
30
|
"@rollup/plugin-terser": "^0.4.4",
|
|
26
31
|
"axios": "^1.7.7",
|
|
27
32
|
"knex": "^3.1.0",
|
|
28
|
-
"rollup": "^4.
|
|
33
|
+
"rollup": "^4.40.2"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
37
|
+
"@rollup/plugin-json": "^6.1.0"
|
|
29
38
|
}
|
|
30
39
|
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Node.js Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
release:
|
|
8
|
-
types: [created]
|
|
9
|
-
|
|
10
|
-
jobs:
|
|
11
|
-
build:
|
|
12
|
-
runs-on: ubuntu-latest
|
|
13
|
-
steps:
|
|
14
|
-
- uses: actions/checkout@v4
|
|
15
|
-
- uses: actions/setup-node@v4
|
|
16
|
-
with:
|
|
17
|
-
node-version: 20
|
|
18
|
-
- run: npm ci
|
|
19
|
-
- run: npm test
|
|
20
|
-
|
|
21
|
-
publish-npm:
|
|
22
|
-
needs: build
|
|
23
|
-
runs-on: ubuntu-latest
|
|
24
|
-
steps:
|
|
25
|
-
- uses: actions/checkout@v4
|
|
26
|
-
- uses: actions/setup-node@v4
|
|
27
|
-
with:
|
|
28
|
-
node-version: 20
|
|
29
|
-
registry-url: https://registry.npmjs.org/
|
|
30
|
-
- run: npm ci
|
|
31
|
-
- run: npm publish
|
|
32
|
-
env:
|
|
33
|
-
NODE_AUTH_TOKEN: ${{secrets.KELTOI_GITPUB}}
|
package/rollup.config.mjs
DELETED
package/src/handler/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import Result from "../model/result";
|
|
2
|
-
import Context from "../repository/db/context";
|
|
3
|
-
|
|
4
|
-
export default class Handler {
|
|
5
|
-
#context
|
|
6
|
-
constructor({ context = new Context }) {
|
|
7
|
-
this.#context = context;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
get context(){
|
|
11
|
-
return this.#context
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
handle(){}
|
|
15
|
-
handleError({ code, message }) {
|
|
16
|
-
return Promise.reject(new Result({ code, message }));
|
|
17
|
-
}
|
|
18
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import Result from './model/result.js'
|
|
2
|
-
import Traced from './model/traced.js'
|
|
3
|
-
import Logged from './model/logged.js'
|
|
4
|
-
import Model from './model/index.js'
|
|
5
|
-
import Linking from './model/linking.js'
|
|
6
|
-
import Thing from './model/thing.js'
|
|
7
|
-
import DbContext from './repository/db/context.js'
|
|
8
|
-
import DbRepository from './repository/db/index.js'
|
|
9
|
-
import DbLinked from './repository/db/linked.js'
|
|
10
|
-
import DbLoggedRepository from './repository/db/loggedRepository.js'
|
|
11
|
-
import DbTracedRepository from './repository/db/tracedRepository.js'
|
|
12
|
-
import DbThingRepository from './repository/db/thingRepository.js'
|
|
13
|
-
import ApiContext from './repository/api/context.js'
|
|
14
|
-
import ApiRepository from './repository/api/index.js'
|
|
15
|
-
import ApiRestRepository from './repository/api/restRepository.js'
|
|
16
|
-
import Service from './service/index.js'
|
|
17
|
-
import Handler from './handler/index.js'
|
|
18
|
-
import runWhenTrue from './helper/runWhenTrue.js'
|
|
19
|
-
import runWhenFalse from './helper/runWhenFalse.js'
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
Result,
|
|
23
|
-
Model,
|
|
24
|
-
Thing,
|
|
25
|
-
Traced,
|
|
26
|
-
Logged,
|
|
27
|
-
Linking,
|
|
28
|
-
DbContext,
|
|
29
|
-
DbRepository,
|
|
30
|
-
DbLinked,
|
|
31
|
-
DbTracedRepository,
|
|
32
|
-
DbLoggedRepository,
|
|
33
|
-
DbThingRepository,
|
|
34
|
-
ApiContext,
|
|
35
|
-
ApiRepository,
|
|
36
|
-
ApiRestRepository,
|
|
37
|
-
Service,
|
|
38
|
-
Handler,
|
|
39
|
-
runWhenTrue,
|
|
40
|
-
runWhenFalse
|
|
41
|
-
}
|
package/src/model/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export default class Model{
|
|
2
|
-
#key
|
|
3
|
-
|
|
4
|
-
constructor(key={}){
|
|
5
|
-
this.#key = key
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
get key(){ return this.#key }
|
|
9
|
-
set key(value={}){ this.#key=value }
|
|
10
|
-
|
|
11
|
-
get entity(){ return {} }
|
|
12
|
-
|
|
13
|
-
static build(key={}){
|
|
14
|
-
return new Model(key)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
}
|
package/src/model/linking.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex"
|
|
2
|
-
import Model from "./index"
|
|
3
|
-
import runWhenFalse from "../helper/runWhenFalse"
|
|
4
|
-
|
|
5
|
-
export default class Linking{
|
|
6
|
-
#abscissa=new Model()
|
|
7
|
-
#ordinate=new Model()
|
|
8
|
-
#AbscissaModel=Model
|
|
9
|
-
#OrdinateModel=Model
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
get Abscissa(){
|
|
13
|
-
return this.#AbscissaModel
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get Ordinate(){
|
|
17
|
-
return this.#OrdinateModel
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
constructor(
|
|
21
|
-
AbscissaModel=Model,
|
|
22
|
-
OrdinateModel=Model,
|
|
23
|
-
{
|
|
24
|
-
abscissa = new AbscissaModel(),
|
|
25
|
-
ordinate = new OrdinateModel()
|
|
26
|
-
})
|
|
27
|
-
{
|
|
28
|
-
this.#AbscissaModel = AbscissaModel
|
|
29
|
-
this.#OrdinateModel = OrdinateModel
|
|
30
|
-
|
|
31
|
-
this.#abscissa = abscissa
|
|
32
|
-
this.#ordinate = ordinate
|
|
33
|
-
}
|
|
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
|
-
|
|
42
|
-
static makeMe(
|
|
43
|
-
db=knex(),
|
|
44
|
-
abscissa=Model,
|
|
45
|
-
ordinate=Model,
|
|
46
|
-
schema=(t=new TableBuilder())=>{}
|
|
47
|
-
){
|
|
48
|
-
const tableName = `${abscissa.name}${ordinate.name}`
|
|
49
|
-
|
|
50
|
-
return runWhenFalse(
|
|
51
|
-
db.schema.hasTable(tableName),
|
|
52
|
-
() => db.schema.createTable(
|
|
53
|
-
tableName,
|
|
54
|
-
table=>{
|
|
55
|
-
schema(table)
|
|
56
|
-
}
|
|
57
|
-
)
|
|
58
|
-
)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
get abscissaKey(){
|
|
63
|
-
return { idAbscissa: this.#abscissa.key }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get ordinateKey(){
|
|
67
|
-
return { idOrdinate: this.#ordinate.key }
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get key(){
|
|
71
|
-
return {
|
|
72
|
-
...this.abscissaKey,
|
|
73
|
-
...this.ordinateKey
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get entity(){}
|
|
78
|
-
|
|
79
|
-
get abscissa(){ return this.#abscissa }
|
|
80
|
-
set abscissa(value = this.Abscissa.build()){ this.#abscissa = value }
|
|
81
|
-
|
|
82
|
-
get ordinate(){ return this.#ordinate }
|
|
83
|
-
set ordinate(value = this.Ordinate.build()){ this.ordinate = value }
|
|
84
|
-
|
|
85
|
-
}
|
package/src/model/logged.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex"
|
|
2
|
-
import Model from "./index.js"
|
|
3
|
-
import runWhenFalse from "../helper/runWhenFalse.js"
|
|
4
|
-
|
|
5
|
-
export default class Logged extends Model{
|
|
6
|
-
#createdAt=new Date()
|
|
7
|
-
|
|
8
|
-
constructor({
|
|
9
|
-
key={},
|
|
10
|
-
createdAt=new Date()
|
|
11
|
-
}){
|
|
12
|
-
super(key)
|
|
13
|
-
this.#createdAt = createdAt
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get createdAt(){ return this.#createdAt }
|
|
17
|
-
set createdAt(value = new Date()){ this.createdAt = value }
|
|
18
|
-
|
|
19
|
-
static makeMe(
|
|
20
|
-
db=knex(),
|
|
21
|
-
model=Logged,
|
|
22
|
-
schema=(t=new TableBuilder())=>{}
|
|
23
|
-
){
|
|
24
|
-
return runWhenFalse(
|
|
25
|
-
db.schema.hasTable(model.name),
|
|
26
|
-
()=>db.schema.createTable(
|
|
27
|
-
model.name,
|
|
28
|
-
table=>{
|
|
29
|
-
schema(table)
|
|
30
|
-
|
|
31
|
-
table.dateTime('createdAt')
|
|
32
|
-
.notNullable()
|
|
33
|
-
.defaultTo(db.fn.now())
|
|
34
|
-
}
|
|
35
|
-
)
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/model/result.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
class Result {
|
|
2
|
-
#code
|
|
3
|
-
#message
|
|
4
|
-
#data
|
|
5
|
-
|
|
6
|
-
constructor({ code=0, message='', data={} }) {
|
|
7
|
-
this.#code = code
|
|
8
|
-
this.#message = message
|
|
9
|
-
this.#data = data
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get error() {
|
|
13
|
-
return {
|
|
14
|
-
code: this.code,
|
|
15
|
-
message: this.message
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get ok() {
|
|
20
|
-
return {
|
|
21
|
-
code: this.code,
|
|
22
|
-
data: this.data
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
sendError(res) {
|
|
27
|
-
res
|
|
28
|
-
.status(this.#code)
|
|
29
|
-
.send(this.#message)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
sendOk(res) {
|
|
33
|
-
res
|
|
34
|
-
.status(this.#code)
|
|
35
|
-
.json(this.#data)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export default Result
|
package/src/model/status.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex"
|
|
2
|
-
import Model from "."
|
|
3
|
-
import runWhenFalse from "../helper/runWhenFalse"
|
|
4
|
-
|
|
5
|
-
export default class Status extends Model{
|
|
6
|
-
#description=''
|
|
7
|
-
|
|
8
|
-
static build=({ id=1,description='' })=>new Status({id,description})
|
|
9
|
-
|
|
10
|
-
static makeMe(
|
|
11
|
-
db=knex(),
|
|
12
|
-
model=Status,
|
|
13
|
-
schema=(t=new TableBuilder())=>{}
|
|
14
|
-
){
|
|
15
|
-
return runWhenFalse(
|
|
16
|
-
db.schema.hasTable(model.name),
|
|
17
|
-
()=>db.schema.createTable(
|
|
18
|
-
model.name,
|
|
19
|
-
table=>{
|
|
20
|
-
table.increments()
|
|
21
|
-
|
|
22
|
-
table
|
|
23
|
-
.string('status',50)
|
|
24
|
-
.notNullable()
|
|
25
|
-
.unique()
|
|
26
|
-
|
|
27
|
-
schema(table)
|
|
28
|
-
}
|
|
29
|
-
)
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
constructor({ id=1, description=''}){
|
|
34
|
-
super({id})
|
|
35
|
-
this.#description = description
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get description(){ return this.#description }
|
|
39
|
-
set description( value='' ){ this.#description = value }
|
|
40
|
-
|
|
41
|
-
get entity(){
|
|
42
|
-
return {
|
|
43
|
-
description:this.#description
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
package/src/model/thing.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex";
|
|
2
|
-
import Model from "./index.js";
|
|
3
|
-
import runWhenFalse from "../helper/runWhenFalse.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export default class Thing extends Model{
|
|
7
|
-
#name
|
|
8
|
-
constructor({
|
|
9
|
-
key={},
|
|
10
|
-
name=''
|
|
11
|
-
}){
|
|
12
|
-
super(key)
|
|
13
|
-
this.#name = name
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get name(){ return this.#name }
|
|
17
|
-
set name(value=''){ this.#name=value }
|
|
18
|
-
|
|
19
|
-
get entity(){
|
|
20
|
-
return { name:this.#name }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static makeMe(
|
|
24
|
-
db=knex(),
|
|
25
|
-
thing=Thing,
|
|
26
|
-
size = 255,
|
|
27
|
-
schema=(t=new TableBuilder())=>{}
|
|
28
|
-
){
|
|
29
|
-
return runWhenFalse(
|
|
30
|
-
db.schema.hasTable(thing.name),
|
|
31
|
-
()=>db.schema.createTable(
|
|
32
|
-
thing.name,
|
|
33
|
-
table=>{
|
|
34
|
-
schema(table)
|
|
35
|
-
|
|
36
|
-
table.string(size)
|
|
37
|
-
.notNullable()
|
|
38
|
-
}
|
|
39
|
-
)
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
}
|
package/src/model/traced.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import knex, { TableBuilder } from "knex"
|
|
2
|
-
import Logged from "./logged.js"
|
|
3
|
-
|
|
4
|
-
export default class Traced extends Logged{
|
|
5
|
-
#updatedAt=new Date()|undefined
|
|
6
|
-
#active=true
|
|
7
|
-
|
|
8
|
-
constructor({
|
|
9
|
-
key={},
|
|
10
|
-
createdAt=new Date(),
|
|
11
|
-
updatedAt=new Date()|undefined,
|
|
12
|
-
active=true
|
|
13
|
-
}){
|
|
14
|
-
super({ key,createdAt })
|
|
15
|
-
this.#active=active
|
|
16
|
-
this.#updatedAt = updatedAt
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get updatedAt(){ return this.#updatedAt }
|
|
20
|
-
set updatedAt(value = new Date()){ this.#updatedAt = value }
|
|
21
|
-
|
|
22
|
-
get active(){ return this.#active }
|
|
23
|
-
set active(value = true){ this.#active = value }
|
|
24
|
-
|
|
25
|
-
tickUpdateNow(){
|
|
26
|
-
this.#updatedAt = new Date()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
static makeMe(
|
|
30
|
-
db=knex(),
|
|
31
|
-
model=Traced,
|
|
32
|
-
schema=(t=new TableBuilder())=>{}
|
|
33
|
-
){
|
|
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
|
-
})
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import Model from "../../model/index.js"
|
|
2
|
-
import Context from "./context.js"
|
|
3
|
-
|
|
4
|
-
export default class Repository{
|
|
5
|
-
#context
|
|
6
|
-
|
|
7
|
-
constructor(model=Model,context=new Context()){
|
|
8
|
-
this.#context = context
|
|
9
|
-
this.modelInstance=(m={})=>new model(m)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
static queryString(param={}){
|
|
13
|
-
return '?' + Object.entries(param)
|
|
14
|
-
.map(([k,v])=>`${k}=${v}`)
|
|
15
|
-
.join('&')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get context(){ return this.#context }
|
|
19
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import Model from "../../model/index.js"
|
|
2
|
-
import Context from "./context.js"
|
|
3
|
-
import Repository from "./index.js"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export default class RestRepository extends Repository{
|
|
7
|
-
constructor(model=Model,context=new Context()){
|
|
8
|
-
super(model,context)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
#httpGet=(route='',query={}|undefined)=>this.context
|
|
12
|
-
.http
|
|
13
|
-
.get(!! query ? `${route}${Repository.queryString(query)}` : route)
|
|
14
|
-
|
|
15
|
-
get=(route='',query={}|undefined)=>this.#httpGet(route,query)
|
|
16
|
-
.then(value=>this.modelInstance(value))
|
|
17
|
-
|
|
18
|
-
list=(route='',query={}|undefined)=>this.#httpGet(route,query)
|
|
19
|
-
.then((values=[])=>values.map(value=>this.modelInstance(value)))
|
|
20
|
-
|
|
21
|
-
create=(route='',model=new Model())=>this.context
|
|
22
|
-
.http
|
|
23
|
-
.post(route,model.entity)
|
|
24
|
-
|
|
25
|
-
update=(route='',model=new Model())=>this.context
|
|
26
|
-
.http
|
|
27
|
-
.put(route,model.entity)
|
|
28
|
-
|
|
29
|
-
change=(route='',model=new Model())=>this.context
|
|
30
|
-
.http
|
|
31
|
-
.patch(route,model.entity)
|
|
32
|
-
|
|
33
|
-
delete=(id,model=new Model())=>this.context
|
|
34
|
-
.http
|
|
35
|
-
.delete(`/${id}`)
|
|
36
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import knex from "knex"
|
|
2
|
-
|
|
3
|
-
export default class Context{
|
|
4
|
-
#db
|
|
5
|
-
|
|
6
|
-
constructor(database=knex()){
|
|
7
|
-
this.#db= database
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
get db(){return this.#db}
|
|
11
|
-
|
|
12
|
-
unitOfWork(...repositories){
|
|
13
|
-
return this.#db.transaction().then(trx=>{
|
|
14
|
-
repositories.forEach(repo=>repo.context = trx)
|
|
15
|
-
|
|
16
|
-
const clean =()=>repositories.forEach(repo=>
|
|
17
|
-
repo.resetContext()
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
return {
|
|
21
|
-
done:()=>trx.commit().then(clean),
|
|
22
|
-
rollback:()=>trx.rollback().then(clean)
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static instance(database=knex()){ return new Context(database) }
|
|
28
|
-
|
|
29
|
-
async terraform(models=[]){
|
|
30
|
-
const promises = models.map(async model=>model.makeMe(this.#db))
|
|
31
|
-
|
|
32
|
-
await Promise.all(promises)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import knex from "knex";
|
|
2
|
-
import Model from "../../model/index.js";
|
|
3
|
-
import Context from "./context.js";
|
|
4
|
-
import Result from "../../model/result.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export default class Repository{
|
|
8
|
-
#name='';
|
|
9
|
-
|
|
10
|
-
myContext
|
|
11
|
-
|
|
12
|
-
Entity = Model
|
|
13
|
-
|
|
14
|
-
static anyOrError(model,err={code:0,message:''}){
|
|
15
|
-
if (!!model) return model
|
|
16
|
-
|
|
17
|
-
throw new Result(err);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static setOrEmpty(array=[],modeling=(e)=>e){
|
|
21
|
-
return array.length ? array.map(modeling) : []
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
constructor(model=Model,context=new Context())
|
|
25
|
-
{
|
|
26
|
-
this.#name = model.name
|
|
27
|
-
|
|
28
|
-
this.myContext=()=>context.db(this.#name)
|
|
29
|
-
|
|
30
|
-
this.resetContext=()=>{
|
|
31
|
-
this.myContext=()=>context.db(this.#name)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.Entity = model
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
set context(value=knex()){ this.myContext=()=>value(this.#name) }
|
|
38
|
-
|
|
39
|
-
create = (model = new Model())=>
|
|
40
|
-
this.myContext()
|
|
41
|
-
.insert({
|
|
42
|
-
...model.key,
|
|
43
|
-
...model.entity
|
|
44
|
-
})
|
|
45
|
-
.then(()=>model)
|
|
46
|
-
|
|
47
|
-
insert = (model = new Model())=>
|
|
48
|
-
this.myContext()
|
|
49
|
-
.insert(model.entity,Object.keys(model.key))
|
|
50
|
-
.then(ids=>{
|
|
51
|
-
model.key = ids[0]
|
|
52
|
-
return model
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
update = (model = new Model())=>
|
|
56
|
-
this.myContext()
|
|
57
|
-
.where(model.key)
|
|
58
|
-
.update(model.entity)
|
|
59
|
-
.then(affected=>affected > 0)
|
|
60
|
-
|
|
61
|
-
delete = (model = new Model())=>
|
|
62
|
-
this.myContext()
|
|
63
|
-
.where(model.key)
|
|
64
|
-
.del()
|
|
65
|
-
.then(affected=>affected > 0)
|
|
66
|
-
|
|
67
|
-
get = (model = new Model())=>
|
|
68
|
-
this.myContext()
|
|
69
|
-
.where(model.key)
|
|
70
|
-
.first()
|
|
71
|
-
.then(model=>Repository
|
|
72
|
-
.anyOrError(model,{code:404,message:'Not found'})
|
|
73
|
-
)
|
|
74
|
-
.then(this.Entity.build)
|
|
75
|
-
|
|
76
|
-
list = ()=>
|
|
77
|
-
this.myContext()
|
|
78
|
-
.select()
|
|
79
|
-
.then(result=>Repository
|
|
80
|
-
.setOrEmpty(result,this.Entity.build)
|
|
81
|
-
)
|
|
82
|
-
}
|