|
| 1 | +import { Logger } from 'common/logger'; |
| 2 | +import { IDatabaseConnector } from 'database/database.connector.interface'; |
| 3 | +import { DbConfig } from './database.config'; |
| 4 | +import { Dialect } from 'sequelize'; |
| 5 | +import { Sequelize } from 'sequelize-typescript'; |
| 6 | +import { ConfigurationManager } from 'config/configuration.manager'; |
| 7 | +import { PostgresqlClient } from './dilect.clients/postgresql.client'; |
| 8 | + |
| 9 | +export class DatabaseConnector_Sequelize implements IDatabaseConnector { |
| 10 | + private _sequelize: Sequelize = null; |
| 11 | + |
| 12 | + private getDialect = (): Dialect => { |
| 13 | + let dialect: Dialect = 'postgres'; |
| 14 | + const flavour = ConfigurationManager.DatabaseFlavour(); |
| 15 | + |
| 16 | + if (flavour === 'MySQL') { |
| 17 | + dialect = 'mysql'; |
| 18 | + } |
| 19 | + if (flavour === 'PostGreSQL') { |
| 20 | + dialect = 'postgres'; |
| 21 | + } |
| 22 | + |
| 23 | + return dialect; |
| 24 | + }; |
| 25 | + |
| 26 | + connect = async (): Promise<boolean> => { |
| 27 | + try { |
| 28 | + const config = DbConfig.config; |
| 29 | + const dialect: Dialect = this.getDialect(); |
| 30 | + const modelsPath = [__dirname + '/models']; |
| 31 | + const options = { |
| 32 | + host: config.host, |
| 33 | + dialect: dialect, |
| 34 | + models: modelsPath, |
| 35 | + pool: { |
| 36 | + max: config.pool.max, |
| 37 | + min: config.pool.min, |
| 38 | + acquire: config.pool.acquire, |
| 39 | + idle: config.pool.idle, |
| 40 | + }, |
| 41 | + logging: false, //TODO: Please provide a function here to handle logging... |
| 42 | + }; |
| 43 | + |
| 44 | + this._sequelize = new Sequelize(config.database, config.username, config.password, options); |
| 45 | + |
| 46 | + Logger.instance().log(`Connecting to database '${config.database}' ...`); |
| 47 | + Logger.instance().log(`Database flavour: ${config.dialect}`); |
| 48 | + Logger.instance().log(`Database host: ${config.host}`); |
| 49 | + |
| 50 | + await this.createDatabase(); |
| 51 | + // test the connection by trying to authenticate to database server |
| 52 | + // with the config that we have defined above |
| 53 | + await this._sequelize.authenticate(); |
| 54 | + // alter: true is not adviced to be used on prod env |
| 55 | + await this._sequelize.sync({ alter: true }); |
| 56 | + |
| 57 | + return true; |
| 58 | + } catch (err) { |
| 59 | + Logger.instance().log(err.message); |
| 60 | + return false; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + sync(): Promise<boolean> { |
| 65 | + throw new Error('Method not implemented.'); |
| 66 | + } |
| 67 | + |
| 68 | + private getClient = () => { |
| 69 | + const flavour = ConfigurationManager.DatabaseFlavour(); |
| 70 | + |
| 71 | + if (flavour === 'PostGreSQL') { |
| 72 | + return PostgresqlClient; |
| 73 | + } |
| 74 | + |
| 75 | + throw new Error('Only PostGreSQL is supported for now!'); |
| 76 | + }; |
| 77 | + |
| 78 | + createDatabase = async (): Promise<boolean> => { |
| 79 | + try { |
| 80 | + const client = this.getClient(); |
| 81 | + await client.createDb(); |
| 82 | + return true; |
| 83 | + } catch (err) { |
| 84 | + Logger.instance().log(err.message); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + dropDatabase(): Promise<boolean> { |
| 89 | + throw new Error('Method not implemented.'); |
| 90 | + } |
| 91 | + |
| 92 | + executeQuery(query: string): Promise<boolean> { |
| 93 | + throw new Error('Method not implemented.'); |
| 94 | + } |
| 95 | + |
| 96 | + migrate(): Promise<boolean> { |
| 97 | + throw new Error('Method not implemented.'); |
| 98 | + } |
| 99 | +} |
0 commit comments