# 使用 CLI

> 英文标题：Using CLI
> 官网原文：https://typeorm.io/docs/using-cli
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 67 块

---

<!-- EN -->
# Using CLI

<!-- ZH -->
# 使用 CLI

<!-- EN -->
## Installing CLI

<!-- ZH -->
## 安装 CLI

<!-- EN -->
### If entities files are in javascript

<!-- ZH -->
### 如果实体文件是 javascript

<!-- EN -->
If you have a local typeorm version, make sure it matches the global version we are going to install.

<!-- ZH -->
如果你本地已经装了 typeorm，请确保它的版本与我们将要安装的全局版本一致。

<!-- EN -->
You can install typeorm globally with `npm i -g typeorm`. You can also choose to use `npx typeorm <params>` for each command if you prefer not having to install it.

<!-- ZH -->
你可以用 `npm i -g typeorm` 全局安装 typeorm。如果你不想安装它，也可以对每条命令都使用 `npx typeorm <params>`。

<!-- EN -->
### If entities files are in typescript

<!-- ZH -->
### 如果实体文件是 typescript

<!-- EN -->
This CLI tool is written in javascript and to be run on node. If your entity files are in typescript, you will need to transpile them to javascript before using CLI. You may skip this section if you only use javascript.

<!-- ZH -->
这个 CLI 工具是用 javascript 写的，运行在 node 上。如果你的实体文件是 typescript，在使用 CLI 之前需要先把它们转译成 javascript。如果你只用 javascript，可以跳过本节。

<!-- EN -->
You may setup ts-node in your project to ease the operation as follows:

<!-- ZH -->
你可以在项目中配置 ts-node 来简化操作，如下：

<!-- EN -->
Install ts-node:

<!-- ZH -->
安装 ts-node：

<!-- EN -->
```shell
npm install ts-node --save-dev
```

<!-- ZH -->
```shell
npm install ts-node --save-dev
```

<!-- EN -->
Add typeorm command under scripts section in package.json

<!-- ZH -->
在 package.json 的 scripts 部分加上 typeorm 命令

<!-- EN -->
```json
"scripts": {
    ...
    "typeorm": "typeorm-ts-node-commonjs"
}
```

<!-- ZH -->
```json
"scripts": {
    ...
    "typeorm": "typeorm-ts-node-commonjs"
}
```

<!-- EN -->
For ESM projects add this instead:

<!-- ZH -->
对 ESM 项目，请改为：

<!-- EN -->
```json
"scripts": {
    ...
    "typeorm": "typeorm-ts-node-esm"
}
```

<!-- ZH -->
```json
"scripts": {
    ...
    "typeorm": "typeorm-ts-node-esm"
}
```

