mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
refactored connection and added docs about connection
This commit is contained in:
parent
99592c02e9
commit
77cc40bbe5
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,4 +5,5 @@ coverage/
|
||||
node_modules/
|
||||
ormconfig.json
|
||||
ormlogs.log
|
||||
npm-debug.log
|
||||
npm-debug.log
|
||||
/src/connection/BaseConnectionOptions.ts
|
||||
|
||||
11
CHANGELOG.md
11
CHANGELOG.md
@ -64,10 +64,15 @@ Also now all
|
||||
Column type must be passed in options object, e.g. `@PrimaryGeneratedColumn({ type: "bigint"})`
|
||||
* Logger interface has changed. Custom logger supply mechanism has changed
|
||||
* Now `logging` options in connection options is simple "true", or "all", or list of logging modes can be supplied
|
||||
|
||||
### DEPRECATIONS
|
||||
|
||||
* removed `driver` section in connection options. Define options right in the connection options section.
|
||||
* `Embedded` decorator is deprecated now. use `@Column(type => SomeEmbedded)` instead
|
||||
* `tablesPrefix` in connection options is deprecated. Use `entityPrefix` instead
|
||||
* `autoMigrationsRun` in connection options is deprecated. Use `migrationsRun` instead
|
||||
* `autoSchemaSync` in connection options is deprecated. Use `synchronize` instead
|
||||
* `dropSchemaOnConnection` in connection options is deprecated. Use `dropSchema` instead
|
||||
* `schemaName` in connection options is deprecated. Use `schema` instead
|
||||
* `TYPEORM_AUTO_SCHEMA_SYNC` env variable is now called `TYPEORM_SYNCHRONIZE`
|
||||
* `schemaSync` method in `Connection` has been renamed to `synchronize`
|
||||
|
||||
### NEW FEATURES
|
||||
|
||||
|
||||
@ -1,256 +0,0 @@
|
||||
# Connection and connection options
|
||||
|
||||
* What is `Connection` in TypeORM
|
||||
* Creating a new connection
|
||||
* Creating a new connection using main api
|
||||
* Creating connection using `ConnectionManager`
|
||||
* Connection options
|
||||
* Common connection options
|
||||
* Specific options for `mysql` / `mariadb`
|
||||
* Specific options for `postgres`
|
||||
* Specific options for `sqlite` / `websql`
|
||||
* Specific options for `mssql`
|
||||
* Specific options for `mongodb`
|
||||
* Creating a new connection from the configuration files
|
||||
* Loading from `ormconfig.json`
|
||||
* Loading from `ormconfig.js` or from environment variables
|
||||
* Loading from `ormconfig.env`
|
||||
* Loading from `ormconfig.yml`
|
||||
* Loading from `ormconfig.xml`
|
||||
* Working with connection
|
||||
* Connection usage example in sample express application
|
||||
* Using service container and typedi extensions
|
||||
* API
|
||||
* Main API
|
||||
* `Connection` class API
|
||||
* `ConnectionManager` class API
|
||||
|
||||
## What is `Connection` in TypeORM
|
||||
|
||||
Connection is a TypeORM class which setups a real connection with your database.
|
||||
Depend on database type it may also setup a connection pool.
|
||||
Connection (or connection pool) setup is made once its `connection` method is called.
|
||||
Disconnection (or closing all connections in the pool) is made once its `disconnect` method is called.
|
||||
You must create TypeORM `Connection` only once in your application bootstrap.
|
||||
|
||||
## Creating a new connection
|
||||
|
||||
### Creating a new connection using main api
|
||||
|
||||
There are several ways how connection can be created.
|
||||
The most simple and common way is to use main api `createConnection` and `createConnections` methods.
|
||||
|
||||
`createConnection` creates a single connection:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
const connection: Connection = await createConnection({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
```
|
||||
|
||||
`createConnections` creates multiple connection:
|
||||
|
||||
```typescript
|
||||
import {createConnections, Connection} from "typeorm";
|
||||
|
||||
const connections: Connection[] = await createConnections([{
|
||||
name: "default",
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
}, {
|
||||
name: "test2-connection",
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test2",
|
||||
}]);
|
||||
```
|
||||
|
||||
Both these methods automatically call `Connection#connect` method.
|
||||
Both these methods automatically load configuration from `ormconfig` file if connection options are not specified.
|
||||
For example:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
// here createConnection will load connection options from
|
||||
// ormconfig.json / ormconfig.js / ormconfig.yml / ormconfig.env / ormconfig.xml
|
||||
// files, or from special environment variables
|
||||
// if ormconfig contains multiple connections then it will load connection named "default"
|
||||
// or connection without name at all (which means it will be named "default" by default)
|
||||
const connection: Connection = await createConnection();
|
||||
|
||||
// if createConnections is called instead of createConnection then it will initialize and return all connections
|
||||
// defined in ormconfig file
|
||||
```
|
||||
|
||||
Different connections must have different connection names.
|
||||
Different connections cannot have same connection name.
|
||||
By default, if connection name is not specified it is equal to `default`.
|
||||
Usually use multiple connections when you have multiple databases or multiple connection configurations.
|
||||
|
||||
Once you create a connection you can obtain it anywhere, using `getConnection` method:
|
||||
|
||||
```typescript
|
||||
import {getConnection} from "typeorm";
|
||||
|
||||
// can be used once createConnection is called and is resolved
|
||||
const connection = getConnection();
|
||||
|
||||
// if you have multiple connections you can get connection by name:
|
||||
const secondConnection = getConnection("test2-connection");
|
||||
```
|
||||
|
||||
Avoid creating extra classes / services which store instance of your connections.
|
||||
This functionality is already embed into TypeORM -
|
||||
you don't need to overengineer and create useless abstractions.
|
||||
|
||||
### Creating connection using `ConnectionManager`
|
||||
|
||||
You can create connection using `ConnectionManager` class. For example:
|
||||
|
||||
```typescript
|
||||
import {getConnectionManager, ConnectionManager, Connection} from "typeorm";
|
||||
|
||||
const connectionManager: ConnectionManager = getConnectionManager(); // or you can initialize your own connection manager like this: new ConnectionManager()
|
||||
const connection: Connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
await connection.connect(); // performs connection
|
||||
```
|
||||
|
||||
This is not general way of creating connection, but it may be useful for some users.
|
||||
For example users who want to create connection and store its instance,
|
||||
but have a control when actual "connection" will be established.
|
||||
Also you can create and maintain your own `ConnectionManager`:
|
||||
|
||||
```typescript
|
||||
import {getConnectionManager, ConnectionManager, Connection} from "typeorm";
|
||||
|
||||
const connectionManager = new ConnectionManager(); // or you can initialize your own connection manager like this: new ConnectionManager()
|
||||
const connection: Connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
await connection.connect(); // performs connection
|
||||
```
|
||||
|
||||
But note, this way you won't be able to use `getConnection()` method anymore -
|
||||
you'll need to store your connection manager instance and use `connectionManager.get` method to get a connection you need.
|
||||
|
||||
Generally avoid this method and avoid unnecessary complications in your application,
|
||||
use `ConnectionManager` only if you really think you need it.
|
||||
|
||||
## Creating a new connection from the configuration files
|
||||
|
||||
Most of the times you want to store your connection options in a separate configuration file.
|
||||
It makes it convenient and easy to manage.
|
||||
TypeORM supports multiple configuration sources of this purpose.
|
||||
You'll only need to create `ormconfig.[format]` file in root directory of your application (near `package.json`),
|
||||
write configuration there and in your app simply call `createConnection()` without any configuration passed:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
// createConnection method will automatically read connection options from your ormconfig file
|
||||
const connection: Connection = await createConnection();
|
||||
```
|
||||
|
||||
### Loading from `ormconfig.json`
|
||||
|
||||
Create `ormconfig.json` file in root of your project. It should have following content:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}
|
||||
```
|
||||
|
||||
You can specify any other options from `ConnectionOptions`.
|
||||
|
||||
If you want to create multiple connections then simply create multiple connections in a single array:
|
||||
|
||||
```json
|
||||
[{
|
||||
"name": "default",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}, {
|
||||
"name": "second-connection",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}]
|
||||
```
|
||||
|
||||
### Loading from `ormconfig.js`
|
||||
|
||||
Create `ormconfig.js` file in root of your project. It should have following content:
|
||||
|
||||
```javascript
|
||||
module.exports = {
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}
|
||||
```
|
||||
|
||||
You can specify any other options from `ConnectionOptions`.
|
||||
If you want to create multiple connections then simply create multiple connections in a single array and return it.
|
||||
|
||||
|
||||
### Loading from `ormconfig.env` or from environment variables
|
||||
|
||||
Create `ormconfig.env` file in root of your project. It should have following content:
|
||||
|
||||
```env
|
||||
TYPEORM_CONNECTION = mysql
|
||||
TYPEORM_HOST = localhost
|
||||
TYPEORM_USERNAME = root
|
||||
TYPEORM_PASSWORD = admin
|
||||
TYPEORM_PORT = 3000
|
||||
TYPEORM_LOGGING = true
|
||||
```
|
||||
|
||||
`ormconfig.env` should be used only during development.
|
||||
On production you can set all these values in real ENVIRONMENT VARIABLES.
|
||||
|
||||
You cannot define multiple connections using `env` file or environment variables.
|
||||
If your app has multiple connections then use alternative configuration storage format.
|
||||
183
docs/connection-options.md
Normal file
183
docs/connection-options.md
Normal file
@ -0,0 +1,183 @@
|
||||
# Connection Options
|
||||
|
||||
* What is `ConnectionOptions`
|
||||
* Common connection options
|
||||
* `mysql` / `mariadb` connection options
|
||||
* `postgres` connection options
|
||||
* `sqlite` / `websql` connection options
|
||||
* `mssql` connection options
|
||||
* `mongodb` connection options
|
||||
* Connection options example
|
||||
|
||||
## What is `ConnectionOptions` interface
|
||||
|
||||
Connection options is a connection configuration object you pass to `createConnection` method
|
||||
or create in `ormconfig` file. Different drivers have their own specific connection options.
|
||||
|
||||
## Common connection options
|
||||
|
||||
* `type` - Database type. You must specify what database engine you use.
|
||||
Possible values are "mysql", "postgres", "mariadb", "sqlite", "oracle", "mssql", "websql", "mongodb".
|
||||
This option is required.
|
||||
|
||||
* `name` - Connection name. You'll use it to get connection you need using `getConnection(name: string)`
|
||||
or `ConnectionManager.get(name: string)` methods.
|
||||
Connection names for different connections cannot be same - they all must be unique.
|
||||
If connection name is not given then it will be called "default".
|
||||
|
||||
* `extra` - Extra connection options to be passed to the underlying driver.
|
||||
Use it if you want to pass extra settings to underlying database driver.
|
||||
|
||||
* `entities` - Entities to be loaded and used for this connection.
|
||||
Accepts both entity classes and directories where from they must to be loaded.
|
||||
Directories support glob patterns.
|
||||
Example: `entities: [Post, Category, "entity/*.js", "modules/**/entity/*.js"]`.
|
||||
For more information about entities refer to [Entities](./entities.md) documentation.
|
||||
|
||||
* `subscribers` - Subscribers to be loaded and used for this connection.
|
||||
Accepts both entity classes and directories where from they must to be loaded.
|
||||
Directories support glob patterns.
|
||||
Example: `subscribers: [PostSubscriber, AppSubscriber, "subscriber/*.js", "modules/**/subscriber/*.js"]`.
|
||||
For more information about subscribers refer to [Subscribers](./subscribers-and-listeners.md) documentation.
|
||||
|
||||
* `entitySchemas` - Entity schemas to be loaded and used for this connection.
|
||||
Accepts both entity schema classes and directories where from they must to be loaded.
|
||||
Directories support glob patterns.
|
||||
Example: `entitySchemas: [PostSchema, CategorySchema, "entity-schema/*.json", "modules/**/entity-schema/*.json"]`.
|
||||
For more information about subscribers refer to [Entity Schemas](./schema-in-files.md) documentation.
|
||||
|
||||
* `migrations` - Migrations to be loaded and used for this connection.
|
||||
Accepts both migration classes and directories where from they must to be loaded.
|
||||
Directories support glob patterns.
|
||||
Example: `migrations: [FirstMigration, SecondMigration, "migration/*.js", "modules/**/migration/*.js"]`.
|
||||
For more information about migrations refer to [Migrations](./migrations.md) documentation.
|
||||
|
||||
* `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"]`.
|
||||
For more information about logging refer to [Logging](./logging.md) documentation.
|
||||
|
||||
* `logger` - Logger to be used for logging purposes. Possible values are "advanced-console", "simple-console" and "file".
|
||||
Default is "advanced-console". You can also specify a logger class that implements `Logger` interface.
|
||||
For more information about logging refer to [Logging](./logging.md) documentation.
|
||||
|
||||
* `maxQueryExecutionTime` - If query execution time exceed this given max execution time (in milliseconds)
|
||||
then logger will log this query.
|
||||
|
||||
* `namingStrategy` - Naming strategy to be used to name tables and columns in the database.
|
||||
Refer to [Naming strategy](./naming-strategy.md) documentation for more information.
|
||||
|
||||
* `entityPrefix` - Prefixes with the given string all tables (or collections) on this database connection.
|
||||
|
||||
* `dropSchema` - Drops the schema each time connection is being established.
|
||||
Be careful with this option and don't use this in production - otherwise you'll loose 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 loose production data.
|
||||
This option is useful during debug and development.
|
||||
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 indices.
|
||||
|
||||
* `migrationsRun` - Indicates if migrations should be auto run on every application launch.
|
||||
Alternative to it, you can use CLI and run migrations:run command.
|
||||
|
||||
* `cli.entitiesDir` - Directory where entities should be created by default by CLI.
|
||||
|
||||
* `cli.migrationsDir` - Directory where migrations should be created by default by CLI.
|
||||
|
||||
* `cli.subscribersDir` - Directory where subscribers should be created by default by CLI.
|
||||
|
||||
## `mysql` / `mariadb` connection options
|
||||
|
||||
* `url` - Connection url where perform connection to.
|
||||
|
||||
* `host` - Database host.
|
||||
|
||||
* `port` - Database host port. Default mysql port is `3306`.
|
||||
|
||||
* `username` - Database username.
|
||||
|
||||
* `password` - Database password.
|
||||
|
||||
* `database` - Database name.
|
||||
|
||||
## `postgres` connection options
|
||||
|
||||
* `url` - Connection url where perform connection to.
|
||||
|
||||
* `host` - Database host.
|
||||
|
||||
* `port` - Database host port. Default mysql port is `5432`.
|
||||
|
||||
* `username` - Database username.
|
||||
|
||||
* `password` - Database password.
|
||||
|
||||
* `database` - Database name.
|
||||
|
||||
* `schema` - Schema name. Default is "public".
|
||||
|
||||
## `sqlite` / `websql` connection options
|
||||
|
||||
* `database` - Database path. For example "./mydb.sql"
|
||||
|
||||
## `mssql` connection options
|
||||
|
||||
* `url` - Connection url where perform connection to.
|
||||
|
||||
* `host` - Database host.
|
||||
|
||||
* `port` - Database host port. Default mssql port is `1433`.
|
||||
|
||||
* `username` - Database username.
|
||||
|
||||
* `password` - Database password.
|
||||
|
||||
* `database` - Database name.
|
||||
|
||||
* `schema` - Schema name. Default is "public".
|
||||
|
||||
## `mongodb` connection options
|
||||
|
||||
* `url` - Connection url where perform connection to.
|
||||
|
||||
* `host` - Database host.
|
||||
|
||||
* `port` - Database host port. Default mongodb port is `27017`.
|
||||
|
||||
* `database` - Database name.
|
||||
|
||||
## Connection options example
|
||||
|
||||
Here is small example of connection options for mysql:
|
||||
|
||||
```typescript
|
||||
{
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
logging: true,
|
||||
synchronize: true,
|
||||
entities: [
|
||||
"entity/*.js"
|
||||
],
|
||||
subscribers: [
|
||||
"subscriber/*.js"
|
||||
],
|
||||
entitySchemas: [
|
||||
"schema/*.json"
|
||||
],
|
||||
migrations: [
|
||||
"migration/*.js"
|
||||
],
|
||||
cli: {
|
||||
entitiesDir: "entity",
|
||||
migrationsDir: "migration",
|
||||
subscribersDir: "subscriber"
|
||||
}
|
||||
}
|
||||
```
|
||||
681
docs/connection.md
Normal file
681
docs/connection.md
Normal file
@ -0,0 +1,681 @@
|
||||
# Connection and connection options
|
||||
|
||||
* What is `Connection` in TypeORM
|
||||
* Creating a new connection
|
||||
* Creating a new connection using main api
|
||||
* Creating connection using `ConnectionManager`
|
||||
* Creating a new connection from the configuration files
|
||||
* Loading from `ormconfig.json`
|
||||
* Loading from `ormconfig.js`
|
||||
* Loading from `ormconfig.env` or from environment variables
|
||||
* Loading from `ormconfig.yml`
|
||||
* Loading from `ormconfig.xml`
|
||||
* Working with connection
|
||||
* Connection usage example in sample express application
|
||||
* Using service container and typedi extensions
|
||||
* API
|
||||
* Main API
|
||||
* `Connection` class API
|
||||
* `ConnectionManager` class API
|
||||
|
||||
## What is `Connection` in TypeORM
|
||||
|
||||
Connection is a TypeORM class which setups a real connection with your database.
|
||||
Depend on database type it may also setup a connection pool.
|
||||
Connection (or connection pool) setup is made once its `connection` method is called.
|
||||
Disconnection (or closing all connections in the pool) is made once its `close` method is called.
|
||||
You must create TypeORM `Connection` only once in your application bootstrap.
|
||||
|
||||
## Creating a new connection
|
||||
|
||||
### Creating a new connection using main api
|
||||
|
||||
There are several ways how connection can be created.
|
||||
The most simple and common way is to use main api `createConnection` and `createConnections` methods.
|
||||
|
||||
`createConnection` creates a single connection:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
const connection: Connection = await createConnection({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
```
|
||||
|
||||
`createConnections` creates multiple connection:
|
||||
|
||||
```typescript
|
||||
import {createConnections, Connection} from "typeorm";
|
||||
|
||||
const connections: Connection[] = await createConnections([{
|
||||
name: "default",
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
}, {
|
||||
name: "test2-connection",
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test2",
|
||||
}]);
|
||||
```
|
||||
|
||||
Both these methods automatically call `Connection#connect` method.
|
||||
Both these methods automatically load configuration from `ormconfig` file if connection options are not specified.
|
||||
For example:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
// here createConnection will load connection options from
|
||||
// ormconfig.json / ormconfig.js / ormconfig.yml / ormconfig.env / ormconfig.xml
|
||||
// files, or from special environment variables
|
||||
// if ormconfig contains multiple connections then it will load connection named "default"
|
||||
// or connection without name at all (which means it will be named "default" by default)
|
||||
const connection: Connection = await createConnection();
|
||||
|
||||
// if createConnections is called instead of createConnection then it will initialize and return all connections
|
||||
// defined in ormconfig file
|
||||
```
|
||||
|
||||
Different connections must have different connection names.
|
||||
Different connections cannot have same connection name.
|
||||
By default, if connection name is not specified it is equal to `default`.
|
||||
Usually use multiple connections when you have multiple databases or multiple connection configurations.
|
||||
|
||||
Once you create a connection you can obtain it anywhere, using `getConnection` method:
|
||||
|
||||
```typescript
|
||||
import {getConnection} from "typeorm";
|
||||
|
||||
// can be used once createConnection is called and is resolved
|
||||
const connection = getConnection();
|
||||
|
||||
// if you have multiple connections you can get connection by name:
|
||||
const secondConnection = getConnection("test2-connection");
|
||||
```
|
||||
|
||||
Avoid creating extra classes / services which store instance of your connections.
|
||||
This functionality is already embed into TypeORM -
|
||||
you don't need to overengineer and create useless abstractions.
|
||||
|
||||
### Creating connection using `ConnectionManager`
|
||||
|
||||
You can create connection using `ConnectionManager` class. For example:
|
||||
|
||||
```typescript
|
||||
import {getConnectionManager, ConnectionManager, Connection} from "typeorm";
|
||||
|
||||
const connectionManager: ConnectionManager = getConnectionManager(); // or you can initialize your own connection manager like this: new ConnectionManager()
|
||||
const connection: Connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
await connection.connect(); // performs connection
|
||||
```
|
||||
|
||||
This is not general way of creating connection, but it may be useful for some users.
|
||||
For example users who want to create connection and store its instance,
|
||||
but have a control when actual "connection" will be established.
|
||||
Also you can create and maintain your own `ConnectionManager`:
|
||||
|
||||
```typescript
|
||||
import {getConnectionManager, ConnectionManager, Connection} from "typeorm";
|
||||
|
||||
const connectionManager = new ConnectionManager(); // or you can initialize your own connection manager like this: new ConnectionManager()
|
||||
const connection: Connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
});
|
||||
await connection.connect(); // performs connection
|
||||
```
|
||||
|
||||
But note, this way you won't be able to use `getConnection()` method anymore -
|
||||
you'll need to store your connection manager instance and use `connectionManager.get` method to get a connection you need.
|
||||
|
||||
Generally avoid this method and avoid unnecessary complications in your application,
|
||||
use `ConnectionManager` only if you really think you need it.
|
||||
|
||||
## Creating a new connection from the configuration files
|
||||
|
||||
Most of the times you want to store your connection options in a separate configuration file.
|
||||
It makes it convenient and easy to manage.
|
||||
TypeORM supports multiple configuration sources of this purpose.
|
||||
You'll only need to create `ormconfig.[format]` file in root directory of your application (near `package.json`),
|
||||
write configuration there and in your app simply call `createConnection()` without any configuration passed:
|
||||
|
||||
```typescript
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
|
||||
// createConnection method will automatically read connection options from your ormconfig file
|
||||
const connection: Connection = await createConnection();
|
||||
```
|
||||
|
||||
### Loading from `ormconfig.json`
|
||||
|
||||
Create `ormconfig.json` file in root of your project. It should have following content:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}
|
||||
```
|
||||
|
||||
You can specify any other options from `ConnectionOptions`.
|
||||
|
||||
If you want to create multiple connections then simply create multiple connections in a single array:
|
||||
|
||||
```json
|
||||
[{
|
||||
"name": "default",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}, {
|
||||
"name": "second-connection",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}]
|
||||
```
|
||||
|
||||
### Loading from `ormconfig.js`
|
||||
|
||||
Create `ormconfig.js` file in root of your project. It should have following content:
|
||||
|
||||
```javascript
|
||||
module.exports = {
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}
|
||||
```
|
||||
|
||||
You can specify any other options from `ConnectionOptions`.
|
||||
If you want to create multiple connections then simply create multiple connections in a single array and return it.
|
||||
|
||||
### Loading from `ormconfig.env` or from environment variables
|
||||
|
||||
Create `ormconfig.env` file in root of your project. It should have following content:
|
||||
|
||||
```env
|
||||
TYPEORM_CONNECTION = mysql
|
||||
TYPEORM_HOST = localhost
|
||||
TYPEORM_USERNAME = root
|
||||
TYPEORM_PASSWORD = admin
|
||||
TYPEORM_PORT = 3000
|
||||
TYPEORM_AUTO_SCHEMA_SYNC = true
|
||||
TYPEORM_LOGGING = true
|
||||
TYPEORM_ENTITIES = entity/.*js,modules/**/entity/.*js
|
||||
```
|
||||
|
||||
List of available env variables you can set:
|
||||
|
||||
* TYPEORM_CONNECTION
|
||||
* TYPEORM_HOST
|
||||
* TYPEORM_USERNAME
|
||||
* TYPEORM_PASSWORD
|
||||
* TYPEORM_PORT
|
||||
* TYPEORM_AUTO_SCHEMA_SYNC
|
||||
* TYPEORM_URL
|
||||
* TYPEORM_SID
|
||||
* TYPEORM_ENTITIES
|
||||
* TYPEORM_MIGRATIONS
|
||||
* TYPEORM_SUBSCRIBERS
|
||||
* TYPEORM_ENTITY_SCHEMAS
|
||||
* TYPEORM_LOGGING
|
||||
* TYPEORM_ENTITIES_DIR
|
||||
* TYPEORM_MIGRATIONS_DIR
|
||||
* TYPEORM_SUBSCRIBERS_DIR
|
||||
* TYPEORM_DRIVER_EXTRA
|
||||
|
||||
`ormconfig.env` should be used only during development.
|
||||
On production you can set all these values in real ENVIRONMENT VARIABLES.
|
||||
|
||||
You cannot define multiple connections using `env` file or environment variables.
|
||||
If your app has multiple connections then use alternative configuration storage format.
|
||||
|
||||
### Loading from `ormconfig.yml`
|
||||
|
||||
Create `ormconfig.yml` file in root of your project. It should have following content:
|
||||
|
||||
```yaml
|
||||
default: # default connection
|
||||
host: "localhost"
|
||||
port: 3306
|
||||
username: "test"
|
||||
password: "test"
|
||||
database: "test"
|
||||
|
||||
second-connection: # other connections
|
||||
host: "localhost"
|
||||
port: 3306
|
||||
username: "test"
|
||||
password: "test"
|
||||
database: "test2"
|
||||
```
|
||||
|
||||
You can use any connection options available.
|
||||
|
||||
### Loading from `ormconfig.xml`
|
||||
|
||||
Create `ormconfig.xml` file in root of your project. It should have following content:
|
||||
|
||||
```xml
|
||||
<connections>
|
||||
<connection type="mysql" name="default">
|
||||
<host>localhost</host>
|
||||
<username>root</username>
|
||||
<password>admin</password>
|
||||
<database>test</database>
|
||||
<port>3000</port>
|
||||
<logging>true</logging>
|
||||
</connection>
|
||||
<connection type="mysql" name="second-connection">
|
||||
<host>localhost</host>
|
||||
<username>root</username>
|
||||
<password>admin</password>
|
||||
<database>test2</database>
|
||||
<port>3000</port>
|
||||
<logging>true</logging>
|
||||
</connection>
|
||||
</connections>
|
||||
```
|
||||
|
||||
You can use any connection options available.
|
||||
|
||||
### Working with connection
|
||||
|
||||
Once you setup connection you can use it anywhere in your app using `getConnection` method.
|
||||
For example:
|
||||
|
||||
```typescript
|
||||
import {getConnection} from "typeorm";
|
||||
import {User} from "../entity/User";
|
||||
|
||||
export class UserController {
|
||||
|
||||
@Get("/users")
|
||||
getAll() {
|
||||
return getConnection().manager.find(User);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
You can also use `ConnectionManager#get` method to get a connection,
|
||||
but using `getConnection()` method is enough in most cases.
|
||||
|
||||
Using connection you work with your entities, particularly using `EntityManager` and `Repository`.
|
||||
For more information about them see [Entity Manager and Repository](./entity-manager-and-repository.md) documentation.
|
||||
|
||||
## API
|
||||
|
||||
### 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.
|
||||
|
||||
```typescript
|
||||
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 it in global connection manager.
|
||||
If connection options parameter is omitted then connection options are read from `ormconfig` file or environment variables.
|
||||
|
||||
```typescript
|
||||
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 connections using `createConnection` method.
|
||||
|
||||
```typescript
|
||||
import {getConnectionManager} from "typeorm";
|
||||
|
||||
const defaultConnection = getConnectionManager().get("default");
|
||||
const secondaryConnection = getConnectionManager().get("secondary");
|
||||
```
|
||||
|
||||
* `getConnection()` - Gets connection which is created using `createConnection` method.
|
||||
|
||||
```typescript
|
||||
import {getConnection} from "typeorm";
|
||||
|
||||
const connection = getConnection();
|
||||
// if you have named connection you can specify its name:
|
||||
const secondaryConnection = getConnection("secondary-connection");
|
||||
```
|
||||
|
||||
* `getEntityManager()` - Gets `EntityManager` from connection.
|
||||
Connection name can be specified to indicate what connection's entity manager should be taken.
|
||||
|
||||
```typescript
|
||||
import {getEntityManager} from "typeorm";
|
||||
|
||||
const manager = getEntityManager();
|
||||
// you can use manager methods now
|
||||
|
||||
const secondaryManager = getEntityManager("secondary-connection");
|
||||
// you can use secondary connection manager methods
|
||||
```
|
||||
|
||||
* `getRepository()` - Gets some entity's `Repository` from connection.
|
||||
Connection name can be specified to indicate what connection's entity manager should be taken.
|
||||
|
||||
```typescript
|
||||
import {getRepository} from "typeorm";
|
||||
|
||||
const userRepository = getRepository(User);
|
||||
// you can use repository methods now
|
||||
|
||||
const blogRepository = getRepository(Blog, "secondary-connection");
|
||||
// you can use secondary connection repository methods
|
||||
```
|
||||
|
||||
* `getTreeRepository()` - Gets some entity's `TreeRepository` from connection.
|
||||
Connection name can be specified to indicate what connection's entity manager should be taken.
|
||||
|
||||
```typescript
|
||||
import {getTreeRepository} from "typeorm";
|
||||
|
||||
const userRepository = getTreeRepository(User);
|
||||
// you can use repository methods now
|
||||
|
||||
const blogRepository = getTreeRepository(Blog, "secondary-connection");
|
||||
// you can use secondary connection repository methods
|
||||
```
|
||||
|
||||
* `getMongoRepository()` - Gets some entity's `MongoRepository` from connection.
|
||||
Connection name can be specified to indicate what connection's entity manager should be taken.
|
||||
|
||||
```typescript
|
||||
import {getMongoRepository} from "typeorm";
|
||||
|
||||
const userRepository = getMongoRepository(User);
|
||||
// you can use repository methods now
|
||||
|
||||
const blogRepository = getMongoRepository(Blog, "secondary-connection");
|
||||
// you can use secondary connection repository methods
|
||||
```
|
||||
|
||||
### `Connection` API
|
||||
|
||||
* `name` - Connection name. If you created nameless connection then its equal to "default".
|
||||
You use this name when you work with multiple connections and call `getConnection(connectionName: string)`
|
||||
|
||||
```typescript
|
||||
const connectionName: string = connection.name;
|
||||
```
|
||||
|
||||
* `options` - Connection options used to create this connection.
|
||||
For more information about connection options see [Connection Options](./connection-options.md) documentation.
|
||||
|
||||
```typescript
|
||||
const connectionOptions: ConnectionOptions = connection.options;
|
||||
// you can cast connectionOptions to MysqlConnectionOptions or any other xxxConnectionOptions depend on database driver you use
|
||||
```
|
||||
|
||||
* `isConnected` - Indicates if real connection to the database is established.
|
||||
|
||||
```typescript
|
||||
const isConnected: boolean = connection.isConnected;
|
||||
```
|
||||
|
||||
* `driver` - Underlying database driver used in this connection.
|
||||
|
||||
```typescript
|
||||
const driver: Driver = connection.driver;
|
||||
// you can cast connectionOptions to MysqlDriver or any other xxxDriver depend on database driver you use
|
||||
```
|
||||
|
||||
* `manager` - `EntityManager` used to work with connection entities.
|
||||
For more information about EntityManager see [Entity Manager and Repository](./entity-manager-and-repository.md) documentation.
|
||||
|
||||
```typescript
|
||||
const manager: EntityManager = connection.manager;
|
||||
// you can call manager methods, for example find:
|
||||
const user = await manager.findOneById(1);
|
||||
```
|
||||
|
||||
* `mongoManager` - `MongoEntityManager` used to work with connection entities in mongodb connections.
|
||||
For more information about MongoEntityManager see [MongoDB](./mongodb.md) documentation.
|
||||
|
||||
```typescript
|
||||
const manager: MongoEntityManager = connection.mongoManager;
|
||||
// you can call manager or mongodb-manager specific methods, for example find:
|
||||
const user = await manager.findOneById(1);
|
||||
```
|
||||
|
||||
* `connect` - Performs connection to the database.
|
||||
When you use `createConnection` method it automatically calls this method and you usually don't need to call it by yourself.
|
||||
|
||||
```typescript
|
||||
await connection.connect();
|
||||
```
|
||||
|
||||
* `close` - Closes connection with the database.
|
||||
Usually you call this method when your application is shutdown.
|
||||
|
||||
```typescript
|
||||
await connection.close();
|
||||
```
|
||||
|
||||
* `synchronize` - Synchronizes database schema. When `synchronize: true` is set in connection options it calls exactly this method.
|
||||
Usually you call this method when your application is shutdown.
|
||||
|
||||
```typescript
|
||||
await connection.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.
|
||||
|
||||
```typescript
|
||||
await connection.dropDatabase();
|
||||
```
|
||||
|
||||
* `runMigrations` - Runs all pending migrations.
|
||||
|
||||
```typescript
|
||||
await connection.runMigrations();
|
||||
```
|
||||
|
||||
* `undoLastMigration` - Reverts last executed migration.
|
||||
|
||||
```typescript
|
||||
await connection.undoLastMigration();
|
||||
```
|
||||
|
||||
* `hasMetadata` - Checks if metadata for a given entity is registered.
|
||||
Learn more about metadata in [Entity Metadata](./entity-metadata.md) documentation.
|
||||
|
||||
```typescript
|
||||
if (connection.hasMetadata(User))
|
||||
const userMetadata = connection.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.
|
||||
Learn more about metadata in [Entity Metadata](./entity-metadata.md) documentation.
|
||||
|
||||
```typescript
|
||||
const userMetadata = connection.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 in [Repository](./entity-manager-and-repository.md) documentation.
|
||||
|
||||
```typescript
|
||||
const repository = connection.getRepository(User);
|
||||
// now you can call repository methods, for example find:
|
||||
const users = await repository.findOneById(1);
|
||||
```
|
||||
|
||||
* `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 in [Repository](./entity-manager-and-repository.md) documentation.
|
||||
|
||||
```typescript
|
||||
const repository = connection.getTreeRepository(Category);
|
||||
// now you can call tree repository methods, for example findTrees:
|
||||
const categories = await repository.findTrees();
|
||||
```
|
||||
|
||||
* `getMongoRepository` - Gets `getMongoRepository` of the given entity.
|
||||
This repository is used for entities in MongoDB connection.
|
||||
Learn more about mongodb support refer to [MongoDB documentation](./mongodb.md).
|
||||
|
||||
```typescript
|
||||
const repository = connection.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();
|
||||
```
|
||||
|
||||
* `getCustomRepository` - Gets customly defined repository.
|
||||
Learn more about custom repositories in [Repository](./entity-manager-and-repository.md) documentation.
|
||||
|
||||
```typescript
|
||||
const userRepository = connection.getCustomRepository(UserRepository);
|
||||
// now you can call methods inside your custom repository - UserRepository class
|
||||
const crazyUsers = await userRepository.findCrazyUsers();
|
||||
```
|
||||
|
||||
* `transaction` - Provides a single transaction where multiple database requests will be executed in a single database transaction.
|
||||
Learn more about transactions in [Transactions](./transactions.md) documentation.
|
||||
|
||||
```typescript
|
||||
await connection.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 given raw SQL query.
|
||||
|
||||
```typescript
|
||||
const rawData = await connection.query(`SELECT * FROM USERS`);
|
||||
```
|
||||
|
||||
* `createQueryBuilder` - Creates a query builder use to build SQL queries.
|
||||
Learn more about query builder in [QueryBuilder](./query-builder.md) documentation.
|
||||
|
||||
```typescript
|
||||
const users = await connection.createQueryBuilder()
|
||||
.select()
|
||||
.from(User, "user")
|
||||
.where("user.name = :name", { name: "John" })
|
||||
.getMany();
|
||||
```
|
||||
|
||||
* `createQueryRunner` - Creates a query runner used to work with a single real database connection, manage it and work with it.
|
||||
Learn more about query runners in [QueryRunner](./query-runner.md) documentation.
|
||||
|
||||
```typescript
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
|
||||
// you can use it methods only after you call connect method
|
||||
// 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();
|
||||
```
|
||||
|
||||
### `ConnectionManager` API
|
||||
|
||||
* `create` - Creates a new connection and register it in the manager.
|
||||
|
||||
```typescript
|
||||
const connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test"
|
||||
});
|
||||
```
|
||||
|
||||
* `get` - Gets already created connection stored in the manager by its name.
|
||||
|
||||
```typescript
|
||||
const defaultConnection = connectionManager.get("default");
|
||||
const secondaryConnection = connectionManager.get("secondary");
|
||||
```
|
||||
|
||||
* `has` - Checks if connection is registered in the given connection manager.
|
||||
|
||||
```typescript
|
||||
if (connectionManager.has("default")) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
1
docs/entities.md
Normal file
1
docs/entities.md
Normal file
@ -0,0 +1 @@
|
||||
# What is Entity?
|
||||
1
docs/entity-metadata.md
Normal file
1
docs/entity-metadata.md
Normal file
@ -0,0 +1 @@
|
||||
# Entity Metadata
|
||||
2
docs/query-runner.md
Normal file
2
docs/query-runner.md
Normal file
@ -0,0 +1,2 @@
|
||||
# Query Runner
|
||||
|
||||
@ -34,7 +34,7 @@ const options: ConnectionOptions = {
|
||||
// logging: ["query", "error"],
|
||||
// logging: ["error", "schema", "query"],
|
||||
// maxQueryExecutionTime: 90,
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post]
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [EverythingEntity]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
namingStrategy: new CustomNamingStrategy(),
|
||||
entities: [Post]
|
||||
};
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, PostAuthor]
|
||||
};
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ const options: ConnectionOptions = {
|
||||
password: "test",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, BasePost]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post]
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author, Category]
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author, Category, PostMetadata]
|
||||
};
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, PostDetails, PostCategory, PostMetadata, PostImage, PostInformation, PostAuthor]
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author, Category]
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author, Category]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Category]
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author, Category]
|
||||
};
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
// entitySchemaDirectories: [__dirname + "/schemas"],
|
||||
entitySchemas: [
|
||||
require(__dirname + "/../../../../sample/sample24-schemas/schemas/post.json"),
|
||||
|
||||
@ -11,7 +11,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Author]
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Question, Counters]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post]
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [
|
||||
Person,
|
||||
Employee,
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [
|
||||
Person,
|
||||
Employee,
|
||||
|
||||
@ -20,7 +20,7 @@ const options: ConnectionOptions = {
|
||||
password: "oracle",
|
||||
port: 1521,
|
||||
sid: "xe.oracle.docker",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
logging: ["query", "error"],
|
||||
entities: [Post, PostDetails, PostCategory, PostMetadata, PostImage, PostInformation, PostAuthor]
|
||||
};
|
||||
|
||||
@ -7,7 +7,7 @@ const options: ConnectionOptions = {
|
||||
type: "sqlite",
|
||||
database: "temp/sqlitedb.db",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, Category]
|
||||
};
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@ import {Category} from "./entity/Category";
|
||||
const options: ConnectionOptions = {
|
||||
type: "sqlite",
|
||||
database: "temp/sqlitedb.db",
|
||||
tablesPrefix: "samples_", // pay attention on this prefix
|
||||
autoSchemaSync: true,
|
||||
entityPrefix: "samples_", // pay attention on this prefix
|
||||
synchronize: true,
|
||||
logging: ["query", "error"],
|
||||
entities: [Post, Author, Category],
|
||||
};
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
logging: ["query", "error"],
|
||||
entities: [Post, Author],
|
||||
};
|
||||
|
||||
@ -14,7 +14,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
logging: ["query", "error"],
|
||||
entities: [Post, Author, User],
|
||||
};
|
||||
|
||||
@ -8,16 +8,16 @@ import {User} from "../entity/User";
|
||||
@EntityRepository()
|
||||
export class UserRepository {
|
||||
|
||||
constructor(private entityManager: EntityManager) {
|
||||
constructor(private manager: EntityManager) {
|
||||
}
|
||||
|
||||
async createAndSave(firstName: string, lastName: string) {
|
||||
const user = await this.entityManager.create(User, { firstName, lastName });
|
||||
return this.entityManager.save(user);
|
||||
const user = await this.manager.create(User, { firstName, lastName });
|
||||
return this.manager.save(user);
|
||||
}
|
||||
|
||||
async findByName(firstName: string, lastName: string) {
|
||||
return this.entityManager.createQueryBuilder(User, "user")
|
||||
return this.manager.createQueryBuilder(User, "user")
|
||||
.where("user.firstName = :firstName AND user.lastName = :lastName")
|
||||
.setParameters({ firstName, lastName })
|
||||
.getOne();
|
||||
|
||||
@ -7,7 +7,7 @@ const options: ConnectionOptions = {
|
||||
host: "localhost",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
// autoSchemaSync: true,
|
||||
// synchronize: true,
|
||||
entities: [Post]
|
||||
};
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ const options: ConnectionOptions = {
|
||||
password: "admin12345",
|
||||
database: "test",
|
||||
logging: ["query", "error"],
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [Post, PostAuthor, PostCategory],
|
||||
subscribers: [EverythingSubscriber]
|
||||
};
|
||||
|
||||
@ -12,7 +12,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"]
|
||||
};
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ const options: ConnectionOptions = {
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaSync: true,
|
||||
synchronize: true,
|
||||
entities: [__dirname + "/entity/*"],
|
||||
subscribers: [__dirname + "/subscriber/*"]
|
||||
};
|
||||
|
||||
@ -61,7 +61,7 @@ export class MigrationGenerateCommand {
|
||||
logging: { logQueries: false, logFailedQueryError: false, logSchemaCreation: false }
|
||||
});
|
||||
connection = await createConnection(connectionOptions);
|
||||
const sqlQueries = await connection.logSyncSchema();
|
||||
const sqlQueries = await connection.driver.createSchemaBuilder().log();
|
||||
const upSqls: string[] = [], downSqls: string[] = [];
|
||||
|
||||
// mysql is exceptional here because it uses ` character in to escape names in queries, thats why for mysql
|
||||
|
||||
@ -41,7 +41,7 @@ export class SchemaLogCommand {
|
||||
logging: { logQueries: false, logFailedQueryError: false, logSchemaCreation: false }
|
||||
});
|
||||
connection = await createConnection(connectionOptions);
|
||||
const sqls = await connection.logSyncSchema();
|
||||
const sqls = await connection.driver.createSchemaBuilder().log();
|
||||
if (sqls.length === 0) {
|
||||
console.log(chalk.yellow("Your schema is up to date - there are no queries to be executed by schema syncronization."));
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ export class SchemaSyncCommand {
|
||||
}
|
||||
});
|
||||
connection = await createConnection(connectionOptions);
|
||||
await connection.syncSchema(false);
|
||||
await connection.synchronize(false);
|
||||
console.log(chalk.green("Schema syncronization finished successfully."));
|
||||
|
||||
} catch (err) {
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import {DriverOptions} from "../driver/DriverOptions";
|
||||
import {EntitySchema} from "../entity-schema/EntitySchema";
|
||||
import {LoggerOptions} from "../logger/LoggerOptions";
|
||||
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategyInterface";
|
||||
@ -12,10 +11,8 @@ export interface BaseConnectionOptions {
|
||||
|
||||
/**
|
||||
* Database type. This value is required.
|
||||
*
|
||||
* "?" is temporary.
|
||||
*/
|
||||
readonly type?: DatabaseType;
|
||||
readonly type: DatabaseType;
|
||||
|
||||
/**
|
||||
* Connection name. If connection name is not given then it will be called "default".
|
||||
@ -23,32 +20,6 @@ export interface BaseConnectionOptions {
|
||||
*/
|
||||
readonly name?: string;
|
||||
|
||||
/**
|
||||
* Database options of this connection.
|
||||
*
|
||||
* @deprecated Define options right in the connection options section.
|
||||
*/
|
||||
readonly driver?: DriverOptions;
|
||||
|
||||
/**
|
||||
* Extra connection options to be passed to the underlying driver.
|
||||
*
|
||||
* todo: deprecate this and move all database-specific types into hts own connection options object.
|
||||
*/
|
||||
readonly extra?: any;
|
||||
|
||||
/**
|
||||
* Prefix to use on all tables (collections) of this connection in the database.
|
||||
*
|
||||
* todo: rename to entityPrefix
|
||||
*/
|
||||
readonly tablesPrefix?: string;
|
||||
|
||||
/**
|
||||
* Naming strategy to be used to name tables and columns in the database.
|
||||
*/
|
||||
readonly namingStrategy?: NamingStrategyInterface;
|
||||
|
||||
/**
|
||||
* Entities to be loaded for this connection.
|
||||
* Accepts both entity classes and directories where from entities need to be loaded.
|
||||
@ -77,6 +48,11 @@ export interface BaseConnectionOptions {
|
||||
*/
|
||||
readonly migrations?: Function[]|string[];
|
||||
|
||||
/**
|
||||
* Naming strategy to be used to name tables and columns in the database.
|
||||
*/
|
||||
readonly namingStrategy?: NamingStrategyInterface;
|
||||
|
||||
/**
|
||||
* Logging options.
|
||||
*/
|
||||
@ -92,13 +68,6 @@ export interface BaseConnectionOptions {
|
||||
*/
|
||||
readonly maxQueryExecutionTime?: number;
|
||||
|
||||
/**
|
||||
* Drops the schema each time connection is being established.
|
||||
* Be careful with this option and don't use this in production - otherwise you'll loose all production data.
|
||||
* This option is useful during debug and development.
|
||||
*/
|
||||
readonly dropSchemaOnConnection?: boolean;
|
||||
|
||||
/**
|
||||
* 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 loose production data.
|
||||
@ -107,18 +76,33 @@ export interface BaseConnectionOptions {
|
||||
*
|
||||
* Note that for MongoDB database it does not create schema, because MongoDB is schemaless.
|
||||
* Instead, it syncs just by creating indices.
|
||||
*
|
||||
* todo: rename it simply to synchronize: boolean ?
|
||||
*/
|
||||
readonly autoSchemaSync?: boolean;
|
||||
readonly synchronize?: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if migrations should be auto run on every application launch.
|
||||
* Alternative to it, you can use CLI and run migration:create command.
|
||||
*
|
||||
* todo: rename it simply to runMigrations: boolean ?
|
||||
* Alternative to it, you can use CLI and run migrations:run command.
|
||||
*/
|
||||
readonly autoMigrationsRun?: boolean;
|
||||
readonly migrationsRun?: boolean;
|
||||
|
||||
/**
|
||||
* Drops the schema each time connection is being established.
|
||||
* Be careful with this option and don't use this in production - otherwise you'll loose all production data.
|
||||
* This option is useful during debug and development.
|
||||
*/
|
||||
readonly dropSchema?: boolean;
|
||||
|
||||
/**
|
||||
* Prefix to use on all tables (collections) of this connection in the database.
|
||||
*/
|
||||
readonly entityPrefix?: string;
|
||||
|
||||
/**
|
||||
* Extra connection options to be passed to the underlying driver.
|
||||
*
|
||||
* todo: deprecate this and move all database-specific types into hts own connection options object.
|
||||
*/
|
||||
readonly extra?: any;
|
||||
|
||||
/**
|
||||
* CLI settings.
|
||||
@ -142,4 +126,24 @@ export interface BaseConnectionOptions {
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated - Use entityPrefix instead
|
||||
*/
|
||||
readonly tablesPrefix?: string;
|
||||
|
||||
/**
|
||||
* @deprecated - Use synchronize instead
|
||||
*/
|
||||
readonly autoSchemaSync?: boolean;
|
||||
|
||||
/**
|
||||
* @deprecated - Use migrationsRun instead
|
||||
*/
|
||||
readonly autoMigrationsRun?: boolean;
|
||||
|
||||
/**
|
||||
* @deprecated - Use dropSchema instead
|
||||
*/
|
||||
readonly dropSchemaOnConnection?: boolean;
|
||||
|
||||
}
|
||||
@ -13,7 +13,6 @@ import {Logger} from "../logger/Logger";
|
||||
import {EntityMetadataNotFound} from "../error/EntityMetadataNotFound";
|
||||
import {MigrationInterface} from "../migration/MigrationInterface";
|
||||
import {MigrationExecutor} from "../migration/MigrationExecutor";
|
||||
import {PlatformTools} from "../platform/PlatformTools";
|
||||
import {MongoRepository} from "../repository/MongoRepository";
|
||||
import {MongoDriver} from "../driver/mongodb/MongoDriver";
|
||||
import {MongoEntityManager} from "../entity-manager/MongoEntityManager";
|
||||
@ -25,7 +24,6 @@ import {DriverFactory} from "../driver/DriverFactory";
|
||||
import {ConnectionMetadataBuilder} from "./ConnectionMetadataBuilder";
|
||||
import {QueryRunner} from "../query-runner/QueryRunner";
|
||||
import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder";
|
||||
import {SqliteDriver} from "../driver/sqlite/SqliteDriver";
|
||||
import {LoggerFactory} from "../logger/LoggerFactory";
|
||||
|
||||
/**
|
||||
@ -145,15 +143,15 @@ export class Connection {
|
||||
this.buildMetadatas();
|
||||
|
||||
// if option is set - drop schema once connection is done
|
||||
if (this.options.dropSchemaOnConnection && !PlatformTools.getEnvVariable("SKIP_SCHEMA_CREATION"))
|
||||
if (this.options.dropSchema || this.options.dropSchemaOnConnection)
|
||||
await this.dropDatabase();
|
||||
|
||||
// if option is set - automatically synchronize a schema
|
||||
if (this.options.autoSchemaSync && !PlatformTools.getEnvVariable("SKIP_SCHEMA_CREATION"))
|
||||
await this.syncSchema();
|
||||
if (this.options.synchronize || this.options.autoSchemaSync)
|
||||
await this.synchronize();
|
||||
|
||||
// if option is set - automatically synchronize a schema
|
||||
if (this.options.autoMigrationsRun && !PlatformTools.getEnvVariable("SKIP_MIGRATIONS_RUN"))
|
||||
if (this.options.migrationsRun || this.options.autoMigrationsRun)
|
||||
await this.runMigrations();
|
||||
|
||||
} catch (error) {
|
||||
@ -185,7 +183,7 @@ export class Connection {
|
||||
*
|
||||
* @param dropBeforeSync If set to true then it drops the database with all its tables and data
|
||||
*/
|
||||
async syncSchema(dropBeforeSync: boolean = false): Promise<void> {
|
||||
async synchronize(dropBeforeSync: boolean = false): Promise<void> {
|
||||
|
||||
if (!this.isConnected)
|
||||
throw new CannotExecuteNotConnectedError(this.name);
|
||||
@ -197,17 +195,6 @@ export class Connection {
|
||||
await schemaBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sql queries generated by schema builder.
|
||||
*/
|
||||
async logSyncSchema(): Promise<(string|{ up: string, down: string })[]> {
|
||||
if (!this.isConnected)
|
||||
throw new CannotExecuteNotConnectedError(this.name);
|
||||
|
||||
const schemaBuilder = this.driver.createSchemaBuilder();
|
||||
return schemaBuilder.log();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -370,7 +357,7 @@ export class Connection {
|
||||
* This may be useful if you want to perform all db queries within one connection.
|
||||
* After finishing with entity manager, don't forget to release it (to release database connection back to pool).
|
||||
*/
|
||||
createIsolatedManager(): EntityManager {
|
||||
/*createIsolatedManager(): EntityManager {
|
||||
if (this.driver instanceof MongoDriver)
|
||||
throw new Error(`You can use createIsolatedManager only for non MongoDB connections.`);
|
||||
|
||||
@ -379,14 +366,14 @@ export class Connection {
|
||||
return this.manager;
|
||||
|
||||
return new EntityManagerFactory().create(this, this.driver.createQueryRunner());
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Creates a new repository with a single opened connection to the database.
|
||||
* This may be useful if you want to perform all db queries within one connection.
|
||||
* After finishing with repository, don't forget to release its query runner (to release database connection back to pool).
|
||||
*/
|
||||
createIsolatedRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): Repository<Entity> {
|
||||
/*createIsolatedRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): Repository<Entity> {
|
||||
if (this.driver instanceof MongoDriver)
|
||||
throw new Error(`You can use createIsolatedRepository only for non MongoDB connections.`);
|
||||
|
||||
@ -395,7 +382,7 @@ export class Connection {
|
||||
return this.manager.getRepository(entityClassOrName);
|
||||
|
||||
return this.createIsolatedManager().getRepository(entityClassOrName);
|
||||
}
|
||||
}*/
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Deprecated Public Methods
|
||||
@ -437,10 +424,8 @@ export class Connection {
|
||||
const entityMetadataValidator = new EntityMetadataValidator();
|
||||
|
||||
// create subscribers instances if they are not disallowed from high-level (for example they can disallowed from migrations run process)
|
||||
if (!PlatformTools.getEnvVariable("SKIP_SUBSCRIBERS_LOADING")) {
|
||||
const subscribers = connectionMetadataBuilder.buildSubscribers(this.options.subscribers || []);
|
||||
Object.assign(this, { subscribers: subscribers });
|
||||
}
|
||||
const subscribers = connectionMetadataBuilder.buildSubscribers(this.options.subscribers || []);
|
||||
Object.assign(this, { subscribers: subscribers });
|
||||
|
||||
// build entity metadatas
|
||||
const entityMetadatas = connectionMetadataBuilder.buildEntityMetadatas(this.options.entities || [], this.options.entitySchemas || []);
|
||||
|
||||
@ -48,10 +48,6 @@ export class ConnectionManager {
|
||||
*/
|
||||
create(options: ConnectionOptions): Connection {
|
||||
|
||||
// (backward compatibility) if options are set in the driver section of connection options then merge them into the option
|
||||
if (options.driver)
|
||||
Object.assign(options, options.driver);
|
||||
|
||||
// check if such connection is already registered
|
||||
const existConnection = this.connections.find(connection => connection.name === (options.name || "default"));
|
||||
if (existConnection) {
|
||||
|
||||
@ -27,7 +27,7 @@ export class ConnectionOptionsEnvReader {
|
||||
database: PlatformTools.getEnvVariable("TYPEORM_DATABASE"),
|
||||
sid: PlatformTools.getEnvVariable("TYPEORM_SID"),
|
||||
extra: PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA") ? JSON.parse(PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA")) : undefined,
|
||||
autoSchemaSync: OrmUtils.toBoolean(PlatformTools.getEnvVariable("TYPEORM_AUTO_SCHEMA_SYNC")),
|
||||
synchronize: OrmUtils.toBoolean(PlatformTools.getEnvVariable("TYPEORM_SYNCHRONIZE")),
|
||||
entities: PlatformTools.getEnvVariable("TYPEORM_ENTITIES") ? PlatformTools.getEnvVariable("TYPEORM_ENTITIES").split(",") : [],
|
||||
migrations: PlatformTools.getEnvVariable("TYPEORM_MIGRATIONS") ? PlatformTools.getEnvVariable("TYPEORM_MIGRATIONS").split(",") : [],
|
||||
subscribers: PlatformTools.getEnvVariable("TYPEORM_SUBSCRIBERS") ? PlatformTools.getEnvVariable("TYPEORM_SUBSCRIBERS").split(",") : [],
|
||||
|
||||
@ -1,68 +0,0 @@
|
||||
import {DatabaseType} from "./types/DatabaseType";
|
||||
|
||||
/**
|
||||
* Connectivity options used to connect to the database, and other database-driver-specific options.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export interface DriverOptions {
|
||||
|
||||
/**
|
||||
* Database type. This value is required.
|
||||
*/
|
||||
type?: DatabaseType;
|
||||
|
||||
/**
|
||||
* Connection url to where perform connection to.
|
||||
*/
|
||||
url?: string;
|
||||
|
||||
/**
|
||||
* Database host.
|
||||
*/
|
||||
host?: string;
|
||||
|
||||
/**
|
||||
* Database host port.
|
||||
*/
|
||||
port?: number;
|
||||
|
||||
/**
|
||||
* Database username.
|
||||
*/
|
||||
username?: string;
|
||||
|
||||
/**
|
||||
* Database password.
|
||||
*/
|
||||
password?: string;
|
||||
|
||||
/**
|
||||
* Database name to connect to.
|
||||
* Storage type or path to the database (used for SQLite databases).
|
||||
*/
|
||||
database?: string;
|
||||
|
||||
/**
|
||||
* Schema name. By default is "public" (used only in Postgres databases).
|
||||
*/
|
||||
schemaName?: string;
|
||||
|
||||
/**
|
||||
* Connection SID (used for Oracle databases).
|
||||
*/
|
||||
sid?: string;
|
||||
|
||||
/**
|
||||
* Extra connection options to be passed to the underlying driver.
|
||||
*/
|
||||
extra?: any;
|
||||
|
||||
/**
|
||||
* Prefix to use on all tables (collections) of this connection in the database.
|
||||
*
|
||||
* @todo: rename to entityPrefix
|
||||
*/
|
||||
tablesPrefix?: string;
|
||||
|
||||
}
|
||||
@ -1,5 +1,3 @@
|
||||
import {DriverOptions} from "./DriverOptions";
|
||||
|
||||
/**
|
||||
* Common driver utility functions.
|
||||
*/
|
||||
@ -13,11 +11,11 @@ export class DriverUtils {
|
||||
* Normalizes and builds a new driver options.
|
||||
* Extracts settings from connection url and sets to a new options object.
|
||||
*/
|
||||
static buildDriverOptions(options: DriverOptions, buildOptions?: { useSid: boolean }): DriverOptions {
|
||||
static buildDriverOptions(options: any, buildOptions?: { useSid: boolean }): any {
|
||||
if (options.url) {
|
||||
const parsedUrl = this.parseConnectionUrl(options.url);
|
||||
if (buildOptions && buildOptions.useSid) {
|
||||
const urlDriverOptions: DriverOptions = {
|
||||
const urlDriverOptions: any = {
|
||||
type: options.type,
|
||||
host: parsedUrl.host,
|
||||
username: parsedUrl.username,
|
||||
@ -28,7 +26,7 @@ export class DriverUtils {
|
||||
return Object.assign(urlDriverOptions, options);
|
||||
|
||||
} else {
|
||||
const urlDriverOptions: DriverOptions = {
|
||||
const urlDriverOptions: any = {
|
||||
type: options.type,
|
||||
host: parsedUrl.host,
|
||||
username: parsedUrl.username,
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
import {Driver} from "../Driver";
|
||||
import {ConnectionIsNotSetError} from "../../error/ConnectionIsNotSetError";
|
||||
import {DriverOptions} from "../DriverOptions";
|
||||
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
|
||||
import {MongoQueryRunner} from "./MongoQueryRunner";
|
||||
import {ObjectLiteral} from "../../common/ObjectLiteral";
|
||||
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
|
||||
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
|
||||
import {PlatformTools} from "../../platform/PlatformTools";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
import {MongoConnectionOptions} from "./MongoConnectionOptions";
|
||||
@ -14,6 +12,7 @@ import {ColumnType} from "../types/ColumnTypes";
|
||||
import {MongoSchemaBuilder} from "../../schema-builder/MongoSchemaBuilder";
|
||||
import {DataTypeDefaults} from "../types/DataTypeDefaults";
|
||||
import {ColumnSchema} from "../../schema-builder/schema/ColumnSchema";
|
||||
import {ConnectionOptions} from "../../connection/ConnectionOptions";
|
||||
|
||||
/**
|
||||
* Organizes communication with MongoDB.
|
||||
@ -192,11 +191,11 @@ export class MongoDriver implements Driver {
|
||||
/**
|
||||
* Validate driver options to make sure everything is correct and driver will be able to establish connection.
|
||||
*/
|
||||
protected validateOptions(options: DriverOptions) {
|
||||
if (!options.url) {
|
||||
if (!options.database)
|
||||
throw new DriverOptionNotSetError("database");
|
||||
}
|
||||
protected validateOptions(options: ConnectionOptions) { // todo: fix
|
||||
// if (!options.url) {
|
||||
// if (!options.database)
|
||||
// throw new DriverOptionNotSetError("database");
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -48,6 +48,11 @@ export interface OracleConnectionOptions extends BaseConnectionOptions {
|
||||
/**
|
||||
* Schema name. By default is "public".
|
||||
*/
|
||||
readonly schema?: string;
|
||||
|
||||
/**
|
||||
* @deprecated use "schema" instead.
|
||||
*/
|
||||
readonly schemaName?: string;
|
||||
|
||||
}
|
||||
@ -715,7 +715,7 @@ AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDE
|
||||
* Database name shortcut.
|
||||
*/
|
||||
protected get dbName(): string {
|
||||
return this.driver.options.schemaName as string;
|
||||
return (this.driver.options.schema || this.driver.options.schemaName) as string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -43,6 +43,11 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions {
|
||||
/**
|
||||
* Schema name. By default is "public".
|
||||
*/
|
||||
readonly schema?: string;
|
||||
|
||||
/**
|
||||
* @deprecated use "schema" instead
|
||||
*/
|
||||
readonly schemaName?: string;
|
||||
|
||||
}
|
||||
@ -777,7 +777,7 @@ where constraint_type = 'PRIMARY KEY' AND c.table_schema = '${this.schemaName}'
|
||||
* Schema name shortcut.
|
||||
*/
|
||||
protected get schemaName() {
|
||||
return this.driver.options.schemaName || "public";
|
||||
return this.driver.options.schema || this.driver.options.schemaName || "public";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
19
src/index.ts
19
src/index.ts
@ -70,6 +70,10 @@ export * from "./decorator/Index";
|
||||
export * from "./decorator/Embedded";
|
||||
export * from "./decorator/DiscriminatorValue";
|
||||
export * from "./decorator/EntityRepository";
|
||||
export * from "./logger/Logger";
|
||||
export * from "./logger/AdvancedConsoleLogger";
|
||||
export * from "./logger/SimpleConsoleLogger";
|
||||
export * from "./logger/FileLogger";
|
||||
export * from "./schema-builder/schema/ColumnSchema";
|
||||
export * from "./schema-builder/schema/ForeignKeySchema";
|
||||
export * from "./schema-builder/schema/IndexSchema";
|
||||
@ -82,7 +86,6 @@ export {ConnectionOptionsReader} from "./connection/ConnectionOptionsReader";
|
||||
export {Connection} from "./connection/Connection";
|
||||
export {ConnectionManager} from "./connection/ConnectionManager";
|
||||
export {ConnectionOptions} from "./connection/ConnectionOptions";
|
||||
export {DriverOptions} from "./driver/DriverOptions";
|
||||
export {Driver} from "./driver/Driver";
|
||||
export {QueryBuilder} from "./query-builder/QueryBuilder";
|
||||
export {SelectQueryBuilder} from "./query-builder/SelectQueryBuilder";
|
||||
@ -134,13 +137,6 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
|
||||
return globalScope.typeormMetadataArgsStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a ConnectionManager which creates connections.
|
||||
*/
|
||||
export function getConnectionManager(): ConnectionManager {
|
||||
return getFromContainer(ConnectionManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads connection options stored in ormconfig configuration file.
|
||||
*/
|
||||
@ -148,6 +144,13 @@ export async function getConnectionOptions(connectionName: string = "default"):
|
||||
return new ConnectionOptionsReader().get(connectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a ConnectionManager which creates connections.
|
||||
*/
|
||||
export function getConnectionManager(): ConnectionManager {
|
||||
return getFromContainer(ConnectionManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connection and registers it in the manager.
|
||||
*
|
||||
|
||||
@ -343,7 +343,7 @@ export class EntityMetadata {
|
||||
args: TableMetadataArgs
|
||||
}) {
|
||||
const namingStrategy = options.connection.namingStrategy;
|
||||
const tablesPrefix = options.connection.options.tablesPrefix;
|
||||
const entityPrefix = options.connection.options.entityPrefix || options.connection.options.tablesPrefix;
|
||||
this.lazyRelationsWrapper = new LazyRelationsWrapper(options.connection);
|
||||
this.parentClosureEntityMetadata = options.parentClosureEntityMetadata!;
|
||||
this.target = options.args.target;
|
||||
@ -353,7 +353,7 @@ export class EntityMetadata {
|
||||
this.skipSync = options.args.skipSync || false;
|
||||
this.targetName = options.args.target instanceof Function ? (options.args.target as any).name : options.args.target;
|
||||
this.tableNameWithoutPrefix = this.tableType === "closure-junction" ? namingStrategy.closureJunctionTableName(this.givenTableName!) : namingStrategy.tableName(this.targetName, this.givenTableName);
|
||||
this.tableName = tablesPrefix ? namingStrategy.prefixTableName(tablesPrefix, this.tableNameWithoutPrefix) : this.tableNameWithoutPrefix;
|
||||
this.tableName = entityPrefix ? namingStrategy.prefixTableName(entityPrefix, this.tableNameWithoutPrefix) : this.tableNameWithoutPrefix;
|
||||
this.target = this.target ? this.target : this.tableName;
|
||||
this.name = this.targetName ? this.targetName : this.tableName;
|
||||
|
||||
|
||||
@ -83,7 +83,7 @@ class LoadMap {
|
||||
*/
|
||||
export class PlainObjectToDatabaseEntityTransformer {
|
||||
|
||||
constructor(private entityManager: EntityManager) {
|
||||
constructor(private manager: EntityManager) {
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -110,7 +110,7 @@ export class PlainObjectToDatabaseEntityTransformer {
|
||||
fillLoadMap(plainObject, metadata);
|
||||
// load all entities and store them in the load map
|
||||
await Promise.all(loadMap.groupByTargetIds().map(targetWithIds => { // todo: fix type hinting
|
||||
return this.entityManager
|
||||
return this.manager
|
||||
.findByIds<ObjectLiteral>(targetWithIds.target as any, targetWithIds.ids)
|
||||
.then(entities => loadMap.fillEntities(targetWithIds.target, entities));
|
||||
}));
|
||||
|
||||
@ -9,7 +9,7 @@ describe.skip("benchmark > bulk-save", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -10,7 +10,7 @@ describe.skip("cascades > should insert by cascades from both sides (#57)", () =
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true
|
||||
dropSchema: true
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -112,7 +112,7 @@ describe("ConnectionManager", () => {
|
||||
|
||||
describe("create connection options", function() {
|
||||
|
||||
it("should not drop the database if dropSchemaOnConnection was not specified", async () => {
|
||||
it("should not drop the database if dropSchema was not specified", async () => {
|
||||
const options: ConnectionOptions = setupSingleTestingConnection("mysql", {
|
||||
name: "myMysqlConnection",
|
||||
schemaCreate: true,
|
||||
@ -134,11 +134,11 @@ describe("ConnectionManager", () => {
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
it("should drop the database if dropSchemaOnConnection was set to true (mysql)", async () => {
|
||||
it("should drop the database if dropSchema was set to true (mysql)", async () => {
|
||||
const options: ConnectionOptions = setupSingleTestingConnection("mysql", {
|
||||
name: "myMysqlConnection",
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
entities: [Post]
|
||||
});
|
||||
const connectionManager = new ConnectionManager();
|
||||
@ -156,9 +156,9 @@ describe("ConnectionManager", () => {
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
/* it("should drop the database if dropSchemaOnConnection was set to true (postgres)", async () => {
|
||||
/* it("should drop the database if dropSchema was set to true (postgres)", async () => {
|
||||
const options: ConnectionOptions = {
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
autoSchemaSync: true,
|
||||
driver: createTestingConnectionOptions("postgres"),
|
||||
entities: [Post]
|
||||
@ -179,9 +179,9 @@ describe("ConnectionManager", () => {
|
||||
await connection.close();
|
||||
});*/
|
||||
|
||||
/* it("should drop the database if dropSchemaOnConnection was set to true (postgres)", async () => {
|
||||
/* it("should drop the database if dropSchema was set to true (postgres)", async () => {
|
||||
const options: ConnectionOptions = {
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
autoSchemaSync: true,
|
||||
driver: createTestingConnectionOptions("postgres"),
|
||||
entities: [Post]
|
||||
|
||||
@ -65,7 +65,7 @@ describe("Connection", () => {
|
||||
});
|
||||
|
||||
it("should not be able to sync a schema", () => {
|
||||
return connection.syncSchema().should.be.rejected; // CannotCloseNotConnectedError
|
||||
return connection.synchronize().should.be.rejected; // CannotCloseNotConnectedError
|
||||
});
|
||||
|
||||
it.skip("should not be able to use repositories", () => {
|
||||
@ -92,7 +92,7 @@ describe("Connection", () => {
|
||||
database: "test",
|
||||
entities: [],
|
||||
entitySchemas: [],
|
||||
dropSchemaOnConnection: false,
|
||||
dropSchema: false,
|
||||
schemaCreate: false,
|
||||
enabledDrivers: ["mysql"],
|
||||
});
|
||||
@ -103,7 +103,7 @@ describe("Connection", () => {
|
||||
describe("after connection is established successfully", function() {
|
||||
|
||||
let connections: Connection[];
|
||||
beforeEach(() => createTestingConnections({ entities: [Post, Category], schemaCreate: true, dropSchemaOnConnection: true }).then(all => connections = all));
|
||||
beforeEach(() => createTestingConnections({ entities: [Post, Category], schemaCreate: true, dropSchema: true }).then(all => connections = all));
|
||||
afterEach(() => closeTestingConnections(connections));
|
||||
|
||||
it("connection.isConnected should be true", () => connections.forEach(connection => {
|
||||
@ -128,7 +128,7 @@ describe("Connection", () => {
|
||||
describe("working with repositories after connection is established successfully", function() {
|
||||
|
||||
let connections: Connection[];
|
||||
before(() => createTestingConnections({ entities: [Post, Category], schemaCreate: true, dropSchemaOnConnection: true }).then(all => connections = all));
|
||||
before(() => createTestingConnections({ entities: [Post, Category], schemaCreate: true, dropSchema: true }).then(all => connections = all));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should be able to get simple entity repository", () => connections.forEach(connection => {
|
||||
@ -170,7 +170,7 @@ describe("Connection", () => {
|
||||
describe("generate a schema when connection.syncSchema is called", function() {
|
||||
|
||||
let connections: Connection[];
|
||||
before(() => createTestingConnections({ entities: [Post], schemaCreate: true, dropSchemaOnConnection: true }).then(all => connections = all));
|
||||
before(() => createTestingConnections({ entities: [Post], schemaCreate: true, dropSchema: true }).then(all => connections = all));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("database should be empty after schema is synced with dropDatabase flag", () => Promise.all(connections.map(async connection => {
|
||||
@ -180,7 +180,7 @@ describe("Connection", () => {
|
||||
await postRepository.save(post);
|
||||
const loadedPost = await postRepository.findOneById(post.id);
|
||||
expect(loadedPost).to.be.eql(post);
|
||||
await connection.syncSchema(true);
|
||||
await connection.synchronize(true);
|
||||
const againLoadedPost = await postRepository.findOneById(post.id);
|
||||
expect(againLoadedPost).to.be.empty;
|
||||
})));
|
||||
@ -196,7 +196,7 @@ describe("Connection", () => {
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should return sql log properly", () => Promise.all(connections.map(async connection => {
|
||||
await connection.logSyncSchema();
|
||||
await connection.driver.createSchemaBuilder().log();
|
||||
// console.log(sql);
|
||||
})));
|
||||
|
||||
@ -206,7 +206,7 @@ describe("Connection", () => {
|
||||
|
||||
// open a close connections
|
||||
let connections: Connection[] = [];
|
||||
before(() => createTestingConnections({ entities: [Post], schemaCreate: true, dropSchemaOnConnection: true }).then(all => {
|
||||
before(() => createTestingConnections({ entities: [Post], schemaCreate: true, dropSchema: true }).then(all => {
|
||||
connections = all;
|
||||
return Promise.all(connections.map(connection => connection.close()));
|
||||
}));
|
||||
@ -224,10 +224,10 @@ describe("Connection", () => {
|
||||
describe("skip schema generation when skipSync option is used", function() {
|
||||
|
||||
let connections: Connection[];
|
||||
beforeEach(() => createTestingConnections({ entities: [View], dropSchemaOnConnection: true }).then(all => connections = all));
|
||||
beforeEach(() => createTestingConnections({ entities: [View], dropSchema: true }).then(all => connections = all));
|
||||
afterEach(() => closeTestingConnections(connections));
|
||||
it("database should be empty after schema sync", () => Promise.all(connections.map(async connection => {
|
||||
await connection.syncSchema(true);
|
||||
await connection.synchronize(true);
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
let schema = await queryRunner.loadTableSchemas(["view"]);
|
||||
await queryRunner.release();
|
||||
@ -244,36 +244,36 @@ describe("Connection", () => {
|
||||
name: "test",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV1, GuestV1],
|
||||
schemaName: "test-schema",
|
||||
dropSchemaOnConnection: true,
|
||||
schema: "test-schema",
|
||||
dropSchema: true,
|
||||
});
|
||||
const connections2 = await createTestingConnections({
|
||||
name: "another",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV1, GuestV1],
|
||||
schemaName: "another-schema",
|
||||
dropSchemaOnConnection: true
|
||||
schema: "another-schema",
|
||||
dropSchema: true
|
||||
});
|
||||
connections = [...connections1, ...connections2];
|
||||
});
|
||||
after(() => closeTestingConnections(connections));
|
||||
it("should not interfere with each other", async () => {
|
||||
await Promise.all(connections.map(c => c.syncSchema()));
|
||||
await Promise.all(connections.map(c => c.synchronize()));
|
||||
await closeTestingConnections(connections);
|
||||
const connections1 = await createTestingConnections({
|
||||
name: "test",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV2, GuestV2],
|
||||
schemaName: "test-schema",
|
||||
dropSchemaOnConnection: false,
|
||||
schema: "test-schema",
|
||||
dropSchema: false,
|
||||
schemaCreate: true
|
||||
});
|
||||
const connections2 = await createTestingConnections({
|
||||
name: "another",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV2, GuestV2],
|
||||
schemaName: "another-schema",
|
||||
dropSchemaOnConnection: false,
|
||||
schema: "another-schema",
|
||||
dropSchema: false,
|
||||
schemaCreate: true
|
||||
});
|
||||
connections = [...connections1, ...connections2];
|
||||
@ -287,15 +287,15 @@ describe("Connection", () => {
|
||||
name: "test",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV1, GuestV1],
|
||||
schemaName: "test-schema",
|
||||
dropSchemaOnConnection: true,
|
||||
schema: "test-schema",
|
||||
dropSchema: true,
|
||||
});
|
||||
const connections2 = await createTestingConnections({
|
||||
name: "another",
|
||||
enabledDrivers: ["postgres"],
|
||||
entities: [CommentV1, GuestV1],
|
||||
schemaName: "another-schema",
|
||||
dropSchemaOnConnection: true
|
||||
schema: "another-schema",
|
||||
dropSchema: true
|
||||
});
|
||||
connections = [...connections1, ...connections2];
|
||||
});
|
||||
@ -303,8 +303,8 @@ describe("Connection", () => {
|
||||
|
||||
it("schema name can be set", () => {
|
||||
return Promise.all(connections.map(async connection => {
|
||||
await connection.syncSchema(true);
|
||||
const schemaName = (connection.options as PostgresConnectionOptions).schemaName;
|
||||
await connection.synchronize(true);
|
||||
const schemaName = (connection.options as PostgresConnectionOptions).schema;
|
||||
const comment = new CommentV1();
|
||||
comment.title = "Change SchemaName";
|
||||
comment.context = `To ${schemaName}`;
|
||||
|
||||
@ -14,7 +14,7 @@ describe("database schema > column types > mssql", () => {
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["mssql"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
});
|
||||
});
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -14,7 +14,7 @@ describe("database schema > column types > mysql", () => {
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["mysql"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
});
|
||||
});
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("database schema > column types > postgres", () => {
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["postgres"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
});
|
||||
});
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("database schema > column types > sqlite", () => {
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["sqlite"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
});
|
||||
});
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("database schema > mssql-parameters", () => {
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["mssql"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
});
|
||||
});
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("decorators > relation-count-decorator > one-to-many", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("decorators > relation-id-decorator > many-to-many", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("decorators > relation-id-decorator > many-to-one", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("decorators > relation-id > one-to-many", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("decorators > relation-id > one-to-one", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -10,7 +10,7 @@ describe("driver > convert raw results to entity", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [Post],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true
|
||||
dropSchema: true
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("embedded > basic functionality", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-many-case1", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-many-case2", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-many-case3", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-many-case4", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-many-case5", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-one-case1", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-one-case2", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-one-case3", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-one-case4", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-many-to-one-case5", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("embedded > embedded-one-to-one", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -17,7 +17,7 @@ describe("embedded > embedded-with-special-columns", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("embedded > multiple-primary-columns-with-nested-embed", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("embedded > multiple-primary-column", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -11,7 +11,7 @@ describe("embedded > outer-primary-column", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -13,7 +13,7 @@ describe("entity-metadata > property-map", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -10,7 +10,7 @@ describe("entity-model", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -9,7 +9,7 @@ describe("indices > basic unique index test", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -15,7 +15,7 @@ describe("jsonb type", () => {
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should make correct schema with Postgres' jsonb type", () => Promise.all(connections.map(async connection => {
|
||||
await connection.syncSchema(true);
|
||||
await connection.synchronize(true);
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
let schema = await queryRunner.loadTableSchema("record");
|
||||
await queryRunner.release();
|
||||
@ -25,7 +25,7 @@ describe("jsonb type", () => {
|
||||
})));
|
||||
|
||||
it("should persist jsonb correctly", () => Promise.all(connections.map(async connection => {
|
||||
await connection.syncSchema(true);
|
||||
await connection.synchronize(true);
|
||||
let recordRepo = connection.getRepository(Record);
|
||||
let record = new Record();
|
||||
record.data = { foo: "bar" };
|
||||
|
||||
@ -26,7 +26,7 @@ describe("lazy-relations", () => {
|
||||
entities: [Post, Category],
|
||||
entitySchemas: [userSchema, profileSchema],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
enabledDrivers: ["mysql"] // we can properly test lazy-relations only on one platform
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
@ -12,7 +12,7 @@ describe("metadata-builder > ColumnMetadata", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -10,7 +10,7 @@ describe("persistence > cascade operations", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -10,7 +10,7 @@ describe("persistence > cascade operations with custom name", () => {
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
@ -21,7 +21,7 @@ describe("persistence > custom-column-names", function() {
|
||||
// clean up database before each test
|
||||
function reloadDatabase() {
|
||||
return connection
|
||||
.syncSchema(true)
|
||||
.synchronize(true)
|
||||
.catch(e => {
|
||||
console.log("Error during schema re-creation: ", e);
|
||||
throw e;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user