How to use the command "typeorm" (with examples)

How to use the command "typeorm" (with examples)

TypeORM is a JavaScript ORM (Object-Relational Mapping) library that allows developers to work with databases using object-oriented programming. It can be used in a variety of platforms including Node.js, browser, Cordova, Ionic, React Native, NativeScript, and Electron. This article will guide you through various use cases of the “typeorm” command.

Use case 1: Generate a new initial TypeORM project structure

Code:

typeorm init

Motivation:

This command generates a new initial TypeORM project structure. It is useful when you want to start a new project with TypeORM, as it scaffolds the basic directory structure and configuration files.

Explanation:

  • init: This argument tells TypeORM to initialize a new project.

Example output:

Project created inside "my-project" directory.

Use case 2: Create an empty migration file

Code:

typeorm migration:create --name migration_name

Motivation:

When working with databases, it is common to make changes to the schema over time. Migrations allow you to manage these changes and keep track of the database schema history. This command creates an empty migration file, which you can then fill with the necessary SQL statements to update the schema.

Explanation:

  • migration:create: This subcommand is used to create a migration file.
  • --name migration_name: This option specifies the name of the migration file. Replace “migration_name” with the desired name.

Example output:

Migration my-migration successfully created.

Use case 3: Create a migration file with the SQL statements to update the schema

Code:

typeorm migration:generate --name migration_name

Motivation:

Similar to the previous use case, this command also creates a migration file. However, in this case, it generates the SQL statements necessary to update the schema based on the changes you have made to your entities.

Explanation:

  • migration:generate: This subcommand generates a migration file based on the differences between the current entity definitions and the database schema.
  • --name migration_name: This option specifies the name of the migration file. Replace “migration_name” with the desired name.

Example output:

Migration my-migration successfully generated.

Use case 4: Run all pending migrations

Code:

typeorm migration:run

Motivation:

Running migrations is essential to keep the database schema updated with the latest changes made to the entities. This command applies all pending migrations to the database.

Explanation:

  • migration:run: This subcommand is used to run all pending migrations.

Example output:

Migration my-migration1 executed successfully.
Migration my-migration2 executed successfully.

Use case 5: Create a new entity file in a specific directory

Code:

typeorm entity:create --name entity --dir path/to/directory

Motivation:

Entities in TypeORM represent database tables or collections. This command creates a new entity file in the specified directory, which makes it easier to organize your entities.

Explanation:

  • entity:create: This subcommand is used to create a new entity file.
  • --name entity: This option specifies the name of the entity file. Replace “entity” with the desired name.
  • --dir path/to/directory: This option specifies the directory where the entity file should be created. Replace “path/to/directory” with the desired directory path.

Example output:

Entity EntityName successfully created in path/to/directory.

Use case 6: Display the SQL statements to be executed by “typeorm schema:sync” on the default connection

Code:

typeorm schema:log

Motivation:

The “schema:sync” command in TypeORM synchronizes the entities with the database schema, applying any necessary changes automatically. This command displays the SQL statements that would be executed by the “schema:sync” command, allowing you to inspect the changes before applying them.

Explanation:

  • schema:log: This subcommand displays the SQL statements that would be executed to synchronize the entities with the database schema.

Example output:

ALTER TABLE `table_name` ADD COLUMN `column_name` varchar(255) NOT NULL

Use case 7: Execute a specific SQL statement on the default connection

Code:

typeorm query sql_sentence

Motivation:

Sometimes you may need to execute custom SQL statements directly on the database. This command allows you to execute a specific SQL statement on the default TypeORM connection.

Explanation:

  • query: This subcommand is used to execute a specific SQL statement.
  • sql_sentence: This argument specifies the SQL statement to be executed. Replace “sql_sentence” with the desired SQL statement.

Example output:

1 row(s) affected.

Use case 8: Display help for a subcommand

Code:

typeorm subcommand --help

Motivation:

If you need information about a specific subcommand, this command provides detailed help and usage instructions.

Explanation:

  • subcommand: Replace this with the desired subcommand for which you want to display help.

Example output:

Usage: typeorm schema:log [options]

Options:
  -h, --help     display help for command

Conclusion

The “typeorm” command offers a wide range of functionality for working with databases using TypeORM. By understanding and utilizing its various subcommands and options, you can efficiently manage migrations, entities, database synchronization, and even execute custom SQL statements. Experiment with these examples to gain a deeper understanding of the capabilities of TypeORM.

Related Posts

How to use the command tlmgr key (with examples)

How to use the command tlmgr key (with examples)

The tlmgr key command is used to manage GPG keys that are used to verify TeX Live databases.

Read More
How to use the command 'getent' (with examples)

How to use the command 'getent' (with examples)

The getent command is used to retrieve entries from the Name Service Switch (NSS) libraries.

Read More
How to use the command snmpwalk (with examples)

How to use the command snmpwalk (with examples)

Snmpwalk is a command-line tool that is used for querying system information of a remote host using the SNMP (Simple Network Management Protocol).

Read More