@@ -5,9 +5,13 @@ import express from 'express';
55import jsonServer = require( 'json-server' ) ;
66import { StorageAdapter } from '../storage/storage' ;
77import { ApiSpecification } from '../specifications/apispecification' ;
8+ import { JSONValidator } from '../validations/json.validator' ;
9+ import { Output } from '../utils/output' ;
10+
811export class CoreApp {
912 storageAdapter : StorageAdapter ;
1013 static storage = { } as lowdb . AdapterAsync ;
14+ static adapter = { } as lowdb . LowdbAsync < { } > ;
1115 logger = new Logger ( ) . logger ;
1216 appConfig : AppConfig ;
1317 protected server : express . Express ;
@@ -27,19 +31,38 @@ export class CoreApp {
2731
2832 async setup ( ) : Promise < void > {
2933 await this . setupStorage ( ) ;
30- const json = await this . setupApp ( ) ;
31- await this . setupSwagger ( json ) ;
32- await this . setupRoutes ( ) ;
34+ const json = await this . getJSON ( ) ;
35+ const isValid = this . validateJSON ( json ) ;
36+ if ( isValid ) {
37+ await this . setupApp ( ) ;
38+ this . setupSwagger ( json ) ;
39+ await this . setupRoutes ( ) ;
40+ } else {
41+ Output . setError ( 'provided json is not valid - see validation checks' ) ;
42+ throw Error ( 'provided json is not valid - see validation checks' ) ;
43+ }
3344 }
3445
3546 protected async setupStorage ( ) {
3647 CoreApp . storage = await this . storageAdapter . init ( ) ;
48+ CoreApp . adapter = await lowdb . default ( CoreApp . storage ) ;
3749 }
3850
39- protected async setupApp ( ) : Promise < object > {
40- const { middlewares, router, adapter } = await this . initializeLayers ( ) ;
51+ protected async setupApp ( ) : Promise < void > {
52+ const { middlewares, router } = await this . initializeLayers ( ) ;
4153 this . setupServer ( middlewares , router ) ;
42- const json = await adapter . getState ( ) ;
54+ }
55+
56+ protected validateJSON ( db : { } ) : boolean {
57+ let isValid = true ;
58+ if ( this . appConfig . enableJSONValidation ) {
59+ isValid = JSONValidator . validate ( db ) ;
60+ }
61+ return isValid ;
62+ }
63+
64+ protected async getJSON ( ) : Promise < object > {
65+ const json = await CoreApp . adapter . getState ( ) ;
4366 return json ;
4467 }
4568
@@ -56,13 +79,18 @@ export class CoreApp {
5679 }
5780
5881 protected async initializeLayers ( ) {
59- this . logger . trace ( 'initLayer: ' + JSON . stringify ( CoreApp . storage ) ) ;
60- const adapter = await lowdb . default ( CoreApp . storage ) ;
61- const router = jsonServer . router ( adapter ) ;
82+ if (
83+ CoreApp . adapter &&
84+ Object . entries ( CoreApp . adapter ) . length === 0 &&
85+ CoreApp . adapter . constructor === Object
86+ ) {
87+ CoreApp . adapter = await lowdb . default ( CoreApp . storage ) ;
88+ }
89+ const router = jsonServer . router ( CoreApp . adapter ) ;
6290 const middlewares = jsonServer . defaults ( {
6391 readOnly : this . appConfig . readOnly ,
6492 } ) ;
65- return { middlewares, router, adapter } ;
93+ return { middlewares, router } ;
6694 }
6795
6896 protected setupServer (
0 commit comments