Skip to content

Commit d2ea022

Browse files
committed
defined database connector interface
1 parent 3854dce commit d2ea022

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Projects/BooksLibrary/src/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export default class Application {
3737
//Load the modules
3838
await Loader.init();
3939

40+
if (process.env.NODE_ENV === 'test') {
41+
await Loader.databaseConnector.dropDatabase();
42+
}
43+
44+
//Connect with database
45+
await Loader.databaseConnector.init();
46+
4047
//Set-up middlewares
4148
await this.setupMiddlewares();
4249

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface IDatabaseConnector {
2+
connect(): Promise<boolean>;
3+
4+
sync(): Promise<boolean>;
5+
6+
createDatabase(): Promise<boolean>;
7+
8+
dropDatabase(): Promise<boolean>;
9+
10+
executeQuery(query: string): Promise<boolean>;
11+
12+
migrate(): Promise<boolean>;
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Logger } from 'common/logger';
2+
import { inject, injectable } from 'tsyringe';
3+
import { IDatabaseConnector } from './database.connector.interface';
4+
5+
@injectable()
6+
export class DatabaseConnector {
7+
constructor(@inject('IDatabaseConnector') private _db: IDatabaseConnector) {}
8+
9+
public init = async (): Promise<boolean> => {
10+
try {
11+
await this._db.connect();
12+
return true;
13+
} catch (error) {
14+
Logger.instance().log('Create database error: ' + error.message);
15+
return false;
16+
}
17+
};
18+
19+
public dropDatabase = async (): Promise<boolean> => {
20+
try {
21+
await this._db.dropDatabase();
22+
return true;
23+
} catch (error) {
24+
Logger.instance().log('Drop database error: ' + error.message);
25+
return false;
26+
}
27+
};
28+
}

Projects/BooksLibrary/src/startup/loader.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { container, DependencyContainer } from 'tsyringe';
22
import { Authenticator } from 'auth/authenticator';
33
import { Logger } from '../common/logger';
44
import { Authorizer } from 'auth/authorizer';
5+
import { DatabaseConnector } from 'database/database.connector';
56

67
export class Loader {
78
private static _container: DependencyContainer = container;
@@ -22,9 +23,16 @@ export class Loader {
2223
return Loader._authenticator;
2324
}
2425

26+
private static _databaseConnector: DatabaseConnector = null;
27+
28+
public static get databaseConnector() {
29+
return Loader._databaseConnector;
30+
}
31+
2532
public static init = async (): Promise<boolean> => {
2633
try {
2734
//Register injections here...
35+
Loader._databaseConnector = container.resolve(DatabaseConnector);
2836

2937
return true;
3038
} catch (error) {

0 commit comments

Comments
 (0)