typeorm
Manage TypeORM database entities and migrations
TLDR
Generate a new initial TypeORM project structure
Create an empty migration file
Create a migration file with the SQL statements to update the schema
Run all pending migrations
Create a new entity file in a specific directory
Display the SQL statements to be executed by typeorm schema:sync on the default connection
Execute a specific SQL statement on the default connection
Display help for a subcommand
SYNOPSIS
typeorm
PARAMETERS
help
Displays help information about the CLI or a specific command. Usually invoked with `typeorm help
version
Displays the current version of the TypeORM CLI.
migration:create -n
Creates a new migration file with the specified name.
migration:generate -n
Generates a new migration file based on the differences between the current database schema and the entities defined in your code. Requires a data source file.
migration:run -d
Runs all pending migrations against the database. Requires a data source file.
migration:revert -d
Reverts the last executed migration. Requires a data source file.
migration:show -d
Shows the status of all migrations (applied or pending). Requires a data source file.
schema:sync -d
Synchronizes the database schema with the entities defined in your code. Warning: This can potentially drop existing data! Use with caution. Requires a data source file.
entity:create -n
Creates a new entity file with the specified name.
subscriber:create -n
Creates a new subscriber file with the specified name.
-c, --config
Specifies the path to the TypeORM configuration file (deprecated, use data source)
-d, --dataSource
Specifies the path to the TypeORM data source file. This is the preferred way to configure TypeORM.
-l, --logging
Specifies the logging level (e.g., 'all', 'query', 'error').
DESCRIPTION
The TypeORM CLI is a command-line interface tool used to manage and interact with databases when using TypeORM in TypeScript projects. It allows developers to perform various database-related tasks, such as creating and running migrations, generating entities from database schemas, executing custom SQL queries, and more. This simplifies database management and schema evolution by providing a standardized and automated workflow. It is particularly useful for managing complex database structures and maintaining data integrity throughout the development lifecycle. It requires TypeORM to be installed as a project dependency. Configuration is typically handled using a `ormconfig.ts` or `ormconfig.js` file, or environment variables, allowing developers to define database connection details, migration directories, and other settings. The CLI eliminates the need for manual database scripting, streamlining the development process and reducing the risk of errors.
CAVEATS
The TypeORM CLI relies on a properly configured TypeORM project with a valid configuration file or data source. Incorrect configuration can lead to errors or data loss. Schema synchronization should be used with caution, especially in production environments, as it can potentially drop existing data if the database schema and entities are out of sync.
DATA SOURCE
A Data Source is a class that encapsulates the connection settings and metadata required for TypeORM to interact with a database. It replaced the configuration file as the main configuration source.
USAGE EXAMPLES
Creating a migration: typeorm migration:create -n MyNewMigration
Running migrations using data source:typeorm migration:run -d ./src/data-source.ts
Generating a migration using data source:typeorm migration:generate -n MyNewMigration -d ./src/data-source.ts
HISTORY
The TypeORM CLI was developed alongside the TypeORM library to provide a convenient way to manage database schemas and migrations in TypeScript projects. It has evolved with TypeORM, adding features and improvements to streamline database development and management. Originally it relied on a single configuration file but then added the option to use a Data Source, this became the new way to configure a typeorm project.