使用 CLI
安装 CLI
如果实体文件是 javascript
如果你本地已经装了 typeorm,请确保它的版本与我们将要安装的全局版本一致。
你可以用 npm i -g typeorm 全局安装 typeorm。如果你不想安装它,也可以对每条命令都使用 npx typeorm <params>。
如果实体文件是 typescript
这个 CLI 工具是用 javascript 写的,运行在 node 上。如果你的实体文件是 typescript,在使用 CLI 之前需要先把它们转译成 javascript。如果你只用 javascript,可以跳过本节。
你可以在项目中配置 ts-node 来简化操作,如下:
安装 ts-node:
npm install ts-node --save-dev在 package.json 的 scripts 部分加上 typeorm 命令
"scripts": {
...
"typeorm": "typeorm-ts-node-commonjs"
}对 ESM 项目,请改为:
"scripts": {
...
"typeorm": "typeorm-ts-node-esm"
}如果你想加载更多模块,比如 module-alias,可以再加 --require my-module-supporting-register
之后你就可以这样运行命令:
npm run typeorm migration:run -- -d path-to-datasource-config如何阅读本文档?
为了减少文档的冗长,下面各节都使用全局安装的 typeorm CLI。根据你安装 CLI 的方式,你可以把命令开头的 typeorm 替换成 npx typeorm 或 npm run typeorm。
初始化一个新的 TypeORM 项目
你可以创建一个所有东西都已配置好的新项目:
typeorm init它会创建一个基础 TypeORM 项目所需的全部文件:
- .gitignore
- package.json
- README.md
- tsconfig.json
- src/entity/User.ts
- src/index.ts
然后你可以运行 npm install 安装所有依赖。之后,运行 npm start 即可启动应用。
所有文件都生成在当前目录。如果你想把它们生成到某个特定目录,可以使用 --name:
typeorm init --name my-project要指定你使用的具体数据库,可以使用 --database:
typeorm init --database mssql要生成一个 ESM 基础项目,可以使用 --module esm:
typeorm init --name my-project --module esm你也可以生成一个带 Express 的基础项目:
typeorm init --name my-project --express如果你使用 docker,可以这样生成 docker-compose.yml 文件:
typeorm init --dockertypeorm init 是搭建 TypeORM 项目最简单也最快的方式。
创建新实体
你可以用 CLI 创建新实体:
typeorm entity:create path-to-entity-dir/entity进一步了解实体。
创建新订阅者
你可以用 CLI 创建新订阅者:
typeorm subscriber:create path-to-subscriber-dir/subscriber进一步了解订阅者。
管理迁移
typeorm migration:create- 创建空迁移typeorm migration:generate- 通过比较实体与实际数据库结构来生成迁移typeorm migration:run- 执行所有迁移typeorm migration:revert- 回滚最近一次迁移typeorm migration:show- 列出所有迁移及其执行状态
进一步了解迁移。
同步数据库结构
要同步数据库结构,请使用:
typeorm schema:sync在生产环境中运行该命令请务必小心——如果使用不当,结构同步可能导致数据丢失。在生产环境执行之前,请先检查它将运行哪些 sql 查询。
只记录结构同步的查询而不实际执行
要查看 schema:sync 将会运行哪些 sql 查询,请使用:
typeorm schema:log删除数据库结构
要彻底删除数据库结构,请使用:
typeorm schema:drop -- -d path-to-datasource-config在生产环境中使用该命令请务必小心,因为它会把数据库中的数据彻底清除。
运行任意 SQL 查询
你可以直接在数据库中执行任何你想要的 SQL 查询:
typeorm query "SELECT * FROM USERS"清空缓存
如果你使用了 QueryBuilder 缓存,有时可能想清空缓存中存储的所有内容。可以用以下命令做到:
typeorm cache:clear查看版本
你可以运行以下命令查看已安装的 typeorm 版本(本地和全局):
typeorm versionInstalling CLI
If entities files are in javascript
If you have a local typeorm version, make sure it matches the global version we are going to install.
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.
If entities files are in typescript
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.
You may setup ts-node in your project to ease the operation as follows:
Install ts-node:
npm install ts-node --save-devAdd typeorm command under scripts section in package.json
"scripts": {
...
"typeorm": "typeorm-ts-node-commonjs"
}For ESM projects add this instead:
"scripts": {
...
"typeorm": "typeorm-ts-node-esm"
}If you want to load more modules like module-alias you can add more --require my-module-supporting-register
Then you may run the command like this:
npm run typeorm migration:run -- -d path-to-datasource-configHow to read the documentation?
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.
Initialize a new TypeORM project
You can create a new project with everything already setup:
typeorm initIt creates all files needed for a basic project with TypeORM:
- .gitignore
- package.json
- README.md
- tsconfig.json
- src/entity/User.ts
- src/index.ts
Then you can run npm install to install all dependencies. After that, you can run your application by running npm start.
All files are generated in the current directory. If you want to generate them in a special directory you can use --name:
typeorm init --name my-projectTo specify a specific database you use you can use --database:
typeorm init --database mssqlTo generate an ESM base project you can use --module esm:
typeorm init --name my-project --module esmYou can also generate a base project with Express:
typeorm init --name my-project --expressIf you are using docker you can generate a docker-compose.yml file using:
typeorm init --dockertypeorm init is the easiest and fastest way to setup a TypeORM project.
Create a new entity
You can create a new entity using CLI:
typeorm entity:create path-to-entity-dir/entityLearn more about entities.
Create a new subscriber
You can create a new subscriber using CLI:
typeorm subscriber:create path-to-subscriber-dir/subscriberLearn more about Subscribers.
Manage migrations
typeorm migration:create- create empty migrationtypeorm migration:generate- generate migration comparing entities with actual database schematypeorm migration:run- execute all migrationstypeorm migration:revert- revert last migrationtypeorm migration:show- list all migrations with their execution status
Learn more about Migrations.
Sync database schema
To synchronize a database schema use:
typeorm schema:syncBe 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.
Log sync database schema queries without actual running them
To check what sql queries schema:sync is going to run use:
typeorm schema:logDrop database schema
To completely drop a database schema use:
typeorm schema:drop -- -d path-to-datasource-configBe careful with this command on production since it completely removes data from your database.
Run any SQL query
You can execute any SQL query you want directly in the database using:
typeorm query "SELECT * FROM USERS"Clear cache
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:
typeorm cache:clearCheck version
You can check what typeorm version you have installed (both local and global) by running:
typeorm version