1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/backend/model/sql/SQLConnection.ts

137 lines
3.7 KiB
TypeScript
Raw Normal View History

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