2018-03-31 03:30:30 +08:00
|
|
|
import 'reflect-metadata';
|
|
|
|
import {Connection, ConnectionOptions, createConnection, getConnection} from 'typeorm';
|
|
|
|
import {UserEntity} from './enitites/UserEntity';
|
|
|
|
import {UserRoles} from '../../../common/entities/UserDTO';
|
|
|
|
import {PhotoEntity} from './enitites/PhotoEntity';
|
|
|
|
import {DirectoryEntity} from './enitites/DirectoryEntity';
|
|
|
|
import {Config} from '../../../common/config/private/Config';
|
|
|
|
import {SharingEntity} from './enitites/SharingEntity';
|
|
|
|
import {DataBaseConfig, DatabaseType} from '../../../common/config/private/IPrivateConfig';
|
|
|
|
import {PasswordHelper} from '../PasswordHelper';
|
|
|
|
import {ProjectPath} from '../../ProjectPath';
|
|
|
|
import {VersionEntity} from './enitites/VersionEntity';
|
|
|
|
import {Logger} from '../../Logger';
|
2018-11-18 02:32:31 +08:00
|
|
|
import {MediaEntity} from './enitites/MediaEntity';
|
|
|
|
import {VideoEntity} from './enitites/VideoEntity';
|
2018-11-19 05:56:05 +08:00
|
|
|
import {DataStructureVersion} from '../../../common/DataStructureVersion';
|
2018-11-26 07:26:29 +08:00
|
|
|
import {FileEntity} from './enitites/FileEntity';
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
2017-07-21 05:37:10 +08:00
|
|
|
export class SQLConnection {
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
2018-01-21 09:36:38 +08:00
|
|
|
constructor() {
|
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> {
|
|
|
|
if (this.connection == null) {
|
2018-05-13 00:19:51 +08:00
|
|
|
const options: any = this.getDriver(Config.Server.database);
|
2018-12-09 18:37:12 +08:00
|
|
|
// options.name = 'main';
|
2017-10-20 00:08:07 +08:00
|
|
|
options.entities = [
|
|
|
|
UserEntity,
|
2018-11-26 07:26:29 +08:00
|
|
|
FileEntity,
|
2018-11-18 02:32:31 +08:00
|
|
|
MediaEntity,
|
2017-10-20 00:08:07 +08:00
|
|
|
PhotoEntity,
|
2018-11-18 02:32:31 +08:00
|
|
|
VideoEntity,
|
2017-12-18 10:34:07 +08:00
|
|
|
DirectoryEntity,
|
2018-01-21 09:36:38 +08:00
|
|
|
SharingEntity,
|
|
|
|
VersionEntity
|
2017-10-20 00:08:07 +08:00
|
|
|
];
|
2018-01-21 09:36:38 +08:00
|
|
|
options.synchronize = false;
|
2018-11-18 05:46:34 +08:00
|
|
|
// options.logging = 'all';
|
2017-10-20 00:08:07 +08:00
|
|
|
this.connection = await createConnection(options);
|
2018-01-31 09:01:16 +08:00
|
|
|
await SQLConnection.schemeSync(this.connection);
|
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-08 18:43:42 +08:00
|
|
|
public static async tryConnection(config: DataBaseConfig) {
|
2017-07-13 00:31:19 +08:00
|
|
|
try {
|
2018-03-31 03:30:30 +08:00
|
|
|
await getConnection('test').close();
|
2017-07-13 00:31:19 +08:00
|
|
|
} catch (err) {
|
|
|
|
}
|
2017-12-18 10:34:07 +08:00
|
|
|
const options: any = this.getDriver(config);
|
2018-03-31 03:30:30 +08:00
|
|
|
options.name = 'test';
|
2017-12-18 10:34:07 +08:00
|
|
|
options.entities = [
|
|
|
|
UserEntity,
|
2018-11-26 07:26:29 +08:00
|
|
|
FileEntity,
|
2018-11-18 02:32:31 +08:00
|
|
|
MediaEntity,
|
2017-12-18 10:34:07 +08:00
|
|
|
PhotoEntity,
|
2018-11-18 02:32:31 +08:00
|
|
|
VideoEntity,
|
2017-12-18 10:34:07 +08:00
|
|
|
DirectoryEntity,
|
2018-01-21 09:36:38 +08:00
|
|
|
SharingEntity,
|
|
|
|
VersionEntity
|
2017-12-18 10:34:07 +08:00
|
|
|
];
|
2018-01-21 09:36:38 +08:00
|
|
|
options.synchronize = false;
|
|
|
|
// options.logging = "all";
|
2017-10-20 00:08:07 +08:00
|
|
|
const conn = await createConnection(options);
|
2018-01-31 09:01:16 +08:00
|
|
|
await SQLConnection.schemeSync(conn);
|
2017-07-21 05:37:10 +08:00
|
|
|
await conn.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
public static async init(): Promise<void> {
|
|
|
|
const connection = await this.getConnection();
|
2018-05-13 00:19:51 +08:00
|
|
|
const userRepository = connection.getRepository(UserEntity);
|
|
|
|
const admins = await userRepository.find({role: UserRoles.Admin});
|
|
|
|
if (admins.length === 0) {
|
|
|
|
const a = new UserEntity();
|
2018-03-31 03:30:30 +08:00
|
|
|
a.name = 'admin';
|
|
|
|
a.password = PasswordHelper.cryptPassword('admin');
|
|
|
|
a.role = UserRoles.Admin;
|
|
|
|
await userRepository.save(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:01:16 +08:00
|
|
|
private static async schemeSync(connection: Connection) {
|
2018-01-21 09:36:38 +08:00
|
|
|
let version = null;
|
|
|
|
try {
|
|
|
|
version = await connection.getRepository(VersionEntity).findOne();
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
2018-11-19 05:56:05 +08:00
|
|
|
if (version && version.version === DataStructureVersion) {
|
2018-01-21 09:36:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.info('Updating database scheme');
|
2018-01-21 09:36:38 +08:00
|
|
|
if (!version) {
|
|
|
|
version = new VersionEntity();
|
|
|
|
}
|
2018-11-19 05:56:05 +08:00
|
|
|
version.version = DataStructureVersion;
|
2018-01-21 09:36:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
await connection.dropDatabase();
|
|
|
|
await connection.synchronize();
|
|
|
|
await connection.getRepository(VersionEntity).save(version);
|
|
|
|
}
|
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
private static getDriver(config: DataBaseConfig): ConnectionOptions {
|
|
|
|
let driver: ConnectionOptions = null;
|
2018-05-13 00:19:51 +08:00
|
|
|
if (config.type === DatabaseType.mysql) {
|
2017-07-21 05:37:10 +08:00
|
|
|
driver = {
|
2018-03-31 03:30:30 +08:00
|
|
|
type: 'mysql',
|
2017-07-08 18:43:42 +08:00
|
|
|
host: config.mysql.host,
|
|
|
|
port: 3306,
|
|
|
|
username: config.mysql.username,
|
|
|
|
password: config.mysql.password,
|
|
|
|
database: config.mysql.database
|
2017-07-21 05:37:10 +08:00
|
|
|
};
|
2018-05-13 00:19:51 +08:00
|
|
|
} else if (config.type === DatabaseType.sqlite) {
|
2017-07-21 05:37:10 +08:00
|
|
|
driver = {
|
2018-03-31 03:30:30 +08:00
|
|
|
type: 'sqlite',
|
2017-10-20 00:08:07 +08:00
|
|
|
database: ProjectPath.getAbsolutePath(config.sqlite.storage)
|
2017-07-21 05:37:10 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return driver;
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
public static async close() {
|
|
|
|
try {
|
2017-12-18 10:34:07 +08:00
|
|
|
if (this.connection != null) {
|
|
|
|
await this.connection.close();
|
2017-12-20 00:19:48 +08:00
|
|
|
this.connection = null;
|
2017-12-18 10:34:07 +08:00
|
|
|
}
|
2017-07-14 05:39:09 +08:00
|
|
|
} catch (err) {
|
2017-12-18 10:34:07 +08:00
|
|
|
console.error(err);
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|