"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.closeDatabase = exports.runMigrations = exports.initializeDatabase = exports.AppDataSource = void 0;
require("reflect-metadata");
const typeorm_1 = require("typeorm");
const env_1 = require("../../main/config/env");
const device_entity_1 = require("./entities/device.entity");
const wallet_entity_1 = require("./entities/wallet.entity");
const address_entity_1 = require("./entities/address.entity");
const network_entity_1 = require("./entities/network.entity");
const token_entity_1 = require("./entities/token.entity");
const transaction_entity_1 = require("./entities/transaction.entity");
const isDevelopment = env_1.env.NODE_ENV === 'development';
const isTest = env_1.env.NODE_ENV === 'test';
exports.AppDataSource = new typeorm_1.DataSource({
    type: 'mysql',
    host: env_1.env.DB_HOST,
    port: env_1.env.DB_PORT,
    username: env_1.env.DB_USER,
    password: env_1.env.DB_PASS,
    database: env_1.env.DB_NAME,
    synchronize: isDevelopment || isTest,
    logging: isDevelopment ? ['error'] : false,
    entities: [device_entity_1.DeviceEntity, wallet_entity_1.WalletEntity, address_entity_1.AddressEntity, network_entity_1.NetworkEntity, token_entity_1.TokenEntity, transaction_entity_1.TransactionEntity],
    migrations: [`${__dirname}/migrations/*.ts`],
    subscribers: [],
});
const initializeDatabase = async () => {
    if (exports.AppDataSource.isInitialized) {
        return exports.AppDataSource;
    }
    return exports.AppDataSource.initialize();
};
exports.initializeDatabase = initializeDatabase;
const runMigrations = async () => {
    if (!exports.AppDataSource.isInitialized) {
        await (0, exports.initializeDatabase)();
    }
    await exports.AppDataSource.runMigrations();
};
exports.runMigrations = runMigrations;
const closeDatabase = async () => {
    if (exports.AppDataSource.isInitialized) {
        await exports.AppDataSource.destroy();
    }
};
exports.closeDatabase = closeDatabase;
//# sourceMappingURL=data-source.js.map