<!-- EN -->
If you want to load more modules like [module-alias](https://github.com/ilearnio/module-alias) you can add more `--require my-module-supporting-register`

<!-- ZH -->
如果你想加载更多模块，比如 [module-alias](https://github.com/ilearnio/module-alias)，可以再加 `--require my-module-supporting-register`

<!-- EN -->
Then you may run the command like this:

<!-- ZH -->
之后你就可以这样运行命令：

<!-- EN -->
```shell
npm run typeorm migration:run -- -d path-to-datasource-config
```

<!-- ZH -->
```shell
npm run typeorm migration:run -- -d path-to-datasource-config
```

<!-- EN -->
### How to read the documentation?

<!-- ZH -->
### 如何阅读本文档？

<!-- EN -->
To reduce verbosity of the documentation, the following sections are using a globally installed typeorm CLI. Depending on how you installed the CLI, you may replace `typeorm` at the start of the command, by either `npx typeorm` or `npm run typeorm`.

<!-- ZH -->
为了减少文档的冗长，下面各节都使用全局安装的 typeorm CLI。根据你安装 CLI 的方式，你可以把命令开头的 `typeorm` 替换成 `npx typeorm` 或 `npm run typeorm`。

<!-- EN -->
## Initialize a new TypeORM project

<!-- ZH -->
## 初始化一个新的 TypeORM 项目

<!-- EN -->
You can create a new project with everything already setup:

<!-- ZH -->
你可以创建一个所有东西都已配置好的新项目：

<!-- EN -->
```shell
typeorm init
```

<!-- ZH -->
```shell
typeorm init
```

<!-- EN -->
It creates all files needed for a basic project with TypeORM:

<!-- ZH -->
它会创建一个基础 TypeORM 项目所需的全部文件：

<!-- EN -->
* .gitignore
* package.json
* README.md
* tsconfig.json
* src/entity/User.ts
* src/index.ts

<!-- ZH -->
* .gitignore
* package.json
* README.md
* tsconfig.json
* src/entity/User.ts
* src/index.ts

<!-- EN -->
Then you can run `npm install` to install all dependencies. After that, you can run your application by running `npm start`.

<!-- ZH -->
然后你可以运行 `npm install` 安装所有依赖。之后，运行 `npm start` 即可启动应用。

<!-- EN -->
All files are generated in the current directory. If you want to generate them in a special directory you can use `--name`:

<!-- ZH -->
所有文件都生成在当前目录。如果你想把它们生成到某个特定目录，可以使用 `--name`：

<!-- EN -->
```shell
typeorm init --name my-project
```

<!-- ZH -->
```shell
typeorm init --name my-project
```

<!-- EN -->
To specify a specific database you use you can use `--database`:

<!-- ZH -->
要指定你使用的具体数据库，可以使用 `--database`：

<!-- EN -->
```shell
typeorm init --database mssql
```

<!-- ZH -->
```shell
typeorm init --database mssql
```

<!-- EN -->
To generate an ESM base project you can use `--module esm`:

<!-- ZH -->
要生成一个 ESM 基础项目，可以使用 `--module esm`：

<!-- EN -->
```shell
typeorm init --name my-project --module esm
```

<!-- ZH -->
```shell
typeorm init --name my-project --module esm
```

<!-- EN -->
You can also generate a base project with Express:

<!-- ZH -->
你也可以生成一个带 Express 的基础项目：

<!-- EN -->
```shell
typeorm init --name my-project --express
```

<!-- ZH -->
```shell
typeorm init --name my-project --express
```

<!-- EN -->
If you are using docker you can generate a `docker-compose.yml` file using:

<!-- ZH -->
如果你使用 docker，可以这样生成 `docker-compose.yml` 文件：

<!-- EN -->
```shell
typeorm init --docker
```

<!-- ZH -->
```shell
typeorm init --docker
```

<!-- EN -->
`typeorm init` is the easiest and fastest way to setup a TypeORM project.

<!-- ZH -->
`typeorm init` 是搭建 TypeORM 项目最简单也最快的方式。

<!-- EN -->
## Create a new entity

<!-- ZH -->
## 创建新实体

<!-- EN -->
You can create a new entity using CLI:

<!-- ZH -->
你可以用 CLI 创建新实体：

<!-- EN -->
```shell
typeorm entity:create path-to-entity-dir/entity
```

<!-- ZH -->
```shell
typeorm entity:create path-to-entity-dir/entity
```

<!-- EN -->
Learn more about [entities](https://typeorm.io/docs/entity/entities).

<!-- ZH -->
进一步了解[实体](https://typeorm.io/docs/entity/entities)。

<!-- EN -->
## Create a new subscriber

<!-- ZH -->
## 创建新订阅者

<!-- EN -->
You can create a new subscriber using CLI:

<!-- ZH -->
你可以用 CLI 创建新订阅者：

<!-- EN -->
```shell
typeorm subscriber:create path-to-subscriber-dir/subscriber
```

<!-- ZH -->
```shell
typeorm subscriber:create path-to-subscriber-dir/subscriber
```

<!-- EN -->
Learn more about [Subscribers](https://typeorm.io/docs/listeners-and-subscribers).

<!-- ZH -->
进一步了解[订阅者](https://typeorm.io/docs/listeners-and-subscribers)。

<!-- EN -->
## Manage migrations

<!-- ZH -->
## 管理迁移

<!-- EN -->
* `typeorm migration:create` - [create](https://typeorm.io/docs/migrations/creating) empty migration
* `typeorm migration:generate` - [generate](https://typeorm.io/docs/migrations/generating) migration comparing entities with actual database schema
* `typeorm migration:run` - [execute](https://typeorm.io/docs/migrations/executing) all migrations
* `typeorm migration:revert` - [revert](https://typeorm.io/docs/migrations/reverting) last migration
* `typeorm migration:show` - [list](https://typeorm.io/docs/migrations/status) all migrations with their execution status

<!-- ZH -->
* `typeorm migration:create` - [创建](https://typeorm.io/docs/migrations/creating)空迁移
* `typeorm migration:generate` - 通过比较实体与实际数据库结构来[生成](https://typeorm.io/docs/migrations/generating)迁移
* `typeorm migration:run` - [执行](https://typeorm.io/docs/migrations/executing)所有迁移
* `typeorm migration:revert` - [回滚](https://typeorm.io/docs/migrations/reverting)最近一次迁移
* `typeorm migration:show` - [列出](https://typeorm.io/docs/migrations/status)所有迁移及其执行状态

<!-- EN -->
Learn more about [Migrations](https://typeorm.io/docs/migrations/why).

<!-- ZH -->
进一步了解[迁移](https://typeorm.io/docs/migrations/why)。

<!-- EN -->
## Sync database schema

<!-- ZH -->
## 同步数据库结构

<!-- EN -->
To synchronize a database schema use:

<!-- ZH -->
要同步数据库结构，请使用：

<!-- EN -->
```shell
typeorm schema:sync
```

<!-- ZH -->
```shell
typeorm schema:sync
```

<!-- EN -->
Be careful running this command in production - schema sync may cause data loss if you don't use it wisely. Check which sql queries it will run before running on production.

<!-- ZH -->
在生产环境中运行该命令请务必小心——如果使用不当，结构同步可能导致数据丢失。在生产环境执行之前，请先检查它将运行哪些 sql 查询。

<!-- EN -->
## Log sync database schema queries without actual running them

<!-- ZH -->
## 只记录结构同步的查询而不实际执行

<!-- EN -->
To check what sql queries `schema:sync` is going to run use:

<!-- ZH -->
要查看 `schema:sync` 将会运行哪些 sql 查询，请使用：

<!-- EN -->
```shell
typeorm schema:log
```

<!-- ZH -->
```shell
typeorm schema:log
```

<!-- EN -->
## Drop database schema

<!-- ZH -->
## 删除数据库结构

<!-- EN -->
To completely drop a database schema use:

<!-- ZH -->
要彻底删除数据库结构，请使用：

<!-- EN -->
```shell
typeorm schema:drop -- -d path-to-datasource-config
```

<!-- ZH -->
```shell
typeorm schema:drop -- -d path-to-datasource-config
```

<!-- EN -->
Be careful with this command on production since it completely removes data from your database.

<!-- ZH -->
在生产环境中使用该命令请务必小心，因为它会把数据库中的数据彻底清除。

<!-- EN -->
## Run any SQL query

<!-- ZH -->
## 运行任意 SQL 查询

<!-- EN -->
You can execute any SQL query you want directly in the database using:

<!-- ZH -->
你可以直接在数据库中执行任何你想要的 SQL 查询：

<!-- EN -->
```shell
typeorm query "SELECT * FROM USERS"
```

<!-- ZH -->
```shell
typeorm query "SELECT * FROM USERS"
```

<!-- EN -->
## Clear cache

<!-- ZH -->
## 清空缓存

<!-- EN -->
If you are using `QueryBuilder` caching, sometimes you may want to clear everything stored in the cache. You can do it using the following command:

<!-- ZH -->
如果你使用了 `QueryBuilder` 缓存，有时可能想清空缓存中存储的所有内容。可以用以下命令做到：

<!-- EN -->
```shell
typeorm cache:clear
```

<!-- ZH -->
```shell
typeorm cache:clear
```

<!-- EN -->
## Check version

<!-- ZH -->
## 查看版本

<!-- EN -->
You can check what typeorm version you have installed (both local and global) by running:

<!-- ZH -->
你可以运行以下命令查看已安装的 typeorm 版本（本地和全局）：

<!-- EN -->
```shell
typeorm version
```

<!-- ZH -->
```shell
typeorm version
```
