Skip to content

Commit 48fca71

Browse files
authored
Merge pull request #2 from Gambitier/database-setup
Database setup
2 parents 3854dce + 745a52b commit 48fca71

25 files changed

+1637
-50
lines changed

Projects/BooksLibrary/.env.sample

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
BASE_URL=http://localhost:7272/api/v1
22
PORT=7272
33
NODE_ENV=development
4-
API_VERSION=0.0.1
4+
API_VERSION=0.0.1
5+
6+
DB_USER_NAME = someusername
7+
DB_USER_PASSWORD = somepass
8+
DB_NAME = somename
9+
DB_HOST = host

Projects/BooksLibrary/package-lock.json

Lines changed: 873 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Projects/BooksLibrary/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "src/index.ts",
66
"scripts": {
77
"build": "tsc",
8-
"start": "tsc && ts-node --files ./src/index.ts",
9-
"start:dev": "tsc && ts-node --files ./src/index.ts --watch",
8+
"rebuild": "rimraf dist && tsc",
9+
"start": "ts-node -r tsconfig-paths/register src/index.ts",
1010
"lint:fix": "eslint ./src/** --fix"
1111
},
1212
"author": "akash jadhav",
@@ -17,11 +17,14 @@
1717
"@types/express-fileupload": "^1.2.2",
1818
"@types/jsonwebtoken": "^8.5.8",
1919
"@types/node": "^17.0.21",
20+
"@types/uuid": "^8.3.4",
2021
"@typescript-eslint/eslint-plugin": "^5.13.0",
2122
"@typescript-eslint/parser": "^5.13.0",
2223
"eslint": "^8.10.0",
2324
"eslint-config-google": "^0.14.0",
2425
"prettier": "2.5.1",
26+
"ts-node": "^10.7.0",
27+
"tsconfig-paths": "^3.13.0",
2528
"typescript": "^4.6.2"
2629
},
2730
"dependencies": {
@@ -30,7 +33,10 @@
3033
"express": "^4.17.3",
3134
"express-fileupload": "^1.3.1",
3235
"jsonwebtoken": "^8.5.1",
36+
"pg": "^8.7.3",
3337
"reflect-metadata": "^0.1.13",
38+
"sequelize": "^6.17.0",
39+
"sequelize-typescript": "^2.1.3",
3440
"tsyringe": "^4.6.0"
3541
}
3642
}

Projects/BooksLibrary/src/app.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"Database": {
88
"Type": "SQL",
99
"ORM": "Sequelize",
10-
"Flavour": "MySQL"
10+
"Flavour": "PostGreSQL"
1111
},
1212
"FileStorage": {
1313
"Provider": "AWS-S3"

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

Projects/BooksLibrary/src/config/configuration.manager.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
import { AuthenticationType, AuthorizationType, Configurations } from './configuration.types';
2-
import * as AppConfig from '../app.config.json';
1+
import {
2+
AuthenticationType,
3+
AuthorizationType,
4+
Configurations,
5+
DatabaseFlavour,
6+
DatabaseORM,
7+
DatabaseType,
8+
} from './configuration.types';
9+
import * as configuration from '../app.config.json';
310

411
export class ConfigurationManager {
512
static _config: Configurations = null;
613

14+
public static DatabaseFlavour = (): DatabaseFlavour => {
15+
return ConfigurationManager._config.Database.Flavour;
16+
};
17+
18+
public static DatabaseType = (): DatabaseType => {
19+
return ConfigurationManager._config.Database.Type;
20+
};
21+
722
public static loadConfigurations = (): void => {
823
ConfigurationManager._config = {
924
BaseUrl: process.env.BASE_URL,
1025
SystemIdentifier: '',
1126
MaxUploadFileSize: 0,
1227
Auth: {
13-
Authentication: AppConfig.Auth.Authentication as AuthenticationType,
14-
Authorization: AppConfig.Auth.Authorization as AuthorizationType,
28+
Authentication: configuration.Auth.Authentication as AuthenticationType,
29+
Authorization: configuration.Auth.Authorization as AuthorizationType,
30+
},
31+
Database: {
32+
Type: configuration.Database.Type as DatabaseType,
33+
ORM: configuration.Database.ORM as DatabaseORM,
34+
Flavour: configuration.Database.Flavour as DatabaseFlavour,
1535
},
1636
};
1737
};

Projects/BooksLibrary/src/config/configuration.types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
export type AuthorizationType = 'Custom'; //TBD: Other options need to be supported
22
export type AuthenticationType = 'Custom'; //TBD: Other options need to be supported
3+
4+
export type DatabaseFlavour = 'MySQL' | 'PostGreSQL' | 'MongoDB';
5+
export type DatabaseType = 'SQL' | 'NoSQL';
6+
export type DatabaseORM = 'Sequelize' | 'Knex' | 'Mongoose';
7+
8+
export interface DatabaseConfig {
9+
Type: DatabaseType;
10+
ORM: DatabaseORM;
11+
Flavour: DatabaseFlavour;
12+
}
13+
314
export interface Configurations {
415
SystemIdentifier: string;
516
BaseUrl: string;
617
MaxUploadFileSize: number;
718
Auth: AuthConfig;
19+
Database: DatabaseConfig;
820
}
921
export interface AuthConfig {
1022
Authentication: AuthenticationType;
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+
throw error;
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ConfigurationManager } from 'config/configuration.manager';
2+
import { DependencyContainer } from 'tsyringe';
3+
import { SQLInjector } from './sql/sql.injector';
4+
5+
export class DatabaseInjector {
6+
static registerInjections(container: DependencyContainer) {
7+
//
8+
const databaseType = ConfigurationManager.DatabaseType();
9+
if (databaseType === 'SQL') {
10+
SQLInjector.registerInjections(container);
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)