LinuxCommandLibrary

typeorm

Manage TypeORM database entities and migrations

TLDR

Generate a new initial TypeORM project structure

$ typeorm init
copy

Create an empty migration file
$ typeorm migration:create --name [migration_name]
copy

Create a migration file with the SQL statements to update the schema
$ typeorm migration:generate --name [migration_name]
copy

Run all pending migrations
$ typeorm migration:run
copy

Create a new entity file in a specific directory
$ typeorm entity:create --name [entity] --dir [path/to/directory]
copy

Display the SQL statements to be executed by typeorm schema:sync on the default connection
$ typeorm schema:log
copy

Execute a specific SQL statement on the default connection
$ typeorm query [sql_sentence]
copy

Display help for a subcommand
$ typeorm [subcommand] --help
copy

SYNOPSIS

typeorm <command> [options]

Where <command> is one of the available TypeORM CLI commands, and [options] are command-specific flags.

PARAMETERS

migration:run
    Executes all pending database migrations.

migration:generate
    Generates a new migration file based on the difference between the current entities and the database schema.

migration:revert
    Reverts the last executed migration.

migration:show
    Lists all executed and pending migrations.

entity:create
    Creates a new entity file with a specified name.

subscriber:create
    Creates a new subscriber file for database events.

schema:sync
    Synchronizes the database schema with defined entities. Use with caution, as it can cause data loss.

schema:drop
    Drops the database schema. Extremely destructive; use only for development or testing.

query
    Executes a raw SQL query directly on the database.

version
    Displays the installed TypeORM version.

init
    Initializes a new TypeORM project with basic configuration.

cache:clear
    Clears the TypeORM query cache.

DESCRIPTION

The typeorm command-line interface (CLI) is an essential tool for developers working with TypeORM, a powerful Object-Relational Mapper (ORM) for TypeScript and JavaScript. It simplifies common database operations, enabling developers to manage their application's data layer efficiently. Key functionalities include generating and running database migrations to evolve schema changes over time, creating new entity and subscriber files, and synchronizing the database schema with defined entities. It also provides utilities for executing raw SQL queries and managing the query cache. By abstracting complex database interactions, the typeorm CLI allows developers to focus on application logic, ensuring data consistency and simplifying collaborative development on database-driven projects. It integrates seamlessly into Node.js workflows, typically used within project directories configured with TypeORM settings.

CAVEATS

The typeorm command is not a native Linux utility; it requires Node.js and npm/yarn to be installed. It typically operates within a Node.js project directory that has TypeORM configured, usually via an ormconfig.json file or datasource.ts module. Commands like schema:sync and schema:drop can lead to irreversible data loss if not used carefully, especially in production environments. Ensure proper backups and environment-specific configurations.

<B>INSTALLATION</B>

The typeorm CLI is installed via npm or yarn. It can be installed globally (npm install -g typeorm) for general use or as a development dependency within a project (npm install --save-dev typeorm), typically invoked using npx typeorm or a script in package.json.

<B>CONFIGURATION</B>

The CLI relies on a configuration file, typically named ormconfig.json, ormconfig.js, ormconfig.ts, or a datasource.ts file. This file specifies database connection details, entity paths, migration paths, and other TypeORM settings. The --config and --connection options can be used to override default configuration files or specified connections.

HISTORY

TypeORM emerged as a prominent Object-Relational Mapper (ORM) in the Node.js ecosystem, gaining significant traction due to its strong support for TypeScript and modern JavaScript features. Developed as an open-source project, it quickly became a preferred choice for building robust and scalable data-driven applications. The typeorm CLI was developed to streamline the development workflow, automating repetitive database tasks such as schema management and migration generation, which are crucial for maintaining database integrity across various development stages and team collaborations. Its continuous development reflects evolving best practices in ORM and database management.

SEE ALSO

npm(1), npx(1), node(1), mysql(1), psql(1)

Copied to clipboard