mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix: drop xml & yml connection option support (#9930)
This commit is contained in:
parent
daf1b47a60
commit
7dac12c2b1
@ -56,7 +56,6 @@ TypeORM 的一些特性:
|
||||
- 监听者和订阅者(钩子)
|
||||
- 支持闭包表模式
|
||||
- 在模型或者分离的配置文件中声明模式
|
||||
- json / xml / yml / env 格式的连接配置
|
||||
- 支持 MySQL / MariaDB / Postgres / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js
|
||||
- 支持 MongoDB NoSQL 数据库
|
||||
- 可在 NodeJS / 浏览器 / Ionic / Cordova / React Native / Expo / Electron 平台上使用
|
||||
|
||||
@ -66,7 +66,6 @@ TypeORM is highly influenced by other ORMs, such as [Hibernate](http://hibernate
|
||||
- Listeners and subscribers (hooks).
|
||||
- Supports closure table pattern.
|
||||
- Schema declaration in models or separate configuration files.
|
||||
- Connection configuration in json / xml / yml / env formats.
|
||||
- Supports MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js.
|
||||
- Supports MongoDB NoSQL database.
|
||||
- Works in NodeJS / Browser / Ionic / Cordova / React Native / NativeScript / Expo / Electron platforms.
|
||||
|
||||
@ -37,7 +37,6 @@ TypeORM은 [Hibernate](http://hibernate.org/orm/), [Doctrine](http://www.doctrin
|
||||
- 리스너 및 구독자(hooks).
|
||||
- 클로저 테이블 패턴 지원.
|
||||
- 모델 또는 별도의 설정 파일에서 스키마 선언.
|
||||
- json / xml / yml / env 형식의 연결 구성.
|
||||
- MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js를 지원.
|
||||
- MongoDB NoSQL 데이터베이스 지원.
|
||||
- NodeJS / Browser / Ionic / Cordova / React Native / NativeScript / Expo / Electron 플랫폼에서 작동.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,10 @@
|
||||
# 数据库连接
|
||||
|
||||
* [连接](#connection)
|
||||
* [什么是`Connection`](#什么是`Connection`)
|
||||
* [创建新的连接](#创建新的连接)
|
||||
* [使用`ConnectionManager`](#使用`ConnectionManager`)
|
||||
* [使用连接](#使用连接)
|
||||
- [连接](#connection)
|
||||
- [什么是`Connection`](#什么是`Connection`)
|
||||
- [创建新的连接](#创建新的连接)
|
||||
- [使用`ConnectionManager`](#使用`ConnectionManager`)
|
||||
- [使用连接](#使用连接)
|
||||
|
||||
## 什么是`Connection`
|
||||
|
||||
@ -22,69 +22,69 @@ TypeORM 的`Connection`不会像看起来那样设置单个数据库连接,而
|
||||
`createConnection` 创建单个连接:
|
||||
|
||||
```typescript
|
||||
import { createConnection, Connection } from "typeorm";
|
||||
import { createConnection, Connection } from "typeorm"
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test"
|
||||
});
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
})
|
||||
```
|
||||
|
||||
只使用`url`和`type`也可以进行连接。
|
||||
|
||||
```js
|
||||
createConnection({
|
||||
type: "postgres",
|
||||
url: "postgres://test:test@localhost/test"
|
||||
});
|
||||
type: "postgres",
|
||||
url: "postgres://test:test@localhost/test",
|
||||
})
|
||||
```
|
||||
|
||||
`createConnections` 创建多个连接:
|
||||
|
||||
```typescript
|
||||
import { createConnections, Connection } from "typeorm";
|
||||
import { createConnections, Connection } from "typeorm"
|
||||
|
||||
const connections = 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"
|
||||
}
|
||||
]);
|
||||
{
|
||||
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",
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
这两种方式都根据你传递的连接选项创建`Connection`,并调用`connect`方法。另外你也可以在项目的根目录中创建一个`ormconfig.json`文件,`createConnection`和`createConnections`将自动从此文件中读取连接选项。项目的根目录与`node_modules`目录的级别相同。
|
||||
|
||||
```typescript
|
||||
import { createConnection, createConnections, Connection } from "typeorm";
|
||||
import { createConnection, createConnections, Connection } from "typeorm"
|
||||
|
||||
// createConnection将从ormconfig.json / ormconfig.js / ormconfig.yml / ormconfig.env / ormconfig.xml 文件或特殊环境变量中加载连接选项
|
||||
const connection: Connection = await createConnection();
|
||||
// createConnection将从ormconfig.json / ormconfig.js / ormconfig.env 文件或特殊环境变量中加载连接选项
|
||||
const connection: Connection = await createConnection()
|
||||
|
||||
// 你可以指定要创建的连接的名称
|
||||
// (如果省略名称,则将创建没有指定名称的连接)
|
||||
const secondConnection: Connection = await createConnection("test2-connection");
|
||||
const secondConnection: Connection = await createConnection("test2-connection")
|
||||
|
||||
// 如果调用createConnections而不是createConnection
|
||||
// 它将初始化并返回ormconfig文件中定义的所有连接
|
||||
const connections: Connection[] = await createConnections();
|
||||
const connections: Connection[] = await createConnections()
|
||||
```
|
||||
|
||||
不同的连接必须具有不同的名称。默认情况下,如果未指定连接名称,则为`default`。
|
||||
@ -93,13 +93,13 @@ const connections: Connection[] = await createConnections();
|
||||
创建连接后,你可以使用`getConnection`函数从应用程序中的任何位置使用它:
|
||||
|
||||
```typescript
|
||||
import { getConnection } from "typeorm";
|
||||
import { getConnection } from "typeorm"
|
||||
|
||||
// 可以在调用createConnection后使用并解析
|
||||
const connection = getConnection();
|
||||
const connection = getConnection()
|
||||
|
||||
// 如果你有多个连接,则可以按名称获取连接
|
||||
const secondConnection = getConnection("test2-connection");
|
||||
const secondConnection = getConnection("test2-connection")
|
||||
```
|
||||
|
||||
应避免额外创建 classes/services 来存储和管理连接。此功能已嵌入到 TypeORM 中 - 无需过度工程并创建无用的抽象。
|
||||
@ -109,35 +109,35 @@ const secondConnection = getConnection("test2-connection");
|
||||
你可以使用`ConnectionManager`类创建连接。例如:
|
||||
|
||||
```typescript
|
||||
import { getConnectionManager, ConnectionManager, Connection } from "typeorm";
|
||||
import { getConnectionManager, ConnectionManager, Connection } from "typeorm"
|
||||
|
||||
const connectionManager = getConnectionManager();
|
||||
const connectionManager = getConnectionManager()
|
||||
const connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test"
|
||||
});
|
||||
await connection.connect(); // 执行连接
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
})
|
||||
await connection.connect() // 执行连接
|
||||
```
|
||||
|
||||
这不是常规创建连接的方法,但它可能对某些用户有用。例如,想要创建连接并存储其实例,同时控制何时建立实际"connection"。你还可以创建和维护自己的`ConnectionManager`:
|
||||
|
||||
```typescript
|
||||
import { getConnectionManager, ConnectionManager, Connection } from "typeorm";
|
||||
import { getConnectionManager, ConnectionManager, Connection } from "typeorm"
|
||||
|
||||
const connectionManager = new ConnectionManager();
|
||||
const connectionManager = new ConnectionManager()
|
||||
const connection = connectionManager.create({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test"
|
||||
});
|
||||
await connection.connect(); // 执行连接
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
})
|
||||
await connection.connect() // 执行连接
|
||||
```
|
||||
|
||||
但请注意,使用该方式,你将无法再使用`getConnection()` - 你需要存储连接管理器实例,并使用`connectionManager.get`来获取所需的连接。
|
||||
@ -149,14 +149,14 @@ await connection.connect(); // 执行连接
|
||||
设置连接后,可以使用`getConnection`函数在应用程序的任何位置使用它:
|
||||
|
||||
```typescript
|
||||
import { getConnection } from "typeorm";
|
||||
import { User } from "../entity/User";
|
||||
import { getConnection } from "typeorm"
|
||||
import { User } from "../entity/User"
|
||||
|
||||
export class UserController {
|
||||
@Get("/users")
|
||||
getAll() {
|
||||
return getConnection().manager.find(User);
|
||||
}
|
||||
@Get("/users")
|
||||
getAll() {
|
||||
return getConnection().manager.find(User)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -168,18 +168,18 @@ export class UserController {
|
||||
但一般来说,你不要太多使用`Connection`。大多数情况下,你只需创建连接并使用`getRepository()`和`getManager()`来访问连接的管理器和存储库,而无需直接使用连接对象:
|
||||
|
||||
```typescript
|
||||
import { getManager, getRepository } from "typeorm";
|
||||
import { User } from "../entity/User";
|
||||
import { getManager, getRepository } from "typeorm"
|
||||
import { User } from "../entity/User"
|
||||
|
||||
export class UserController {
|
||||
@Get("/users")
|
||||
getAll() {
|
||||
return getManager().find(User);
|
||||
}
|
||||
@Get("/users")
|
||||
getAll() {
|
||||
return getManager().find(User)
|
||||
}
|
||||
|
||||
@Get("/users/:id")
|
||||
getAll(@Param("id") userId: number) {
|
||||
return getRepository(User).findOne(userId);
|
||||
}
|
||||
@Get("/users/:id")
|
||||
getAll(@Param("id") userId: number) {
|
||||
return getRepository(User).findOne(userId)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,38 +1,34 @@
|
||||
# 使用 ormconfig.json
|
||||
|
||||
* [从配置文件创建新连接](#从配置文件创建新连接)
|
||||
* [使用`ormconfig.json`](#使用`ormconfig.json`)
|
||||
* [使用`ormconfig.js`](#使用`ormconfig.js`)
|
||||
* [使用环境变量](#使用环境变量)
|
||||
* [使用`ormconfig.yml`](#使用`ormconfig.yml`)
|
||||
* [使用`ormconfig.xml`](#使用`ormconfig.xml`)
|
||||
* [覆盖ormconfig中定义的选项](#覆盖ormconfig中定义的选项)
|
||||
- [从配置文件创建新连接](#从配置文件创建新连接)
|
||||
- [使用`ormconfig.json`](#使用`ormconfig.json`)
|
||||
- [使用`ormconfig.js`](#使用`ormconfig.js`)
|
||||
- [使用环境变量](#使用环境变量)
|
||||
- [覆盖 ormconfig 中定义的选项](#覆盖ormconfig中定义的选项)
|
||||
|
||||
## 从配置文件创建新连接
|
||||
|
||||
大多数情况下,我们希望将连接选项存储在单独的配置文件中,因为此方式使管理变得更方便和容易。 TypeORM 支持多个配置源。你只需要在应用程序的根目录(`package.json`附近)中创建一个`ormconfig.[format]`文件存放连接配置,并在应用程序中调用`createConnection()`,而不传递任何参数配置:
|
||||
|
||||
```typescript
|
||||
import { createConnection } from "typeorm";
|
||||
import { createConnection } from "typeorm"
|
||||
|
||||
// createConnection方法会自动读取来自ormconfig文件或环境变量中的连接选项
|
||||
const connection = await createConnection();
|
||||
const connection = await createConnection()
|
||||
```
|
||||
|
||||
支持的 ormconfig 文件格式有:`.json`, `.js`, `.env`, `.yml` 和 `.xml`.
|
||||
|
||||
## 使用`ormconfig.json`
|
||||
|
||||
在项目根目录(`package.json`附近)中创建`ormconfig.json`,并包含以下内容:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "test",
|
||||
"password": "test",
|
||||
"database": "test"
|
||||
}
|
||||
```
|
||||
|
||||
@ -42,24 +38,24 @@ const connection = await createConnection();
|
||||
|
||||
```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"
|
||||
}
|
||||
{
|
||||
"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"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
@ -69,13 +65,13 @@ const connection = await createConnection();
|
||||
|
||||
```javascript
|
||||
module.exports = {
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test"
|
||||
};
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
}
|
||||
```
|
||||
|
||||
你可以参考[ConnectionOptions](./connection-options.md)来设置其他选项。
|
||||
@ -100,114 +96,65 @@ TYPEORM_ENTITIES = entity/.*js,modules/**/entity/.*js
|
||||
|
||||
可以设置的可用 env 变量列表:
|
||||
|
||||
- TYPEORM_CACHE
|
||||
- TYPEORM_CACHE_ALWAYS_ENABLED
|
||||
- TYPEORM_CACHE_DURATION
|
||||
- TYPEORM_CACHE_OPTIONS
|
||||
- TYPEORM_CONNECTION
|
||||
- TYPEORM_DATABASE
|
||||
- TYPEORM_DEBUG
|
||||
- TYPEORM_DRIVER_EXTRA
|
||||
- TYPEORM_DROP_SCHEMA
|
||||
- TYPEORM_ENTITIES
|
||||
- TYPEORM_ENTITIES_DIR
|
||||
- TYPEORM_ENTITY_PREFIX
|
||||
- TYPEORM_HOST
|
||||
- TYPEORM_LOGGER
|
||||
- TYPEORM_LOGGING
|
||||
- TYPEORM_MAX_QUERY_EXECUTION_TIME
|
||||
- TYPEORM_MIGRATIONS
|
||||
- TYPEORM_MIGRATIONS_DIR
|
||||
- TYPEORM_MIGRATIONS_RUN
|
||||
- TYPEORM_MIGRATIONS_TABLE_NAME
|
||||
- TYPEORM_METADATA_TABLE_NAME
|
||||
- TYPEORM_PASSWORD
|
||||
- TYPEORM_PORT
|
||||
- TYPEORM_SCHEMA
|
||||
- TYPEORM_SID
|
||||
- TYPEORM_SUBSCRIBERS
|
||||
- TYPEORM_SUBSCRIBERS_DIR
|
||||
- TYPEORM_SYNCHRONIZE
|
||||
- TYPEORM_URL
|
||||
- TYPEORM_USERNAME
|
||||
- TYPEORM_UUID_EXTENSION
|
||||
- TYPEORM_CACHE
|
||||
- TYPEORM_CACHE_ALWAYS_ENABLED
|
||||
- TYPEORM_CACHE_DURATION
|
||||
- TYPEORM_CACHE_OPTIONS
|
||||
- TYPEORM_CONNECTION
|
||||
- TYPEORM_DATABASE
|
||||
- TYPEORM_DEBUG
|
||||
- TYPEORM_DRIVER_EXTRA
|
||||
- TYPEORM_DROP_SCHEMA
|
||||
- TYPEORM_ENTITIES
|
||||
- TYPEORM_ENTITIES_DIR
|
||||
- TYPEORM_ENTITY_PREFIX
|
||||
- TYPEORM_HOST
|
||||
- TYPEORM_LOGGER
|
||||
- TYPEORM_LOGGING
|
||||
- TYPEORM_MAX_QUERY_EXECUTION_TIME
|
||||
- TYPEORM_MIGRATIONS
|
||||
- TYPEORM_MIGRATIONS_DIR
|
||||
- TYPEORM_MIGRATIONS_RUN
|
||||
- TYPEORM_MIGRATIONS_TABLE_NAME
|
||||
- TYPEORM_METADATA_TABLE_NAME
|
||||
- TYPEORM_PASSWORD
|
||||
- TYPEORM_PORT
|
||||
- TYPEORM_SCHEMA
|
||||
- TYPEORM_SID
|
||||
- TYPEORM_SUBSCRIBERS
|
||||
- TYPEORM_SUBSCRIBERS_DIR
|
||||
- TYPEORM_SYNCHRONIZE
|
||||
- TYPEORM_URL
|
||||
- TYPEORM_USERNAME
|
||||
- TYPEORM_UUID_EXTENSION
|
||||
|
||||
`ormconfig.env`只能在开发期间使用。在生产环境中,你可以在 ENVIRONMENT VARIABLES 中设置所有这些值。
|
||||
|
||||
你无法使用`env`文件或环境变量定义多个连接。如果你的应用需要有多个连接,请使用其他配置替代。
|
||||
|
||||
## 使用`ormconfig.yml`
|
||||
## Typeorm 使用哪个配置文件
|
||||
|
||||
在项目根目录(`package.json`附近)中创建`ormconfig.yml`,并包含以下内容:
|
||||
有时你可能希望使用不同格式的多个配置。 当调用`getConnectionOptions()`或尝试在没有连接选项的情况下使用`createConnection()`时,Typeorm 将尝试按以下顺序加载配置:
|
||||
|
||||
```yaml
|
||||
default: # 默认连接
|
||||
host: "localhost"
|
||||
port: 3306
|
||||
username: "test"
|
||||
password: "test"
|
||||
database: "test"
|
||||
|
||||
second-connection: # 其他连接
|
||||
host: "localhost"
|
||||
port: 3306
|
||||
username: "test"
|
||||
password: "test"
|
||||
database: "test2"
|
||||
```
|
||||
|
||||
你可以使用任一连接。
|
||||
|
||||
## 使用`ormconfig.xml`
|
||||
|
||||
在项目根目录(`package.json`附近)中创建`ormconfig.xml`,并包含以下内容:
|
||||
|
||||
```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>
|
||||
```
|
||||
|
||||
你可以使用任何可用的连接选项。
|
||||
|
||||
## Typeorm使用哪个配置文件
|
||||
|
||||
有时你可能希望使用不同格式的多个配置。 当调用`getConnectionOptions()`或尝试在没有连接选项的情况下使用`createConnection()`时,Typeorm将尝试按以下顺序加载配置:
|
||||
|
||||
1. 来自环境变量。 Typeorm将尝试使用dotEnv加载`.env`文件(如果存在)。 如果设置了环境变量`TYPEORM_CONNECTION`或`TYPEORM_URL`,Typeorm将使用此方法。
|
||||
1. 来自环境变量。 Typeorm 将尝试使用 dotEnv 加载`.env`文件(如果存在)。 如果设置了环境变量`TYPEORM_CONNECTION`或`TYPEORM_URL`,Typeorm 将使用此方法。
|
||||
2. 来自`ormconfig.env`。
|
||||
3. 从另一个`ormconfig.[format]`文件,按此顺序:`[js,ts,json,yml,yaml,xml]`。
|
||||
3. 从另一个`ormconfig.[format]`文件,按此顺序:`[js,ts,json]`。
|
||||
|
||||
注意,Typeorm将使用找到的第一个有效方法,而不会加载其他方法。 例如,如果在环境中找到配置,Typeorm将不会加载`ormconfig.[format]`文件。
|
||||
注意,Typeorm 将使用找到的第一个有效方法,而不会加载其他方法。 例如,如果在环境中找到配置,Typeorm 将不会加载`ormconfig.[format]`文件。
|
||||
|
||||
## 覆盖ormconfig中定义的选项
|
||||
## 覆盖 ormconfig 中定义的选项
|
||||
|
||||
有时你希望覆盖 ormconfig 文件中定义的值,或者可能会在配置中附加一些 TypeScript / JavaScript 逻辑。
|
||||
在这种情况下,你可以从 ormconfig 加载选项并构建`ConnectionOptions`,然后在将它们传递给`createConnection`函数之前,使用这些选项执行任何操作:
|
||||
|
||||
```typescript
|
||||
// 从ormconfig文件(或ENV变量)读取连接选项
|
||||
const connectionOptions = await getConnectionOptions();
|
||||
const connectionOptions = await getConnectionOptions()
|
||||
|
||||
// 使用connectionOptions做一些事情,
|
||||
// 例如,附加自定义命名策略或自定义记录器
|
||||
Object.assign(connectionOptions, { namingStrategy: new MyNamingStrategy() });
|
||||
Object.assign(connectionOptions, { namingStrategy: new MyNamingStrategy() })
|
||||
|
||||
// 使用覆盖后的连接选项创建连接
|
||||
const connection = await createConnection(connectionOptions);
|
||||
const connection = await createConnection(connectionOptions)
|
||||
```
|
||||
|
||||
22927
package-lock.json
generated
22927
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -95,7 +95,6 @@
|
||||
"@types/chai": "^4.3.4",
|
||||
"@types/chai-as-promised": "^7.1.5",
|
||||
"@types/debug": "^4.1.7",
|
||||
"@types/js-yaml": "^4.0.5",
|
||||
"@types/mkdirp": "^1.0.2",
|
||||
"@types/mocha": "^10.0.1",
|
||||
"@types/node": "^18.13.0",
|
||||
@ -103,7 +102,6 @@
|
||||
"@types/sinon": "^10.0.13",
|
||||
"@types/source-map-support": "^0.5.6",
|
||||
"@types/uuid": "^9.0.0",
|
||||
"@types/xml2js": "^0.4.11",
|
||||
"@types/yargs": "^17.0.22",
|
||||
"better-sqlite3": "^8.1.0",
|
||||
"chai": "^4.3.7",
|
||||
@ -223,13 +221,11 @@
|
||||
"debug": "^4.3.4",
|
||||
"dotenv": "^16.0.3",
|
||||
"glob": "^8.1.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
"mkdirp": "^2.1.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"sha.js": "^2.4.11",
|
||||
"tslib": "^2.5.0",
|
||||
"uuid": "^9.0.0",
|
||||
"xml2js": "^0.4.23",
|
||||
"yargs": "^17.6.2"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@ -3,15 +3,12 @@ import path from "path"
|
||||
import { DataSourceOptions } from "../data-source/DataSourceOptions"
|
||||
import { PlatformTools } from "../platform/PlatformTools"
|
||||
import { ConnectionOptionsEnvReader } from "./options-reader/ConnectionOptionsEnvReader"
|
||||
import { ConnectionOptionsYmlReader } from "./options-reader/ConnectionOptionsYmlReader"
|
||||
import { ConnectionOptionsXmlReader } from "./options-reader/ConnectionOptionsXmlReader"
|
||||
import { TypeORMError } from "../error"
|
||||
import { isAbsolute } from "../util/PathUtils"
|
||||
import { importOrRequireFile } from "../util/ImportUtils"
|
||||
|
||||
/**
|
||||
* Reads connection options from the ormconfig.
|
||||
* Can read from multiple file extensions including env, json, js, xml and yml.
|
||||
*/
|
||||
export class ConnectionOptionsReader {
|
||||
// -------------------------------------------------------------------------
|
||||
@ -106,9 +103,6 @@ export class ConnectionOptionsReader {
|
||||
"mts",
|
||||
"cts",
|
||||
"json",
|
||||
"yml",
|
||||
"yaml",
|
||||
"xml",
|
||||
]
|
||||
|
||||
// Detect if baseFilePath contains file extension
|
||||
@ -168,18 +162,6 @@ export class ConnectionOptionsReader {
|
||||
}
|
||||
} else if (foundFileFormat === "json") {
|
||||
connectionOptions = require(configFile)
|
||||
} else if (foundFileFormat === "yml") {
|
||||
connectionOptions = await new ConnectionOptionsYmlReader().read(
|
||||
configFile,
|
||||
)
|
||||
} else if (foundFileFormat === "yaml") {
|
||||
connectionOptions = await new ConnectionOptionsYmlReader().read(
|
||||
configFile,
|
||||
)
|
||||
} else if (foundFileFormat === "xml") {
|
||||
connectionOptions = await new ConnectionOptionsXmlReader().read(
|
||||
configFile,
|
||||
)
|
||||
}
|
||||
|
||||
// normalize and return connection options
|
||||
|
||||
@ -1,74 +0,0 @@
|
||||
import { parseString as xmlParser } from "xml2js"
|
||||
import { PlatformTools } from "../../platform/PlatformTools"
|
||||
import { DataSourceOptions } from "../../data-source/DataSourceOptions"
|
||||
|
||||
/**
|
||||
* Reads connection options defined in the xml file.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export class ConnectionOptionsXmlReader {
|
||||
// -------------------------------------------------------------------------
|
||||
// Public Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Reads connection options from given xml file.
|
||||
*/
|
||||
async read(path: string): Promise<DataSourceOptions[]> {
|
||||
const xml = await this.readXml(path)
|
||||
return (xml.connection as any[]).map((connection) => {
|
||||
return {
|
||||
name: connection.$.name,
|
||||
type: connection.$.type,
|
||||
url: connection.url ? connection.url[0] : undefined,
|
||||
host: connection.host ? connection.host[0] : undefined,
|
||||
port:
|
||||
connection.port && connection.port[0]
|
||||
? parseInt(connection.port[0])
|
||||
: undefined,
|
||||
username: connection.username
|
||||
? connection.username[0]
|
||||
: undefined,
|
||||
password: connection.password
|
||||
? connection.password[0]
|
||||
: undefined,
|
||||
database: connection.database
|
||||
? connection.database[0]
|
||||
: undefined,
|
||||
sid: connection.sid ? connection.sid[0] : undefined,
|
||||
extra: connection.extra ? connection.extra[0] : undefined,
|
||||
synchronize: connection.synchronize
|
||||
? connection.synchronize[0]
|
||||
: undefined,
|
||||
entities: connection.entities
|
||||
? connection.entities[0].entity
|
||||
: [],
|
||||
subscribers: connection.subscribers
|
||||
? connection.subscribers[0].entity
|
||||
: [],
|
||||
logging: connection.logging[0]
|
||||
? connection.logging[0].split(",")
|
||||
: undefined,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Protected Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Reads xml file contents and returns them in a promise.
|
||||
*/
|
||||
protected readXml(path: string): Promise<any> {
|
||||
const xmlOptions = { trim: true, explicitRoot: false }
|
||||
return new Promise((ok, fail) => {
|
||||
xmlParser(
|
||||
PlatformTools.readFileSync(path),
|
||||
xmlOptions,
|
||||
(err: any, result: any) => (err ? fail(err) : ok(result)),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
import ymlParser from "js-yaml"
|
||||
import { PlatformTools } from "../../platform/PlatformTools"
|
||||
import { DataSourceOptions } from "../../data-source/DataSourceOptions"
|
||||
|
||||
/**
|
||||
* Reads connection options defined in the yml file.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export class ConnectionOptionsYmlReader {
|
||||
// -------------------------------------------------------------------------
|
||||
// Public Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Reads connection options from given yml file.
|
||||
*/
|
||||
async read(path: string): Promise<DataSourceOptions[]> {
|
||||
const contentsBuffer = PlatformTools.readFileSync(path)
|
||||
const contents = contentsBuffer.toString()
|
||||
|
||||
const config = ymlParser.load(contents)
|
||||
|
||||
if (!config || typeof config !== "object" || config === null) {
|
||||
return []
|
||||
}
|
||||
|
||||
return Object.keys(config).map((connectionName) => {
|
||||
return Object.assign(
|
||||
{ name: connectionName },
|
||||
(config as any)[connectionName],
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -84,7 +84,7 @@ export async function createConnection(
|
||||
* Creates a new connection and registers it in the manager.
|
||||
*
|
||||
* If connection options were not specified, then it will try to create connection automatically,
|
||||
* based on content of ormconfig (json/js/yml/xml/env) file or environment variables.
|
||||
* based on content of ormconfig (json/js/env) file or environment variables.
|
||||
* Only one connection from ormconfig will be created (name "default" or connection without name).
|
||||
*
|
||||
* @deprecated
|
||||
@ -104,7 +104,7 @@ export async function createConnection(
|
||||
* Creates new connections and registers them in the manager.
|
||||
*
|
||||
* If connection options were not specified, then it will try to create connection automatically,
|
||||
* based on content of ormconfig (json/js/yml/xml/env) file or environment variables.
|
||||
* based on content of ormconfig (json/js/env) file or environment variables.
|
||||
* All connections from the ormconfig will be created.
|
||||
*
|
||||
* @deprecated
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { promises as fs, existsSync } from "fs"
|
||||
import { promises as fs } from "fs"
|
||||
import { expect } from "chai"
|
||||
import { DataSourceOptions } from "../../../src/data-source/DataSourceOptions"
|
||||
import { ConnectionOptionsReader } from "../../../src/connection/ConnectionOptionsReader"
|
||||
@ -16,16 +16,6 @@ async function createDotenvFiles() {
|
||||
)
|
||||
}
|
||||
|
||||
async function createYamlFiles() {
|
||||
if (!existsSync(path.join(__dirname, "configs/yaml"))) {
|
||||
await fs.mkdir(path.join(__dirname, "configs/yaml"))
|
||||
}
|
||||
await fs.writeFile(
|
||||
path.join(__dirname, "configs/yaml/test-yaml.yaml"),
|
||||
'- type: "sqlite"\n name: "file"\n database: "test-yaml"',
|
||||
)
|
||||
}
|
||||
|
||||
describe("ConnectionOptionsReader", () => {
|
||||
beforeEach(() => {
|
||||
delete process.env["TYPEORM_CONNECTION"]
|
||||
@ -131,16 +121,4 @@ describe("ConnectionOptionsReader", () => {
|
||||
expect(fileOptions.database).to.have.string("test-ormconfig-env")
|
||||
expect(process.env.TYPEORM_DATABASE).to.equal("test-ormconfig-env")
|
||||
})
|
||||
|
||||
it("properly loads config from yaml", async () => {
|
||||
await createYamlFiles()
|
||||
|
||||
const connectionOptionsReader = new ConnectionOptionsReader({
|
||||
root: path.join(__dirname, "configs/yaml"),
|
||||
configName: "test-yaml",
|
||||
})
|
||||
const fileOptions: DataSourceOptions =
|
||||
await connectionOptionsReader.get("file")
|
||||
expect(fileOptions.database).to.have.string("/test-yaml")
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user