Connection APIs

Main API

  • createConnection() - Creates a new connection and registers it in global connection manager.

    If connection options parameter is omitted then connection options are read from ormconfig file or environment variables.

import {createConnection} from "typeorm";

const connection = await createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test"
});
  • createConnections() - Creates multiple connections and registers them in global connection manager.

    If connection options parameter is omitted then connection options are read from ormconfig file or environment variables.

import {createConnections} from "typeorm";

const connection = await createConnections([{
    name: "connection1",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test"
}, {
    name: "connection2",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test"
}]);
  • getConnectionManager() - Gets connection manager which stores all created (using createConnection() or createConnections()) connections.

  • getConnection() - Gets connection which was created by using createConnection method.

  • getEntityManager() - Gets EntityManager from connection.

    Connection name can be specified to indicate what connection's entity manager should be taken.

  • getRepository() - Gets Repository for given entity from connection.

    Connection name can be specified to indicate what connection's entity manager should be taken.

  • getTreeRepository() - Gets TreeRepository for given entity from connection.

    Connection name can be specified to indicate what connection's entity manager should be taken.

  • getMongoRepository() - Gets MongoRepository for given entity from connection.

    Connection name can be specified to indicate what connection's entity manager should be taken.

Connection API

  • name - Connection name. If you created nameless connection then it's equal to "default".

    You use this name when you work with multiple connections and call getConnection(connectionName: string)

  • options - Connection options used to create this connection.

    Learn more about Connection Options.

  • isConnected - Indicates if a real connection to the database is established.

  • driver - Underlying database driver used in this connection.

  • mongoManager - MongoEntityManager used to work with connection entities in mongodb connections.

    For more information about MongoEntityManager see MongoDB documentation.

  • connect - Performs connection to the database.

    When you use createConnection it automatically calls connect and you don't need to call it yourself.

  • close - Closes connection with the database.

    Usually, you call this method when your application is shutting down.

  • synchronize - Synchronizes database schema. When synchronize: true is set in connection options it calls this method.

    Usually, you call this method when your application is starting.

  • 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.

  • runMigrations - Runs all pending migrations.

  • undoLastMigration - Reverts last executed migration.

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

    Learn more about Entity Metadata.

  • 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.

    Learn more about Entity Metadata.

  • 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.

  • 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.

  • getMongoRepository - Gets MongoRepository of the given entity.

    This repository is used for entities in MongoDB connection.

    Learn more about MongoDB support.

  • transaction - Provides a single transaction where multiple database requests will be executed in a single database transaction.

    Learn more about Transactions.

  • query - Executes a raw SQL query.

  • createQueryBuilder - Creates a query builder, which can be used to build queries.

    Learn more about QueryBuilder.

  • createQueryRunner - Creates a query runner used to manage and work with a single real database connection.

    Learn more about QueryRunner.

ConnectionManager API

  • create - Creates a new connection and register it in the manager.

  • get - Gets already created connection stored in the manager by its name.

  • has - Checks if a connection is registered in the given connection manager.

Last updated

Was this helpful?