45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import Knex from 'knex';
|
|
import type { Knex as KnexType } from 'knex';
|
|
import { Model } from 'objection';
|
|
import { CentralTenant, CentralDomain, CentralUser } from '../models/central.model';
|
|
|
|
let centralKnex: KnexType | null = null;
|
|
|
|
/**
|
|
* Get or create a Knex instance for the central database
|
|
* This is used for Objection models that work with central entities
|
|
*/
|
|
export function getCentralKnex(): KnexType {
|
|
if (!centralKnex) {
|
|
const centralDbUrl = process.env.CENTRAL_DATABASE_URL;
|
|
|
|
if (!centralDbUrl) {
|
|
throw new Error('CENTRAL_DATABASE_URL environment variable is not set');
|
|
}
|
|
|
|
centralKnex = Knex({
|
|
client: 'mysql2',
|
|
connection: centralDbUrl,
|
|
pool: {
|
|
min: 2,
|
|
max: 10,
|
|
},
|
|
});
|
|
|
|
// Bind Objection models to this knex instance
|
|
Model.knex(centralKnex);
|
|
}
|
|
|
|
return centralKnex;
|
|
}
|
|
|
|
/**
|
|
* Initialize central models with the knex instance
|
|
*/
|
|
export function initCentralModels() {
|
|
const knex = getCentralKnex();
|
|
CentralTenant.knex(knex);
|
|
CentralDomain.knex(knex);
|
|
CentralUser.knex(knex);
|
|
}
|