import "reflect-metadata";
import { DataSource } from "typeorm";

import * as dotenv from "dotenv";

import { fileURLToPath } from "url";
import { dirname, join } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

dotenv.config();

/**
 * Load config
 */
const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE, NODE_ENV } =
  process.env;

  /**
   * Configures data source
   */

export const AppDataSource = new DataSource({
  type: "postgres",
  host: DB_HOST,
  port: parseInt(DB_PORT || "5432"),
  username: DB_USERNAME,
  password: DB_PASSWORD,
  database: DB_DATABASE,

  synchronize: NODE_ENV === "dev" ? false : false,
//logging logs sql command on the terminal
  logging: NODE_ENV === "dev" ?   ["query", "error"] : false,
  entities: [join(__dirname, "/entities/*.{js, ts}")],
  migrations: [join(__dirname, "/migrations/*.js")],
  subscribers: [],
});