Setup TypeORM

Setup TypeORM

Update the src/models/Profile.ts file by importing CreateDateColumn, OneToMany and the entities:

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'; 
import { OneToMany } from 'typeorm'; 
import { Post } from './Post'; 
import { Comment } from './Comment'; 
import { Like } from './Like'; 

@Entity() 
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; 
} 

Next, add the createdAt field and relations as follows:

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'; 
import { OneToMany } from 'typeorm'; 
import { Post } from './Post'; 
import { Comment } from './Comment'; 
import { Like } from './Like'; 

@Entity() 
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; 

  @OneToMany(type => Post, post => post.author) posts: Post[]; 
  @OneToMany(type => Comment, comment => comment.author) comments: Comment[]; 
  @OneToMany(type => Like, like => like.profile) likes: Like[]; 

  @CreateDateColumn() createdAt: Date; 
} 

Next, update the src/models/Post.ts file as follows:

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'; 
import { OneToOne, OneToMany, ManyToOne, JoinColumn } from 'typeorm'; 

import { Profile } from './Profile'; 
import { Comment } from './Comment'; 
import { Like } from './Like'; 

@Entity() 
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; 

  @OneToOne(type => Comment, comment => comment.post, { onDelete: 'SET NULL' })  
  @ManyToOne(type => Profile, profile => profile.posts, { onDelete: 'CASCADE' }) author: Profile;   
  @OneToMany(type => Comment, comment => comment.post) comments: Comment[]; 
  @OneToMany(type => Like, like => like.post) likes: Like[]; 

  @CreateDateColumn() createdAt: Date;  
} 

Next, update the src/models/Comment.ts file as follows:

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'; 
import { ManyToOne } from 'typeorm'; 
import { Profile } from './Profile'; 
import { Post } from './Post'; 

@Entity() 
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; 

  @CreateDateColumn() createdAt: Date; 
} 

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

Then, we've added fields, author and post, with the annotation @ManyToOne. This suggests that a post may receive numerous comments and that a single user may submit numerous comments. These are the inverse relationships for the user and post entities that have been specified.

Next, update the src/models/Like.ts file as follows:

import { Entity, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'; 
import { ManyToOne } from 'typeorm'; 
import { Profile } from './Profile'; 
import { Post } from './Post'; 

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

  @ManyToOne(type => Profile, profile => profile.likes, { onDelete: 'CASCADE' }) 
  profile: Profile;   

  @ManyToOne(type => Post, post => post.likes, { onDelete: 'CASCADE' }) 
  post: Post; 

  @CreateDateColumn() createdAt: Date; 
} 

  • Date: