import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
import { createConnection, getConnectionOptions } from 'typeorm';

import { env } from '../env';

export const typeormLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {

    const loadedConnectionOptions = await getConnectionOptions();

    const connectionOptions = Object.assign(loadedConnectionOptions, {
        type: env.db.type as any, // See createConnection options for valid types
        host: env.db.host,
        port: env.db.port,
        username: env.db.username,
        password: env.db.password,
        database: env.db.database,
        synchronize: env.db.synchronize,
        logging: env.db.logging,
        entities: env.app.dirs.entities,
        migrations: env.app.dirs.migrations,
    });

    const connection = await createConnection(connectionOptions);

    if (settings) {
        settings.setData('connection', connection);
        settings.onShutdown(() => connection.close());
    }
};

import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
import { createConnection } from 'typeorm';

import { env } from '../env';

export const typeormLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {

    const connection = await createConnection({
        type: env.db.type as any, // See createConnection options for valid types
        host: env.db.host,
        port: env.db.port,
        username: env.db.username,
        password: env.db.password,
        database: env.db.database,
        synchronize: env.db.synchronize,
        logging: env.db.logging,
        entities: env.app.dirs.entities,
        migrations: env.app.dirs.migrations,
    });

    if (settings) {
        settings.setData('connection', connection);
        settings.onShutdown(() => connection.close());
    }
};

--- 
+++ 
@@ -1,11 +1,13 @@
 import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
-import { createConnection } from 'typeorm';
+import { createConnection, getConnectionOptions } from 'typeorm';
 
 import { env } from '../env';
 
 export const typeormLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {
 
-    const connection = await createConnection({
+    const loadedConnectionOptions = await getConnectionOptions();
+
+    const connectionOptions = Object.assign(loadedConnectionOptions, {
         type: env.db.type as any, // See createConnection options for valid types
         host: env.db.host,
         port: env.db.port,
@@ -18,6 +20,8 @@
         migrations: env.app.dirs.migrations,
     });
 
+    const connection = await createConnection(connectionOptions);
+
     if (settings) {
         settings.setData('connection', connection);
         settings.onShutdown(() => connection.close());
Load options by type orm (e.g. env vars, config files) and then overwrite with env.ts
