32 lines
772 B
TypeScript
32 lines
772 B
TypeScript
import * as mysql from 'mysql';
|
|
import IDatabaseSettings from '../interfaces/IDatabaseSettings';
|
|
|
|
let connection: mysql.Connection;
|
|
|
|
export function init(dbSettings: IDatabaseSettings) {
|
|
connection = mysql.createConnection(dbSettings);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
connection.connect(function(err) {
|
|
if (err) {
|
|
reject(err);
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
export function query(command: string): Promise<{ results: any, fields?: mysql.FieldInfo[] }> {
|
|
return new Promise((resolve, reject) => {
|
|
connection.query(command, function(error, results, fields) {
|
|
if (error !== null) {
|
|
reject(error);
|
|
}
|
|
resolve({ results, fields });
|
|
});
|
|
});
|
|
}
|
|
|
|
export function exit(): void {
|
|
connection.end();
|
|
}
|