Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import { DataSource } from 'typeorm';
import { loadCliEnv } from './load-cli-env';
import { sqliteDataMainPathCollision } from '../config/env.validation';
// Load environment variables with the app's precedence (mirrors data-source.ts / main.ts).
loadCliEnv();
// Same guard as data-source.ts: the TypeORM CLI never runs ConfigModule's validate(), so the
// SQLite main/data file collision check from env.validation is re-applied here — a shared broken
// env (DATABASE_NAME resolving to the main file) must refuse BOTH migration entry points, not just
// the data one.
const sqlitePathCollision = sqliteDataMainPathCollision(process.env);
if (sqlitePathCollision) {
throw new Error(sqlitePathCollision);
}
/**
* Standalone TypeORM CLI DataSource for the MAIN connection (auth + audit).
*
* The app runs the main connection as a separate, ALWAYS-SQLite connection (app.module.ts), distinct
* from the pluggable data connection. The default data-source.ts CLI only manages the data
* connection's migrations, so without this the CLI could not run/generate the main-owned migrations
* (migrations-main), which also run at every boot.
*
* Mirrors the runtime main connection exactly: SQLite at ./data/main.sqlite, auth/audit entities,
* migrations-main. synchronize is always false here — the CLI manages schema via migrations.
*
* Usage: `npm run migration:run:main` (dev) / `migration:run:main:prod` (compiled).
*/
const mainDataSource = new DataSource({
type: 'better-sqlite3',
// Mirrors the runtime main path (configuration.ts) — MAIN_DATABASE_NAME overrides the default
// ./data/main.sqlite (e.g. e2e points it at a temp file), so the CLI and the app never target
// different main databases.
database: process.env.MAIN_DATABASE_NAME || './data/main.sqlite',
entities: [__dirname + '/../modules/auth/**/*.entity{.ts,.js}', __dirname + '/../modules/audit/**/*.entity{.ts,.js}'],
migrations: [__dirname + '/migrations-main/*{.ts,.js}'],
synchronize: false,
logging: process.env.DATABASE_LOGGING === 'true',
});
export default mainDataSource;