feat: add setupDatabase() static method for core-provided DB

This commit is contained in:
Kyryll O 2026-05-26 22:16:08 +03:00
parent d5484b074b
commit d998ea2d85
1 changed files with 25 additions and 0 deletions

View File

@ -236,4 +236,29 @@ export class CoreService {
details: payload.details, details: payload.details,
}); });
} }
/**
* Отримати назву БД, наданої ядром (має сенс після виклику setupDatabase)
*/
getDbName(): string {
return process.env.PLUGIN_DB_NAME || process.env.DB_DATABASE || 'ultimate_crm';
}
/**
* Зареєструвати БД плагіна в ядрі (створює БД p_<id>, якщо ще не існує).
* Викликати ДО створення NestJS app, щоб TypeORM міг підхопити PLUGIN_DB_NAME.
*/
static async setupDatabase(options: { pluginId: string }): Promise<string> {
const coreUrl = process.env.CORE_BACKEND_URL || 'http://localhost:3001';
const response = await fetch(`${coreUrl}/api/db/setup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ pluginId: options.pluginId }),
});
if (!response.ok) {
throw new Error(`Database setup failed: ${response.statusText}`);
}
const data = await response.json();
return data.dbName;
}
} }