import { config } from "dotenv";
import { join } from "path";
import { DataSource } from "typeorm";

// CLI config for the control plane (mbinvtaller) migrations — a separate
// history from the tenant migrations in ../migrations. Never share a
// migrations table between the control plane and a tenant database.
config({
  path: [join(process.cwd(), ".env"), join(process.cwd(), "..", ".env")],
});

export default new DataSource({
  type: "mysql",
  host: process.env.CONTROL_PLANE_DB_HOST,
  port: parseInt(process.env.CONTROL_PLANE_DB_PORT || "3306", 10),
  username: process.env.CONTROL_PLANE_DB_USERNAME,
  password: process.env.CONTROL_PLANE_DB_PASSWORD,
  database: process.env.CONTROL_PLANE_DB_DATABASE,
  entities: [join(__dirname, "..", "control-plane", "entities", "*.entity{.ts,.js}")],
  migrations: [join(__dirname, "..", "control-plane", "migrations", "*{.ts,.js}")],
  synchronize: false,
});
