Introduction to TypeORM Entities with Product Entity in Nest JS

An entity is a class that maps to a database ‘table’ (or ‘collection’ in MongoDB). You can create an entity by defining a new class and mark it with @Entity() decorator. More information about TypeORM entities can be found here: https://typeorm.io/#/entities. Let’s create an Entity to understand it in a practical approach.

Creating Product Entity

First, in src folder, create a subfolder called models . In src/models , create a new file product.entity.ts , and fill it with the following code.

TypeORM entities consist of columns and relations. In the above code, we define a Product entity (which will become into a product table) with five attributes ( id , name , description , image , and price ). By specifying the @Column decorator, all these attributes will become columns of the product table.

TypeORM requires that each entity have a primary column (or ObjectId column if using MongoDB). In our case, the Product entity primary column is id . The @PrimaryGeneratedColumn() creates a primary column that automatically generates an auto-increment value.

We are storing our entities inside the src/models folder. We named that folder as models to keep it consistent with our MVC architecture. However, not all Nest applications store their models (or entities) inside a models folder. The official documentation recommends storing the models near their domain, in the corresponding ‘module’ directory. Currently, we only have one module (the AppModule ). Later, we will create four modules. Most of those modules will use most of the entities that we will define. Thus, we decided not to place the entities in specific a ‘module’ directory but under a “global” src/models folder.

Synchronizing the database

We have created a Product entity. Our TypeORM config file ( ormconfig.json ) contains an option called synchronize with a value of true . It also contains an option called entities that specifies the directories where TypeORM should search for entities. The value is dist/**/*.entity{.ts,.js} . The value is a regular expression which searches for all files (in the dist folder) that contain .entity in their filenames. Remember that the dist folder contains the compiled production version of the source code.

Why are the synchronize and entities options important? Because, once we start the server, Nest generates the dist folder, runs the code, and executes TypeORM. Since synchronize option is true , TypeORM searches for new entities or changes in those entities and automatically creates the corresponding tables based on those entities. Be careful to note that setting synchronize to true shouldn't be used in a production environment, otherwise you can mistakenly lose production data.

Let’s synchronize our database. First, stop the server. Then, In the Terminal, go to the project directory, and execute the following:

Execute in Terminal

npm run start:dev

Now, you can see the new product table in your online_store database 

Note: if you create a new entity or modify an existing one, you should repeat the previous process to apply the changes over the database (stop and start the server).

Inserting products

Let’s insert four products into our database. For now, we will insert it manually (through SQL queries). Later, we will insert products through a form in an upcoming chapter.

In phpMyAdmin, click the online_store database, click the SQL tab, paste the following SQL queries, and click go.

Let’s check that the products were successfully inserted. In phpMyAdmin, click the online_store database, and click the product table. Hopefully, you will see the four products inserted.


See Next Article Nest JS ...

Post a Comment for "Introduction to TypeORM Entities with Product Entity in Nest JS"