TypeORM Entity & Column decorators

TypeORM Entity & Column decorators

We'll develop entities in this tutorial to represent our community database. We'll see how to use the Entity and Column decorators of TypeORM to decorate classes that map to SQL tables.

To define an entity, add the @Entity() decorator to the class declaration and the @Column (or equivalent) decorator to each field. TypeORM can generate the database table and columns thanks to these annotations, which provide it the data it needs.

Run the following command to create a folder called models/ in the /backend/src/ folder:

mkdir src/models

We'll create the following models or entities:

  • Profile
  • Post
  • Comment
  • Like

Create a src/models/Profile.ts file and then add the following entity:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; 

export class Profile { 

  @PrimaryGeneratedColumn() id: number; 
  @Column() name: string; 
  @Column("text", { nullable: true }) about: string; 
  @Column({ nullable: true }) avatar: string; 
  @Column({ nullable: true }) coverImage: string; 
  @Column({ default: 0 }) numberOfPosts: number; 

  @Column({ unique: true }) email: string; 
  @Column({ unique: true }) username: string; 
  @Column() password: string; 

The @Entity() declaration tells TypeORM to match the class to a database table, as was already mentioned. We put the entity in the models/ folder to make it simpler for TypeORM to find it. This was previously set up in the entities array of the ormconfig.json file.

We just adorn the class property that you want to turn into a column with the @Column decorator to create the appropriate database's table columns.

Depending on the database type, TypeORM will by default translate a string to a varchar-like type and an integer-like type for a number. By providing the necessary type to the column decorator, we can easily change the default type. It's important to note that we used the text type rather than the constrained varchar type for the bio column.

Next, create the src/models/Post.ts file and add the following entity:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; 

export class Post { 
  @PrimaryGeneratedColumn() id: number; 
  @Column("longtext") content: string; 
  @Column({ nullable: true }) image: string; 
  @Column({ default: 0 }) numberOfComments: number; 
  @Column({ default: 0 }) numberOfLikes: number; 

Next, create the src/models/Comment.ts file and add the following entity:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; 
export class Comment { 

  @PrimaryGeneratedColumn() id: number; 
  @Column("text") content: string; 

  @ManyToOne(type => Profile, profile => profile.comments, { onDelete: 'CASCADE' }) 
  author: Profile;   
  @ManyToOne(type => Post, post => post.comments, { onDelete: 'CASCADE' }) 
  post: Post; 

The user and post entities that we wish to associate with in this case, together with the many-to-one decorator, were imported.

Next, create the src/models/Like.ts file and add the following entity:

import { Entity, PrimaryGeneratedColumn } from 'typeorm'; 

export class Like { 
  @PrimaryGeneratedColumn() id: number; 


Seeding test data

We've created database entities so far that TypeORM can use to build SQL tables. Let's start by adding some data to the database. This could be done manually, but it would be tiresome; we need to adequately automate it so that we can have it up and running right away. The process can be made simpler by using the typeorm-seeding package to generate factories and seeders for our entities. Take the following actions.

Return to your terminal and issue the following command to start installing the package:

npm install typeorm-seeding 

Install the type definitions of the Faker library and create the src/database/, database/seeds/, and database/factories/ folders:

npm install -D @types/faker 
cd src/ && mkdir database  
cd database && mkdir seeds factories 

To run the seed commands, add the following scripts to the package.json file:

"scripts": { 
  "seed:config": "ts-node ./node_modules/typeorm-seeding/dist/cli.js config", 
  "seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed", 

To make it simpler for you to dump and sync the database, add the following commands to the package.json file:

"scripts": { 
  "schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop", 
  "schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync", 

  • Date: