MikroOrm: Get list of schemas


How can i retrieve a list of all the schemas of a mikro-orm.config.js DB Connection?

I tried

const publicOrm = await MikroORM.init(MikroOrmOptions);
const result = await publicOrm
  .execute('select schema_name from information_schema.schemata;');

But ‘result’ always undefined

>Solution :

The execute method on SchemaGenerator does not return anything, as its meant for schema commands and not select queries. You can use execute method on the driver or even use a QB:

const orm = await MikroORM.init(...);
const driver = orm.em.getDriver();
const result = driver.execute('select schema_name from information_schema.schemata');

Or even better, you can use SchemaHelper.getNamespaces() method that will give you a list of existing schemas directly:

const orm = await MikroORM.init(...);
const platform = orm.em.getPlatform();
const result = await platform.getSchemaHelper()!.getNamespaces();

(this is what gets used in the schema generator database reflection, it will also automatically ignore system schemas prefixed with pg_ and crdb_, and information schema)

Leave a Reply Cancel reply