Skip to content

Commit bfdf75e

Browse files
committed
fix: correct dynamic import for the database connectors
1 parent 609ed46 commit bfdf75e

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

adminforth/index.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,50 @@ class AdminForth implements IAdminForth {
488488
const connectorModule = await import(`@adminforth/connector-${connectorName}`);
489489
return connectorModule.default;
490490
} catch (e) {
491-
throw new Error(`____________________
492-
Error while importing ${connectorName} connector: ${e}.
493-
If you want to use ${connectorName} data source, please install @adminforth/connector-${connectorName} package.
494-
____________________
491+
throw new Error(`
492+
╔════════════════════════════════════════════════════════════════════════════╗
493+
║ ║
494+
║ ❌ CONNECTOR IMPORT ERROR ║
495+
║ ──────────────────────────────────────────────────────────────────────── ║
496+
║ ║
497+
║ Error while importing ${connectorName} connector: ${e}
498+
║ ║
499+
║ 💡 SOLUTION ║
500+
║ Install the required package: ║
501+
║ ║
502+
║ npm install @adminforth/connector-${connectorName}
503+
║ # or ║
504+
║ pnpm add @adminforth/connector-${connectorName}
505+
║ ║
506+
╚════════════════════════════════════════════════════════════════════════════╝
495507
`);
496508
}
497509
}
498510

499511
async discoverDatabases() {
500512
this.statuses.dbDiscover = 'running';
501-
const SQLiteConnector = await this.tryToImportConnector('sqlite');
502-
const PostgresConnector = await this.tryToImportConnector('postgres');
503-
const MongoConnector = await this.tryToImportConnector('mongo');
504-
const ClickhouseConnector = await this.tryToImportConnector('clickhouse');
505-
const MysqlConnector = await this.tryToImportConnector('mysql');
513+
const dataSourcesDatabasesTypes = [];
514+
this.config.dataSources.forEach((ds) => {
515+
const dbType = ds.url.split(':')[0];
516+
dataSourcesDatabasesTypes.push(dbType)
517+
});
518+
const uniqueDbTypes = [...new Set(dataSourcesDatabasesTypes)];
519+
let SQLiteConnector, PostgresConnector, MongoConnector, ClickhouseConnector, MysqlConnector;
520+
if (uniqueDbTypes.includes('sqlite')) {
521+
SQLiteConnector = await this.tryToImportConnector('sqlite');
522+
}
523+
if (uniqueDbTypes.includes('postgres') || uniqueDbTypes.includes('postgresql')) {
524+
PostgresConnector = await this.tryToImportConnector('postgres');
525+
}
526+
if (uniqueDbTypes.includes('mongodb')) {
527+
MongoConnector = await this.tryToImportConnector('mongo');
528+
}
529+
if (uniqueDbTypes.includes('clickhouse')) {
530+
ClickhouseConnector = await this.tryToImportConnector('clickhouse');
531+
}
532+
if (uniqueDbTypes.includes('mysql')) {
533+
MysqlConnector = await this.tryToImportConnector('mysql');
534+
}
506535

507536
this.connectorClasses = {
508537
'sqlite': SQLiteConnector,

0 commit comments

Comments
 (0)