Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {Sequelize} from 'sequelize';
import {Options} from 'sequelize';
import config from '@config';
import debug from 'debug';
const httpLog = debug('group-car:http');
/**
* An extension of the {@link Sequelize} class which also
* provides a simple promise based function to check
* if the database is reachable or not.
*/
export class Database extends Sequelize {
private username: string;
private password : string;
private options: Options | undefined;
/**
* Creates an instance of this object
* @param database Name of the database
* @param username Username for the database
* @param password Password for the user of the database
* @param options Optional options
*/
constructor(database: string,
username: string,
password: string,
options: Options | undefined) {
super(database, username, password, options);
this.username = username;
this.password = password;
this.options = options;
}
/**
* Returns a promise which either resolves to true or false,
* depending on if the database is reachable.
* @returns A promise which resolves to true or false depending
* on the availability of the database
*/
async isAvailable() {
httpLog('Check connection to %s:%s', this.options!.host, this.options!.port);
try {
await this.authenticate();
httpLog('Connection test successful');
return true;
} catch (e) {
if (e !== undefined) {
httpLog('Connection test failed because $s', e);
} else {
httpLog('Connection test failed');
}
return false;
}
}
}
const database = new Database(config.database.sequelize.database,
config.database.sequelize.username,
config.database.sequelize.password || '',
config.database.sequelize as unknown as Options);
// If currently in environment sync the database
let syncPromise: Promise<void>;
Eif (config.database.withFlush) {
syncPromise = database.sync({force: true, logging: false}).then(() => {
httpLog('Flushed database');
});
} else {
syncPromise = Promise.resolve();
}
export {
syncPromise,
}
export default database;
|