# DataSource API

* `options` - Options used to create this dataSource. Learn more about [Data Source Options](https://typeorm.io/docs/data-source/data-source-options.md).

```
const dataSourceOptions: DataSourceOptions = dataSource.options
```

* `isInitialized` - Indicates if the `DataSource` was initialized and the connection / connection pool with the database was established or not.

```
const isInitialized: boolean = dataSource.isInitialized
```

* `driver` - Underlying database driver used in this dataSource.

```
const driver: Driver = dataSource.driver
```

* `manager` - `EntityManager` used to work with entities. Learn more about [Entity Manager](https://typeorm.io/docs/working-with-entity-manager/working-with-entity-manager.md) and [Repository](https://typeorm.io/docs/working-with-entity-manager/working-with-repository.md).

```
const manager: EntityManager = dataSource.manager

// you can call manager methods, for example find:

const users = await manager.find()
```

* `mongoManager` - `MongoEntityManager` used to work with entities for mongodb data source. For more information about MongoEntityManager see [MongoDB](https://typeorm.io/docs/drivers/mongodb.md) documentation.

```
const manager: MongoEntityManager = dataSource.mongoManager

// you can call manager or mongodb-manager specific methods, for example find:

const users = await manager.find()
```

* `initialize` - Initializes data source and opens connection pool to the database.

```
await dataSource.initialize()
```

* `destroy` - Destroys the DataSource and closes all database connections. Usually, you call this method when your application is shutting down.

```
await dataSource.destroy()
```

* `synchronize` - Synchronizes database schema. When `synchronize: true` is set in data source options it calls this method. Usually, you call this method when your application is starting.

```
await dataSource.synchronize()
```

* `dropDatabase` - Drops the database and all its data. Be careful with this method on production since this method will erase all your database tables and their data. Can be used only after connection to the database is established.

```
await dataSource.dropDatabase()
```

* `runMigrations` - Runs all pending migrations.

```
await dataSource.runMigrations()
```

* `undoLastMigration` - Reverts last executed migration.

```
await dataSource.undoLastMigration()
```

* `hasMetadata` - Checks if metadata for a given entity is registered.

```
if (dataSource.hasMetadata(User))

    const userMetadata = dataSource.getMetadata(User)
```

* `getMetadata` - Gets `EntityMetadata` of the given entity. You can also specify a table name and if entity metadata with such table name is found it will be returned.

```
const userMetadata = dataSource.getMetadata(User)

// now you can get any information about User entity
```

* `getRepository` - Gets `Repository` of the given entity. You can also specify a table name and if repository for given table is found it will be returned. Learn more about [Repositories](https://typeorm.io/docs/working-with-entity-manager/working-with-repository.md).

```
const repository = dataSource.getRepository(User)

// now you can call repository methods, for example find:

const users = await repository.find()
```

* `getTreeRepository` - Gets `TreeRepository` of the given entity. You can also specify a table name and if repository for given table is found it will be returned. Learn more about [Repositories](https://typeorm.io/docs/working-with-entity-manager/working-with-repository.md).

```
const repository = dataSource.getTreeRepository(Category)

// now you can call tree repository methods, for example findTrees:

const categories = await repository.findTrees()
```

* `getMongoRepository` - Gets `MongoRepository` of the given entity. This repository is used for entities in MongoDB dataSource. Learn more about [MongoDB support](https://typeorm.io/docs/drivers/mongodb.md).

```
const repository = dataSource.getMongoRepository(User)

// now you can call mongodb-specific repository methods, for example createEntityCursor:

const categoryCursor = repository.createEntityCursor()

const category1 = await categoryCursor.next()

const category2 = await categoryCursor.next()
```

* `transaction` - Provides a single transaction where multiple database requests will be executed in a single database transaction. Learn more about [Transactions](https://typeorm.io/docs/transactions.md).

```
await dataSource.transaction(async (manager) => {

    // NOTE: you must perform all database operations using given manager instance

    // its a special instance of EntityManager working with this transaction

    // and don't forget to await things here

})
```

* `query` - Executes a raw SQL query.

```
const rawData = await dataSource.query(`SELECT * FROM USERS`)



// You can also use parameters to avoid SQL injection

// The syntax differs between the drivers



// aurora-mysql, better-sqlite3, capacitor, cordova,

// expo, mariadb, mysql, nativescript, react-native,

// sap, sqlite, sqljs

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = ? and age = ?",

    ["John", 24],

)



// mysql2 additionally supports named parameters

// when extra.namedPlaceholders is true

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = :name and age = :age",

    { name: "John", age: 24 },

)



// aurora-postgres, cockroachdb, postgres

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = $1 and age = $2",

    ["John", 24],

)



// oracle

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = :1 and age = :2",

    ["John", 24],

)



// spanner

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = @param0 and age = @param1",

    ["John", 24],

)



// mssql

const rawData = await dataSource.query(

    "SELECT * FROM USERS WHERE name = @0 and age = @1",

    ["John", 24],

)
```

* `sql` - Executes a raw SQL query using template literals.

```
const rawData =

    await dataSource.sql`SELECT * FROM USERS WHERE name = ${"John"} and age = ${24}`
```

Learn more about using the [SQL Tag syntax](https://typeorm.io/docs/guides/sql-tag.md).

* `createQueryBuilder` - Creates a query builder, which can be used to build queries. Learn more about [QueryBuilder](https://typeorm.io/docs/query-builder/select-query-builder.md).

```
const users = await dataSource

    .createQueryBuilder()

    .select()

    .from(User, "user")

    .where("user.name = :name", { name: "John" })

    .getMany()
```

* `createQueryRunner` - Creates a query runner used to manage and work with a single real database dataSource. Learn more about [QueryRunner](https://typeorm.io/docs/query-runner.md).

```
const queryRunner = dataSource.createQueryRunner()



// you can use its methods only after you call connect

// which performs real database connection

await queryRunner.connect()



// .. now you can work with query runner and call its methods



// very important - don't forget to release query runner once you finished working with it

await queryRunner.release()
```
