TypeORM - Working with CLI


Advertisements

This section explains about TypeORM CLI commands in detail.

Create TypeORM project

typeorm init is the easiest and fastest way to setup a TypeORM project. You can create a new project as,

typeorm init --name Demoproject --database mysql

After executing the command, you will get the following output on your screen −

Project created inside /Users/workspace/TypeORM/Demoproject directory.

Create an entity

To create a new entity using CLI as,

typeorm entity:create -n Person

Now, Person entity is created inside your project src directory.

Entity /Users/workspace/TypeORM/Demoproject/src/entity/Person.ts has been created successfully.

If you have a multi-module project structure with multiple entities in different directories, you can use the below command,

typeorm entity:create -n Person -d src/Person/entity

Create a new subscriber

To create a new subscriber using CLI as follows −

typeorm subscriber:create -n PersonSubscriber

You could see the following response −

Subscriber /path/to/TypeORM/Demoproject/src/subscriber/PersonSubscriber.ts has been created successfully.

Create migrations

You can create a new migration using CLI as mentioned below −

typeorm migration:create -n PersonMigration

The above command created a migration directory inside your project src. Migration files are stored inside it.

Migration /path/to/TypeORM/Demoproject/src/migration/1587395030750-PersonMigration.ts has been generated successfully.

Database schema

To synchronize a database schema, use the below command −

typeorm schema:sync

To completely drop a database schema, use the below command −

typeorm schema:drop

Sql queries

If you want to execute any sql queries, we can execute directly from here. For example, to display all the records of customers, use the below query −

typeorm query "select * from customers"

If you want to clear everything stored in the cache. You can do it using the following command −

typeorm cache:clear

Conclusion

TypeORM is an excellent open source ORM framework to create high quality and scalable applications from small scale applications to large scale enterprise applications with multiple databases.

Advertisements