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.
You may setup ts-node in your project to ease the operation as follows:
npm install ts-node --save-dev
Add typeorm command under scripts section in package.json
For ESM projects add this instead:
Then you may run the command like this:
npm run typeorm migration:run
If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:
npm run typeorm migration:generate -- -n migrationNameHere
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
typeormat the start of the command, by either
npm run typeorm.
You can create a new project with everything already setup:
It creates all files needed for a basic project with TypeORM:
Then you can run
npm installto install all dependencies. After that, you can run your application by running
All files are generated in the current directory. If you want to generate them in a special directory you can use
typeorm init --name my-project
To specify a specific database you use you can use
typeorm init --database mssql
To generate an ESM base project you can use
typeorm init --name my-project --module esm
You can also generate a base project with Express:
typeorm init --name my-project --express
If you are using docker you can generate a
typeorm init --docker
typeorm initis the easiest and fastest way to setup a TypeORM project.
You can create a new entity using CLI:
typeorm entity:create -n User
Useris an entity file and class name. Running the command will create a new empty entity in
entitiesDirof the project. To set up the
entitiesDirof the project you must add it in data source options:
Learn more about data source options. If you have a multi-module project structure with multiple entities in different directories you can provide the path to the CLI command where you want to generate an entity:
typeorm entity:create -n User -d src/user/entity
You can create a new subscriber using CLI:
typeorm subscriber:create -n UserSubscriber
UserSubscriberis a subscriber file and class name. Running the following command will create a new empty subscriber in the
subscribersDirof the project. To setup
subscribersDiryou must add it in data source options:
Learn more about data source options. If you have a multi-module project structure with multiple subscribers in different directories you can provide a path to the CLI command where you want to generate a subscriber:
typeorm subscriber:create -n UserSubscriber -d src/user/subscriber
You can create a new migration using CLI:
typeorm migration:create -n UserMigration
UserMigrationis a migration file and class name. Running the command will create a new empty migration in the
migrationsDirof the project. To setup
migrationsDiryou must add it in data source options:
Learn more about data source options. If you have a multi-module project structure with multiple migrations in different directories you can provide a path to the CLI command where you want to generate a migration:
typeorm migration:create -n UserMigration -d src/user/migration
Automatic migration generation creates a new migration file and writes all sql queries that must be executed to update the database.
If no there were no changes generated, the command will exit with code 1.
typeorm migration:generate path/to/Migration -d path/to/datasource
The rule of thumb is to generate a migration after each entity change. the -d argument value should specify the path where your DataSource instance is defined. You can specify the path and name of the migration with the first argument.
To execute all pending migrations use following command:
To revert the most recently executed migration use the following command:
To show all migrations and whether they've been run or not use following command:
[X] = Migration has been ran
[ ] = Migration is pending/unapplied
This command also returns an error code if there are unapplied migrations.
To synchronize a database schema use:
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.
To check what sql queries
schema:syncis going to run use:
To completely drop a database schema use:
Be careful with this command on production since it completely removes data from your database.
You can execute any SQL query you want directly in the database using:
typeorm query "SELECT * FROM USERS"
If you are using
QueryBuildercaching, sometimes you may want to clear everything stored in the cache. You can do it using the following command:
You can check what typeorm version you have installed (both local and global) by running: