feat: add setupDatabase() static method for core-provided DB
This commit is contained in:
parent
d5484b074b
commit
d998ea2d85
|
|
@ -236,4 +236,29 @@ export class CoreService {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue