2016-12-27 23:09:47 +08:00
|
|
|
import "reflect-metadata";
|
2017-07-13 00:31:19 +08:00
|
|
|
import {Connection, createConnection, getConnection} from "typeorm";
|
2016-12-27 23:09:47 +08:00
|
|
|
import {UserEntity} from "./enitites/UserEntity";
|
|
|
|
import {UserRoles} from "../../../common/entities/UserDTO";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {PhotoEntity, PhotoMetadataEntity} from "./enitites/PhotoEntity";
|
2016-12-28 19:30:26 +08:00
|
|
|
import {DirectoryEntity} from "./enitites/DirectoryEntity";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../../common/config/private/Config";
|
2017-07-04 01:17:49 +08:00
|
|
|
import {SharingEntity} from "./enitites/SharingEntity";
|
2017-07-08 18:43:42 +08:00
|
|
|
import {DataBaseConfig} from "../../../common/config/private/IPrivateConfig";
|
2017-07-10 04:36:25 +08:00
|
|
|
import {PasswordHelper} from "../PasswordHelper";
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
export class MySQLConnection {
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
constructor() {
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
private static connection: Connection = null;
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public static async getConnection(): Promise<Connection> {
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
if (this.connection == null) {
|
|
|
|
this.connection = await createConnection({
|
2017-07-08 18:43:42 +08:00
|
|
|
name: "main",
|
2017-07-04 01:17:49 +08:00
|
|
|
driver: {
|
|
|
|
type: "mysql",
|
|
|
|
host: Config.Server.database.mysql.host,
|
|
|
|
port: 3306,
|
|
|
|
username: Config.Server.database.mysql.username,
|
|
|
|
password: Config.Server.database.mysql.password,
|
|
|
|
database: Config.Server.database.mysql.database
|
|
|
|
},
|
|
|
|
entities: [
|
|
|
|
UserEntity,
|
|
|
|
DirectoryEntity,
|
|
|
|
PhotoMetadataEntity,
|
|
|
|
PhotoEntity,
|
|
|
|
SharingEntity
|
|
|
|
],
|
|
|
|
autoSchemaSync: true,
|
|
|
|
logging: {
|
|
|
|
logQueries: true,
|
|
|
|
logOnlyFailedQueries: true,
|
|
|
|
logFailedQueryError: true,
|
|
|
|
logSchemaCreation: true
|
|
|
|
}
|
|
|
|
});
|
2016-12-27 23:09:47 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
return this.connection;
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
public static async tryConnection(config: DataBaseConfig) {
|
2017-07-13 00:31:19 +08:00
|
|
|
try {
|
|
|
|
await getConnection("test").close();
|
|
|
|
} catch (err) {
|
|
|
|
}
|
2017-07-08 18:43:42 +08:00
|
|
|
const conn = await createConnection({
|
|
|
|
name: "test",
|
|
|
|
driver: {
|
|
|
|
type: "mysql",
|
|
|
|
host: config.mysql.host,
|
|
|
|
port: 3306,
|
|
|
|
username: config.mysql.username,
|
|
|
|
password: config.mysql.password,
|
|
|
|
database: config.mysql.database
|
|
|
|
},
|
|
|
|
entities: [
|
|
|
|
UserEntity,
|
|
|
|
DirectoryEntity,
|
|
|
|
PhotoMetadataEntity,
|
|
|
|
PhotoEntity,
|
|
|
|
SharingEntity
|
|
|
|
],
|
|
|
|
autoSchemaSync: true,
|
|
|
|
logging: {
|
|
|
|
logQueries: true,
|
|
|
|
logOnlyFailedQueries: true,
|
|
|
|
logFailedQueryError: true,
|
|
|
|
logSchemaCreation: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await conn.close();
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
public static async init(): Promise<void> {
|
|
|
|
const connection = await this.getConnection();
|
|
|
|
let userRepository = connection.getRepository(UserEntity);
|
|
|
|
let admins = await userRepository.find({role: UserRoles.Admin});
|
|
|
|
if (admins.length == 0) {
|
|
|
|
let a = new UserEntity();
|
|
|
|
a.name = "admin";
|
2017-07-11 15:01:59 +08:00
|
|
|
a.password = PasswordHelper.cryptPassword("admin");
|
2017-07-08 18:43:42 +08:00
|
|
|
a.role = UserRoles.Admin;
|
|
|
|
await userRepository.persist(a);
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|