Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
  .getSchemaGenerator()
  .execute('select schema_name from information_schema.schemata;');

But ‘result’ always undefined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading