1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-01-28 05:36:45 +02:00
pigallery2/backend/model/sql/SQLConnection.ts

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-12-27 16:09:47 +01:00
import "reflect-metadata";
2017-10-19 12:08:07 -04:00
import {Connection, ConnectionOptions, createConnection, getConnection} from "typeorm";
2016-12-27 16:09:47 +01:00
import {UserEntity} from "./enitites/UserEntity";
import {UserRoles} from "../../../common/entities/UserDTO";
2017-10-19 12:08:07 -04:00
import {PhotoEntity} from "./enitites/PhotoEntity";
import {DirectoryEntity} from "./enitites/DirectoryEntity";
2017-06-04 15:25:08 +02:00
import {Config} from "../../../common/config/private/Config";
2017-07-03 19:17:49 +02:00
import {SharingEntity} from "./enitites/SharingEntity";
import {DataBaseConfig, DatabaseType} from "../../../common/config/private/IPrivateConfig";
2017-07-09 22:36:25 +02:00
import {PasswordHelper} from "../PasswordHelper";
import {ProjectPath} from "../../ProjectPath";
2016-12-27 16:09:47 +01:00
export class SQLConnection {
2016-12-27 16:09:47 +01:00
2017-07-03 19:17:49 +02:00
constructor() {
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-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) {
2017-10-19 12:08:07 -04:00
let options: any = this.getDriver(Config.Server.database);
options.name = "main";
options.entities = [
UserEntity,
PhotoEntity,
DirectoryEntity,
2017-10-19 12:08:07 -04:00
SharingEntity
];
options.synchronize = true;
// options.logging = "all" ;
2017-10-19 12:08:07 -04:00
this.connection = await createConnection(options);
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 {
await getConnection("test").close();
} catch (err) {
}
const options: any = this.getDriver(config);
2017-10-19 12:08:07 -04:00
options.name = "test";
options.entities = [
UserEntity,
PhotoEntity,
DirectoryEntity,
SharingEntity
];
options.synchronize = true;
options.logging = "all";
2017-10-19 12:08:07 -04:00
const conn = await createConnection(options);
await conn.close();
return true;
}
2017-10-19 12:08:07 -04: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";
a.password = PasswordHelper.cryptPassword("admin");
a.role = UserRoles.Admin;
await userRepository.save(a);
}
}
private static getDriver(config: DataBaseConfig): ConnectionOptions {
let driver: ConnectionOptions = null;
if (config.type == DatabaseType.mysql) {
driver = {
2017-07-08 12:43:42 +02:00
type: "mysql",
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 = {
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-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
}