Typeorm: environment not working

Created on 4 Jul 2017  路  3Comments  路  Source: typeorm/typeorm

ormconfig.json:

[
    {
        "environment": "dev",
        "name": "org",
        "driver": {
            "type": "mysql",
            "host": "***",
            "port": 3306,
            "username": "root",
            "password": "***",
            "database": "db_org",
            "extra": {
                "charset": "utf8mb4_unicode_ci"
            }
        },
        "entities": [
            "build/model/admin.js"
        ],
        "logging": {
            "logQueries": true,
            "logSchemaCreation": true
        }
    },
    {
        "environment": "dev",
        "name": "youk",
        "driver": {
            "type": "mysql",
            "host": "***",
            "port": 3306,
            "username": "root",
            "password": "***",
            "database": "db_youk_test",
            "extra": {
                "charset": "utf8mb4_unicode_ci"
            }
        },
        "entities": [
            "build/model/shop.js",
            "build/model/user.js"
        ],
        "logging": {
            "logQueries": true,
            "logSchemaCreation": true
        }
    },
    {
        "environment": "prod",
        "name": "org",
        "driver": {
            "type": "mysql",
            "host": "***",
            "port": 3306,
            "username": "root",
            "password": "***",
            "database": "db_org",
            "extra": {
                "charset": "utf8mb4_unicode_ci"
            }
        },
        "entities": [
            "model/admin.js"
        ]
    },
    {
        "environment": "prod",
        "name": "youk",
        "driver": {
            "type": "mysql",
            "host": "***",
            "port": 3306,
            "username": "root",
            "password": "***",
            "database": "db_youk_test",
            "extra": {
                "charset": "utf8mb4_unicode_ci"
            }
        },
        "entities": [
            "model/shop.js",
            "model/user.js"
        ]
    }
]

process.env :

image

but getEntityManager('youk') get this:

screenshot from 2017-07-04 18-41-35

node version: 8.1.3
tsc version: 2.4.1
typeorm: 0.1.0-alpha.19

invalid

Most helpful comment

Then it would be great if you could remove it from the docs, because it's really misleading right now.

All 3 comments

/**
 * Normalize connection options.
 */
protected normalizeConnectionOptions(connectionOptions: ConnectionOptions | ConnectionOptions[]): ConnectionOptions[] {
    if (!(connectionOptions instanceof Array))
        connectionOptions = [connectionOptions];

    connectionOptions.forEach(options => {

        if (options.entities) {
            const entities = (options.entities as any[]).map(entity => {
                if (typeof entity === "string" || entity.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + entity;

                return entity;
            });
            Object.assign(connectionOptions, { entities: entities });
        }
        if (options.subscribers) {
            const subscribers = (options.subscribers as any[]).map(subscriber => {
                if (typeof subscriber === "string" || subscriber.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + subscriber;

                return subscriber;
            });
            Object.assign(connectionOptions, { subscribers: subscribers });
        }
        if (options.migrations) {
            const migrations = (options.migrations as any[]).map(migration => {
                if (typeof migration === "string" || migration.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + migration;

                return migration;
            });
            Object.assign(connectionOptions, { migrations: migrations });
        }
    });

    return connectionOptions;
}

to:

connectionOptions = connectionOptions.fliter((e)=>{return process.env.NODE_ENV === e.environment });

/**
 * Normalize connection options.
 */
protected normalizeConnectionOptions(connectionOptions: ConnectionOptions | ConnectionOptions[]): ConnectionOptions[] {
    if (!(connectionOptions instanceof Array))
        connectionOptions = [connectionOptions];

    connectionOptions = connectionOptions.fliter((e)=>{return process.env.NODE_ENV === e.environment });

    if(connectionOptions.length == 0) {/** do sth **/}

    connectionOptions.forEach(options => {

        if (options.entities) {
            const entities = (options.entities as any[]).map(entity => {
                if (typeof entity === "string" || entity.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + entity;

                return entity;
            });
            Object.assign(connectionOptions, { entities: entities });
        }
        if (options.subscribers) {
            const subscribers = (options.subscribers as any[]).map(subscriber => {
                if (typeof subscriber === "string" || subscriber.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + subscriber;

                return subscriber;
            });
            Object.assign(connectionOptions, { subscribers: subscribers });
        }
        if (options.migrations) {
            const migrations = (options.migrations as any[]).map(migration => {
                if (typeof migration === "string" || migration.substr(0, 1) !== "/")
                    return this.baseFilePath + "/" + migration;

                return migration;
            });
            Object.assign(connectionOptions, { migrations: migrations });
        }
    });

    return connectionOptions;
}

environment support was removed in latest versions. In practice no-one use both production and development configurations at the same time.

Then it would be great if you could remove it from the docs, because it's really misleading right now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

banduk picture banduk  路  3Comments

laukaichung picture laukaichung  路  3Comments

shroudedcode picture shroudedcode  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

Ionaru picture Ionaru  路  3Comments