2019-06-23 02:30:24 +02:00
|
|
|
import 'reflect-metadata';
|
|
|
|
import { Connection, createConnection } from 'typeorm';
|
2019-10-03 02:09:45 +02:00
|
|
|
import { throwError } from './error';
|
2019-06-23 02:30:24 +02:00
|
|
|
|
2019-10-03 02:09:45 +02:00
|
|
|
export enum Databases {
|
2019-07-26 23:05:29 +02:00
|
|
|
LIBRARY = 'library',
|
2019-06-23 02:30:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:05:29 +02:00
|
|
|
const connections: {
|
|
|
|
[key in Databases]?: Connection;
|
|
|
|
} = {};
|
|
|
|
|
2019-10-03 02:09:45 +02:00
|
|
|
Object.values(Databases).forEach((database: Databases) => {
|
|
|
|
createConnection(database)
|
|
|
|
.then((connection: Connection) => {
|
|
|
|
connections[database] = connection;
|
|
|
|
return connection.runMigrations();
|
|
|
|
})
|
|
|
|
.catch((reason: any) => {
|
|
|
|
throwError(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-26 23:05:29 +02:00
|
|
|
export function getConnection(database: Databases): Promise<Connection> {
|
|
|
|
if (connections[database] === undefined) {
|
|
|
|
return createConnection(database).then((connection: Connection) => {
|
|
|
|
connections[database] = connection;
|
|
|
|
return connection;
|
2019-06-23 02:30:24 +02:00
|
|
|
});
|
2019-07-26 23:05:29 +02:00
|
|
|
} else {
|
|
|
|
return Promise.resolve(connections[database]);
|
|
|
|
}
|
2019-06-23 02:30:24 +02:00
|
|
|
}
|