# Data Source Options

## What is DataSourceOptions?

`DataSourceOptions` is a data source configuration you pass when you create a new `DataSource` instance. Different RDBMS-es have their own specific options.

## Common data source options

* `type` - RDBMS type. You must specify what database engine you use. Possible values are: "mysql", "postgres", "cockroachdb", "sap", "spanner", "mariadb", "cordova", "react-native", "nativescript", "sqljs", "oracle", "mssql", "mongodb", "aurora-mysql", "aurora-postgres", "expo", "better-sqlite3", "capacitor". This option is **required**.
* `extra` - Extra options to be passed to the underlying driver. Use it if you want to pass extra settings to the underlying database driver.
* `entities` - Entities, or Entity Schemas, to be loaded and used for this data source. It accepts entity classes, entity schema classes, and directory paths from which to load. Directories support glob patterns. Example: `entities: [Post, Category, "entities/*.js", "modules/**/entities/*.js"]`. Learn more about [Entities](https://orkhan.gitbook.io/typeorm/docs/docs/entity/1-entities). Learn more about [Entity Schemas](https://orkhan.gitbook.io/typeorm/docs/docs/entity/6-separating-entity-definition).
* `subscribers` - Subscribers to be loaded and used for this data source. It accepts both entity classes and directories from which to load. Directories support glob patterns. Example: `subscribers: [PostSubscriber, AppSubscriber, "subscribers/*.js", "modules/**/subscribers/*.js"]`. Learn more about [Subscribers](https://orkhan.gitbook.io/typeorm/docs/docs/listeners-and-subscribers).
* `logging` - Indicates if logging is enabled or not. If set to `true` then query and error logging will be enabled. You can also specify different types of logging to be enabled, for example `["query", "error", "schema"]`. Learn more about [Logging](https://orkhan.gitbook.io/typeorm/docs/docs/logging).
* `logger` - Logger to be used for logging purposes. Possible values are "advanced-console", "formatted-console", "simple-console" and "file". Default is "advanced-console". You can also specify a logger class that implements `Logger` interface. Learn more about [Logging](https://orkhan.gitbook.io/typeorm/docs/docs/logging).
* `maxQueryExecutionTime` - If query execution time exceed this given max execution time (in milliseconds) then logger will log this query.
* `poolSize` - Configure maximum number of active connections is the pool.
* `namingStrategy` - Naming strategy to be used to name tables and columns in the database.
* `entityPrefix` - Prefixes with the given string all tables (or collections) on this data source.
* `entitySkipConstructor` - Indicates if TypeORM should skip constructors when deserializing entities from the database. Note that when you do not call the constructor both private properties and default properties will not operate as expected.
* `dropSchema` - Drops the schema each time data source is being initialized. Be careful with this option and don't use this in production - otherwise you'll lose all production data. This option is useful during debug and development.
* `synchronize` - Indicates if database schema should be auto created on every application launch. Be careful with this option and don't use this in production - otherwise you can lose production data. This option is useful during debug and development. As an alternative to it, you can use CLI and run schema:sync command. Note that for MongoDB database it does not create schema, because MongoDB is schemaless. Instead, it syncs just by creating indexes.
* `migrations` - [Migrations](https://orkhan.gitbook.io/typeorm/docs/docs/migrations/01-why) to be loaded and used for this data source
* `migrationsRun` - Indicates if [migrations](https://orkhan.gitbook.io/typeorm/docs/docs/migrations/01-why) should be auto-run on every application launch.
* `migrationsTableName` - Name of the table in the database which is going to contain information about executed [migrations](https://orkhan.gitbook.io/typeorm/docs/docs/migrations/01-why).
* `migrationsTransactionMode` - Controls transaction mode when running [migrations](https://orkhan.gitbook.io/typeorm/docs/docs/migrations/01-why).
* `metadataTableName` - Name of the table in the database which is going to contain information about table metadata. By default, this table is called "typeorm\_metadata".
* `cache` - Enables entity result caching. You can also configure cache type and other cache options here. Read more about caching [here](https://orkhan.gitbook.io/typeorm/docs/docs/query-builder/6-caching).
* `isolateWhereStatements` - Enables where statement isolation, wrapping each where clause in brackets automatically. eg. `.where("user.firstName = :search OR user.lastName = :search")` becomes `WHERE (user.firstName = ? OR user.lastName = ?)` instead of `WHERE user.firstName = ? OR user.lastName = ?`
* `invalidWhereValuesBehavior` - Controls how null and undefined values are handled in where conditions for high-level operations (find operations, repository methods, EntityManager methods). Does not affect QueryBuilder's `.where()` directly.

  * `null` behavior options:
    * `'ignore'` - skips null properties
    * `'sql-null'` - transforms null to SQL NULL
    * `'throw'` (default) - throws an error
  * `undefined` behavior options:
    * `'ignore'` - skips undefined properties
    * `'throw'` (default) - throws an error

  Example: `invalidWhereValuesBehavior: { null: 'sql-null', undefined: 'ignore' }`.

  Learn more about [Null and Undefined Handling](https://orkhan.gitbook.io/typeorm/docs/docs/data-source/5-null-and-undefined-handling).

## Data Source Options example

Here is a small example of data source options for mysql:

```typescript
{
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test",
    logging: true,
    synchronize: true,
    entities: [__dirname + "/entities/**/*{.js,.ts}"],
    subscribers: [__dirname + "/subscribers/**/*{.js,.ts}"],
    entitySchemas: [__dirname + "/schemas/**/*.json"],
    migrations: [__dirname + "/migrations/**/*{.js,.ts}"]
}
```
