Skip to main content
Module

x/typeorm/src/index.ts>Connection

Forked from https://github.com/typeorm/typeorm
Latest
class Connection
Re-export
import { Connection } from "https://deno.land/x/typeorm@v0.2.23-rc10/src/index.ts";

Connection is a single database ORM connection to a specific database. Its not required to be a database connection, depend on database type it can create connection pool. You can have multiple connections to multiple databases in your application.

Constructors

new
Connection(options: ConnectionOptions)

Properties

readonly
driver: Driver

Database driver used by this connection.

readonly
entityMetadatas: EntityMetadata[]

All entity metadatas that are registered for this connection.

readonly
isConnected: boolean

Indicates if connection is initialized or not.

readonly
logger: Logger

Logger used to log orm events.

readonly
manager: EntityManager

EntityManager of this connection.

readonly
migrations: MigrationInterface[]

Migration instances that are registered for this connection.

readonly
mongoManager: MongoEntityManager

Gets the mongodb entity manager that allows to perform mongodb-specific repository operations with any entity in this connection.

Available only in mongodb connections.

readonly
name: string

Connection name.

readonly
namingStrategy: NamingStrategyInterface

Naming strategy used in the connection.

readonly
options: ConnectionOptions

Connection options.

readonly
optional
queryResultCache: QueryResultCache

Used to work with query result cache.

readonly
relationIdLoader: RelationIdLoader

Used to load relation ids of specific entity relations.

readonly
relationLoader: RelationLoader

Used to load relations and work with lazy relations.

readonly
sqljsManager: SqljsEntityManager

Gets a sql.js specific Entity Manager that allows to perform special load and save operations

Available only in connection with the sqljs driver.

readonly
subscribers: EntitySubscriberInterface<any>[]

Entity subscriber instances that are registered for this connection.

Methods

protected
buildMetadatas(): Promise<void>

Builds metadatas for all registered classes inside this connection.

protected
findMetadata(target: Function | EntitySchema<any> | string): EntityMetadata | undefined

Finds exist entity metadata by the given entity class, target name or table name.

protected
getDatabaseName(): string
close(): Promise<void>

Closes connection with the database. Once connection is closed, you cannot use repositories or perform any operations except opening connection again.

connect(): Promise<this>

Performs connection to the database. This method should be called once on application bootstrap. This method not necessarily creates database connection (depend on database type), but it also can setup a connection pool with database to use.

createEntityManager(queryRunner?: QueryRunner): EntityManager

Creates an Entity Manager for the current connection with the help of the EntityManagerFactory.

createQueryBuilder<Entity>(
entityClass:
| ObjectType<Entity>
| EntitySchema<Entity>
| Function
| string
,
alias: string,
queryRunner?: QueryRunner,
): SelectQueryBuilder<Entity>

Creates a new query builder that can be used to build a sql query.

createQueryBuilder(queryRunner?: QueryRunner): SelectQueryBuilder<any>

Creates a new query builder that can be used to build a sql query.

createQueryRunner(mode?: "master" | "slave"): QueryRunner

Creates a query runner used for perform queries on a single database connection. Using query runners you can control your queries to execute using single database connection and manually control your database transaction.

Mode is used in replication mode and indicates whatever you want to connect to master database or any of slave databases. If you perform writes you must use master database, if you perform reads you can use slave databases.

dropDatabase(): Promise<void>

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.

getCustomRepository<T>(customRepository: ObjectType<T>): T

Gets custom entity repository marked with @EntityRepository decorator.

getManyToManyMetadata(entityTarget: Function | string, relationPropertyPath: string)

Gets entity metadata of the junction table (many-to-many table).

getMetadata(target: Function | EntitySchema<any> | string): EntityMetadata

Gets entity metadata for the given entity class or schema name.

getMongoRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): MongoRepository<Entity>

Gets mongodb-specific repository for the given entity class or name. Works only if connection is mongodb-specific.

getRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): Repository<Entity>

Gets repository for the given entity.

getTreeRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): TreeRepository<Entity>

Gets tree repository for the given entity class or name. Only tree-type entities can have a TreeRepository, like ones decorated with @Tree decorator.

hasMetadata(target: Function | EntitySchema<any> | string): boolean

Checks if entity metadata exist for the given entity class, target name or table name.

query(
query: string,
parameters?: any[],
queryRunner?: QueryRunner,
): Promise<any>

Executes raw SQL query and returns raw database results.

runMigrations(options?: { transaction?: "all" | "none" | "each"; }): Promise<Migration[]>

Runs all pending migrations. Can be used only after connection to the database is established.

showMigrations(): Promise<boolean>

Lists all migrations and whether they have been run. Returns true if there are pending migrations

synchronize(dropBeforeSync?: boolean): Promise<void>

Creates database schema for all entities registered in this connection. Can be used only after connection to the database is established.

transaction<T>(runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>

Wraps given function execution (and all operations made there) into a transaction. All database operations must be executed using provided entity manager.

transaction<T>(isolationLevel: IsolationLevel, runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>
undoLastMigration(options?: { transaction?: "all" | "none" | "each"; }): Promise<void>

Reverts last executed migration. Can be used only after connection to the database is established.