Skip to content

Commit b036ac2

Browse files
committed
refactor(server): update setup function to to validation immediately after loading the storage layer
1 parent 2e465ed commit b036ac2

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/server/app/core.app.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import jsonServer = require('json-server');
66
import { StorageAdapter } from '../storage/storage';
77
import { ApiSpecification } from '../specifications/apispecification';
88
import { JSONValidator } from '../validations/json.validator';
9+
import { Output } from '../utils/output';
910

1011
export class CoreApp {
1112
storageAdapter: StorageAdapter;
1213
static storage = {} as lowdb.AdapterAsync;
14+
static adapter = {} as lowdb.LowdbAsync<{}>;
1315
logger = new Logger().logger;
1416
appConfig: AppConfig;
1517
protected server: express.Express;
@@ -29,27 +31,39 @@ export class CoreApp {
2931

3032
async setup(): Promise<void> {
3133
await this.setupStorage();
32-
const json = await this.setupApp();
33-
this.validateJSON(json);
34+
const json = await this.getJSON();
35+
const isValid = this.validateJSON(json);
36+
if (isValid) {
37+
await this.setupApp();
3438
this.setupSwagger(json);
3539
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+
}
3644
}
3745

3846
protected async setupStorage() {
3947
CoreApp.storage = await this.storageAdapter.init();
48+
CoreApp.adapter = await lowdb.default(CoreApp.storage);
4049
}
4150

42-
protected async setupApp(): Promise<object> {
43-
const { middlewares, router, adapter } = await this.initializeLayers();
51+
protected async setupApp(): Promise<void> {
52+
const { middlewares, router } = await this.initializeLayers();
4453
this.setupServer(middlewares, router);
45-
const json = await adapter.getState();
46-
return json;
4754
}
4855

49-
protected validateJSON(db: {}): void {
50-
if (this.appConfig.enableSwagger) {
51-
const validator = JSONValidator.validate(db);
56+
protected validateJSON(db: {}): boolean {
57+
let isValid = true;
58+
if (this.appConfig.enableJSONValidation) {
59+
isValid = JSONValidator.validate(db);
5260
}
61+
return isValid;
62+
}
63+
64+
protected async getJSON(): Promise<object> {
65+
const json = await CoreApp.adapter.getState();
66+
return json;
5367
}
5468

5569
protected setupSwagger(db: {}): void {
@@ -65,12 +79,14 @@ export class CoreApp {
6579
}
6680

6781
protected async initializeLayers() {
68-
const adapter = await lowdb.default(CoreApp.storage);
69-
const router = jsonServer.router(adapter);
82+
if (CoreApp.adapter && Object.entries(CoreApp.adapter).length === 0 && CoreApp.adapter.constructor === Object) {
83+
CoreApp.adapter = await lowdb.default(CoreApp.storage);
84+
}
85+
const router = jsonServer.router(CoreApp.adapter);
7086
const middlewares = jsonServer.defaults({
7187
readOnly: this.appConfig.readOnly,
7288
});
73-
return { middlewares, router, adapter };
89+
return { middlewares, router};
7490
}
7591

7692
protected setupServer(

0 commit comments

Comments
 (0)