Using Configuration Sources

Creating a new connection from the configuration file

Most of the times you want to store your connection options in a separate configuration file.It makes it convenient and easy to manage.TypeORM supports multiple configuration sources.You only need to create a ormconfig.[format] file in the root directory of your application (near package.json),put your configuration there and in your app call createConnection() without any configuration passed:

  1. import {createConnection} from "typeorm";
  2. // createConnection method will automatically read connection options
  3. // from your ormconfig file or environment variables
  4. const connection = await createConnection();

Supported ormconfig file formats are: .json, .js, .env, .yml and .xml.

Using ormconfig.json

Create ormconfig.json in the project root (near package.json). It should have the following content:

  1. {
  2. "type": "mysql",
  3. "host": "localhost",
  4. "port": 3306,
  5. "username": "test",
  6. "password": "test",
  7. "database": "test"
  8. }

You can specify any other options from ConnectionOptions.

If you want to create multiple connections then simply create multiple connections in a single array:

  1. [{
  2. "name": "default",
  3. "type": "mysql",
  4. "host": "localhost",
  5. "port": 3306,
  6. "username": "test",
  7. "password": "test",
  8. "database": "test"
  9. }, {
  10. "name": "second-connection",
  11. "type": "mysql",
  12. "host": "localhost",
  13. "port": 3306,
  14. "username": "test",
  15. "password": "test",
  16. "database": "test"
  17. }]

Using ormconfig.js

Create ormconfig.js in the project root (near package.json). It should have following content:

  1. module.exports = {
  2. "type": "mysql",
  3. "host": "localhost",
  4. "port": 3306,
  5. "username": "test",
  6. "password": "test",
  7. "database": "test"
  8. }

You can specify any other options from ConnectionOptions.If you want to create multiple connections then simply create multiple connections in a single array and return it.

Using environment variables

Create .env or ormconfig.env in the project root (near package.json). It should have the following content:

  1. TYPEORM_CONNECTION = mysql
  2. TYPEORM_HOST = localhost
  3. TYPEORM_USERNAME = root
  4. TYPEORM_PASSWORD = admin
  5. TYPEORM_DATABASE = test
  6. TYPEORM_PORT = 3000
  7. TYPEORM_SYNCHRONIZE = true
  8. TYPEORM_LOGGING = true
  9. TYPEORM_ENTITIES = entity/*.js,modules/**/entity/*.js

List of available env variables you can set:

  • TYPEORM_CONNECTION
  • TYPEORM_HOST
  • TYPEORM_USERNAME
  • TYPEORM_PASSWORD
  • TYPEORM_DATABASE
  • TYPEORM_PORT
  • TYPEORM_URL
  • TYPEORM_SID
  • TYPEORM_SCHEMA
  • TYPEORM_SYNCHRONIZE
  • TYPEORM_DROP_SCHEMA
  • TYPEORM_MIGRATIONS_RUN
  • TYPEORM_ENTITIES
  • TYPEORM_MIGRATIONS
  • TYPEORM_MIGRATIONS_TABLE_NAME
  • TYPEORM_SUBSCRIBERS
  • TYPEORM_ENTITY_SCHEMAS
  • TYPEORM_LOGGING
  • TYPEORM_LOGGER
  • TYPEORM_ENTITY_PREFIX
  • TYPEORM_MAX_QUERY_EXECUTION_TIME
  • TYPEORM_ENTITIES_DIR
  • TYPEORM_MIGRATIONS_DIR
  • TYPEORM_SUBSCRIBERS_DIR
  • TYPEORM_DRIVER_EXTRA
  • TYPEORM_DEBUG
  • TYPEORM_CACHE
  • TYPEORM_CACHE_OPTIONS
  • TYPEORM_CACHE_ALWAYS_ENABLED
  • TYPEORM_CACHE_DURATION

TYPEORM_CACHE should be boolean or string of cache type

ormconfig.env should be used only during development.On production you can set all these values in real ENVIRONMENT VARIABLES.

You cannot define multiple connections using an env file or environment variables.If your app has multiple connections then use alternative configuration storage format.

If you need to pass a driver-specific option, e.g. charset for MySQL, you could use the TYPEORM_DRIVER_EXTRA variable in JSON format, e.g.

  1. TYPEORM_DRIVER_EXTRA='{"charset": "utf8mb4"}'

Using ormconfig.yml

Create ormconfig.yml in the project root (near package.json). It should have the following content:

  1. default: # default connection
  2. host: "localhost"
  3. port: 3306
  4. username: "test"
  5. password: "test"
  6. database: "test"
  7. second-connection: # other connection
  8. host: "localhost"
  9. port: 3306
  10. username: "test"
  11. password: "test"
  12. database: "test2"

You can use any connection options available.

Using ormconfig.xml

Create ormconfig.xml in the project root (near package.json). It should have the following content:

  1. <connections>
  2. <connection type="mysql" name="default">
  3. <host>localhost</host>
  4. <username>root</username>
  5. <password>admin</password>
  6. <database>test</database>
  7. <port>3000</port>
  8. <logging>true</logging>
  9. </connection>
  10. <connection type="mysql" name="second-connection">
  11. <host>localhost</host>
  12. <username>root</username>
  13. <password>admin</password>
  14. <database>test2</database>
  15. <port>3000</port>
  16. <logging>true</logging>
  17. </connection>
  18. </connections>

You can use any connection options available.

Which configuration file is used by Typeorm

Sometimes, you may want to use multiple configurations using different formats. When calling getConnectionOptions()or attempting to use createConnection() without the connection options, Typeorm will attempt to load the configurations,in this order:

  1. From the environment variables. Typeorm will attempt to load the .env file using dotEnv if it exists. If the environmentvariables TYPEORM_CONNECTION or TYPEORM_URL are set, Typeorm will use this method.
  2. From the ormconfig.env.
  3. From the other ormconfig.[format] files, in this order: [js, ts, json, yml, yaml, xml].

Note that Typeorm will use the first valid method found and will not load the others. For example, Typeorm will not load theormconfig.[format] files if the configuration was found in the environment.

Overriding options defined in ormconfig

Sometimes you want to override values defined in your ormconfig file,or you might to append some TypeScript / JavaScript logic to your configuration.

In such cases you can load options from ormconfig and get ConnectionOptions built,then you can do whatever you want with those options, before passing them to createConnection function:

  1. // read connection options from ormconfig file (or ENV variables)
  2. const connectionOptions = await getConnectionOptions();
  3. // do something with connectionOptions,
  4. // for example append a custom naming strategy or a custom logger
  5. Object.assign(connectionOptions, { namingStrategy: new MyNamingStrategy() });
  6. // create a connection using modified connection options
  7. const connection = await createConnection(connectionOptions);