This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MySQL and PostgreSQL (when PostGIS is available) both support spatial indices.
To create a spatial index on a column in MySQL, add an `Index` with `spatial:
true` on a column that uses a spatial type (`geometry`, `point`, `linestring`,
To create a spatial index on a column in MySQL, add an `Index` with `spatial: true` on a column that uses a spatial type (`geometry`, `point`, `linestring`,
@@ -120,26 +114,26 @@ true` on a column that uses a spatial type (`geometry`, `point`, `linestring`,
exportclassThing {
@Column("point")
@Index({ spatial: true })
point:string;
point:string
}
```
To create a spatial index on a column in PostgreSQL, add an `Index` with `spatial: true` on a column that uses a spatial type (`geometry`, `geography`):
```typescript
exportinterfaceGeometry {
type:"Point";
coordinates: [Number, Number];
type:"Point"
coordinates: [Number, Number]
}
@Entity()
exportclassThing {
@Column("geometry", {
spatialFeatureType: "Point",
srid: 4326
spatialFeatureType: "Point",
srid: 4326,
})
@Index({ spatial: true })
point:Geometry;
point:Geometry
}
```
Expand All
@@ -162,15 +156,10 @@ after that, you should disable synchronization for this index to avoid deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Make sure your `subscribers` property is set in your [Connection Options](./connection-options.md#common-connection-options) so TypeORM loads your subscriber.
Make sure your `subscribers` property is set in your [DataSourceOptions](./data-source-options.md#common-data-source-options) so TypeORM loads your subscriber.
### `Event Object`
Excluding `listenTo`, all `EntitySubscriberInterface` methods are passed an event object that has the following base properties:
-`connection: Connection` - Connection used in the event.
-`dataSource: DataSource` - DataSource used in the event.
-`queryRunner: QueryRunner` - QueryRunner used in the event transaction.
-`manager: EntityManager` - EntityManager used in the event transaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can enable logging of all queries and errors by simply setting `logging: true` in your connection options:
You can enable logging of all queries and errors by simply setting `logging: true` in data source options:
```typescript
{
Expand All
@@ -26,10 +26,10 @@ You can enable logging of all queries and errors by simply setting `logging: tru
## Logging options
You can enable different types of logging in connection options:
You can enable different types of logging in data source options:
```typescript
{
{
host: "localhost",
...
logging: ["query", "error"]
Expand All
@@ -48,14 +48,14 @@ If you want to enable logging of failed queries only then only add `error`:
There are other options you can use:
*`query` - logs all queries.
*`error` - logs all failed queries and errors.
*`schema` - logs the schema build process.
*`warn` - logs internal orm warnings.
*`info` - logs internal orm informative messages.
*`log` - logs internal orm log messages.
-`query` - logs all queries.
-`error` - logs all failed queries and errors.
-`schema` - logs the schema build process.
-`warn` - logs internal orm warnings.
-`info` - logs internal orm informative messages.
-`log` - logs internal orm log messages.
You can specify as many options as needed.
You can specify as many options as needed.
If you want to enable all logging you can simply specify `logging: "all"`:
```typescript
Expand All
@@ -69,7 +69,7 @@ If you want to enable all logging you can simply specify `logging: "all"`:
## Log long-running queries
If you have performance issues, you can log queries that take too much time to execute
by setting `maxQueryExecutionTime` in connection options:
by setting `maxQueryExecutionTime` in data source options:
```typescript
{
Expand All
@@ -85,14 +85,14 @@ This code will log all queries which run more then `1 second`.
TypeORM ships with 4 different types of logger:
*`advanced-console` - this is the default logger which logs all messages into the console using color
and sql syntax highlighting (using [chalk](https://github.com/chalk/chalk)).
*`simple-console` - this is a simple console logger which is exactly the same as the advanced logger, but it does not use any color highlighting.
This logger can be used if you have problems / or don't like colorized logs.
*`file` - this logger writes all logs into `ormlogs.log` in the root folder of your project (near `package.json` and `ormconfig.json`).
*`debug` - this logger uses [debug package](https://github.com/visionmedia/debug), to turn on logging set your env variable `DEBUG=typeorm:*` (note logging option has no effect on this logger).
-`advanced-console` - this is the default logger which logs all messages into the console using color
and sql syntax highlighting (using [chalk](https://github.com/chalk/chalk)).
-`simple-console` - this is a simple console logger which is exactly the same as the advanced logger, but it does not use any color highlighting.
This logger can be used if you have problems / or don't like colorized logs.
-`file` - this logger writes all logs into `ormlogs.log` in the root folder of your project (near `package.json`).
-`debug` - this logger uses [debug package](https://github.com/visionmedia/debug), to turn on logging set your env variable `DEBUG=typeorm:*` (note logging option has no effect on this logger).
You can enable any of them in connection options:
You can enable any of them in data source options:
```typescript
{
Expand All
@@ -108,48 +108,29 @@ You can enable any of them in connection options:
You can create your own logger class by implementing the `Logger` interface:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this example we did not call save or softRemove for category1 and category2, but they will be automatically saved and soft-deleted when the cascade of relation options is set to true like this:
With eager loading enabled on a relation, you don't have to specify relations in the find command as it will ALWAYS be loaded automatically. If you use QueryBuilder eager relations are disabled, you have to use `leftJoinAndSelect` to load the relation.
Expand All
@@ -179,40 +195,46 @@ Bi-directional relations are relations with decorators on both sides of a relati
We just created a uni-directional relation. Let's make it bi-directional:
@@ -235,29 +257,29 @@ In case you need to have additional properties in your many-to-many relationship
For example, if you would like entities `Post` and `Category` to have a many-to-many relationship with an additional `order` column, then you need to create an entity `PostToCategory` with two `ManyToOne` relations pointing in both directions and with custom columns in it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With eager loading enabled on a relation, you don't have to specify relations in the find command as it will ALWAYS be loaded automatically. If you use QueryBuilder eager relations are disabled, you have to use `leftJoinAndSelect` to load the relation.
Expand All
@@ -110,56 +115,58 @@ Bi-directional are relations with decorators on both sides of a relation.
We just created a uni-directional relation. Let's make it bi-directional:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*[Creating a queryRunner](#creating-a-new-queryrunner)
*[Using queryRunner](#using-queryrunner)
*[Working with `QueryRunner`](#working-with-queryrunner)
## What is `QueryRunner`
-[What is `QueryRunner`](#what-is-queryrunner)
-[Creating a new `QueryRunner` instance](#creating-a-new-queryrunner-instance)
-[Using `QueryRunner`](#using-queryrunner)
Your interaction with the database is only possible once you setup a connection.
TypeORM's `Connection` does not setup a database connection as it might seem, instead it sets up a connection pool.
If you are interested in a real database connection, you should use `QueryRunner`.
Each instance of `QueryRunner` is a separate isolated database connection. Using query runners you can control your queries to execute using single database connection and manually control your database transaction.
## What is `QueryRunner`
## Creating a new queryRunner
Each new `QueryRunner` instance takes a single connection from connection pool, if RDBMS supports connection pooling.
For databases not supporting connection pools, it uses the same connection across data source.
To create a new instance of `QueryRunner`you should first create a connection pool, in any of the ways described on the `Connection` documentation. Once a connection has established, use the `createQueryRunner` function to create an isolated connection.
## Creating a new `QueryRunner`instance
`createQueryRunner` Creates a query runner used to perform queries on a single database connection.
Use `createQueryRunner` method to create a new `QueryRunner`:
Since the `QueryRunner` is used to manage an isolated database connection, make sure to release it when it is not needed anymore to make it available to the connection pool again. After connection is released it is not possible to use the query runner methods.
## Working with `QueryRunner`
Once you set your queryRunner up, you can use it with an interface similar to the `Connection` interface:
**Important**: make sure to release it when it is not needed anymore to make it available to the connection pool again:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Relations helps you to work with related entities easily.
There are several types of relations:
*[one-to-one](./one-to-one-relations.md) using `@OneToOne`
*[many-to-one](./many-to-one-one-to-many-relations.md) using `@ManyToOne`
*[one-to-many](./many-to-one-one-to-many-relations.md) using `@OneToMany`
*[many-to-many](./many-to-many-relations.md) using `@ManyToMany`
-[one-to-one](./one-to-one-relations.md) using `@OneToOne`
-[many-to-one](./many-to-one-one-to-many-relations.md) using `@ManyToOne`
-[one-to-many](./many-to-one-one-to-many-relations.md) using `@OneToMany`
-[many-to-many](./many-to-many-relations.md) using `@ManyToMany`
## Relation options
There are several options you can specify for relations:
*`eager: boolean` - If set to true, the relation will always be loaded with the main entity when using `find*` methods or `QueryBuilder` on this entity
*`cascade: boolean | ("insert" | "update")[]` - If set to true, the related object will be inserted and updated in the database. You can also specify an array of [cascade options](#cascade-options).
*`onDelete: "RESTRICT"|"CASCADE"|"SET NULL"` - specifies how foreign key should behave when referenced object is deleted
*`primary: boolean` - Indicates whether this relation's column will be a primary column or not.
*`nullable: boolean` - Indicates whether this relation's column is nullable or not. By default it is nullable.
*`orphanedRowAction: "nullify" | "delete" | "soft-delete"` - When a child row is removed from its parent, determines if the child row should be orphaned (default) or deleted (delete or soft delete).
-`eager: boolean` - If set to true, the relation will always be loaded with the main entity when using `find*` methods or `QueryBuilder` on this entity
-`cascade: boolean | ("insert" | "update")[]` - If set to true, the related object will be inserted and updated in the database. You can also specify an array of [cascade options](#cascade-options).
-`onDelete: "RESTRICT"|"CASCADE"|"SET NULL"` - specifies how foreign key should behave when referenced object is deleted
-`nullable: boolean` - Indicates whether this relation's column is nullable or not. By default it is nullable.
-`orphanedRowAction: "nullify" | "delete" | "soft-delete"` - When a child row is removed from its parent, determines if the child row should be orphaned (default) or deleted (delete or soft delete).