Advertisement
aldikhan13

Best Practice Writing Model Schema Using Typeorm

Jun 1st, 2022 (edited)
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   Column,
  3.   Entity,
  4.   PrimaryGeneratedColumn,
  5.   CreateDateColumn,
  6.   DeleteDateColumn,
  7.   UpdateDateColumn,
  8.   BeforeInsert,
  9.   BeforeUpdate,
  10.   JoinColumn,
  11.   ManyToOne
  12. } from 'typeorm'
  13.  
  14. import { IUsers } from '@interfaces/interface.users'
  15. import { Roles } from '@models/model.roles'
  16. import { Bcrypt } from '@libs/lib.bcrypt'
  17.  
  18. /**
  19. * old style writing model schema using typeorm by default, if you check documentation
  20. **/
  21.  
  22. @Entity()
  23. export class Users implements IUsers {
  24.   @PrimaryGeneratedColumn({ type: 'integer', unsigned: true })
  25.   id!: number
  26.  
  27.   @Column({ type: 'varchar', nullable: false })
  28.   name!: string
  29.  
  30.   @Column({ type: 'varchar', unique: true, nullable: false })
  31.   email!: string
  32.  
  33.   @Column({ type: 'varchar', unique: true, unsigned: true, nullable: false })
  34.   phone!: string
  35.  
  36.   @Column({ type: 'varchar', nullable: false })
  37.   password!: string
  38.  
  39.   @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: new Date() })
  40.   createdAt?: Date
  41.  
  42.   @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: new Date() })
  43.   updatedAt?: Date
  44.  
  45.   @DeleteDateColumn({ name: 'deleted_at', type: 'timestamp', nullable: true })
  46.   deletedAt?: Date
  47.  
  48.   @BeforeInsert()
  49.   protected beforeInserthashPassword() {
  50.     this.password = Bcrypt.hashPassword(this.password)
  51.   }
  52.  
  53.   @BeforeUpdate()
  54.   protected beforeUpdateHashPassword() {
  55.     this.password = Bcrypt.hashPassword(this.password)
  56.   }
  57.  
  58.   @ManyToOne(() => Roles, (relation) => relation.users, { onDelete: 'CASCADE' })
  59.   @JoinColumn({ name: 'role_id' })
  60.   role: Roles
  61. }
  62.  
  63. /**
  64. * my style writing model schema using typeorm, easy to maintance and easy to read
  65. **/
  66.  
  67. class DatabaseRelations {
  68.   @ManyToOne(() => Roles, (relation) => relation.users, { onDelete: 'CASCADE' })
  69.   @JoinColumn({ name: 'role_id' })
  70.   role: Roles
  71. }
  72.  
  73. class DatabaseSchema extends DatabaseRelations {
  74.   @PrimaryGeneratedColumn({ type: 'integer', unsigned: true })
  75.   id!: number
  76.  
  77.   @Column({ type: 'varchar', nullable: false })
  78.   name!: string
  79.  
  80.   @Column({ type: 'varchar', unique: true, nullable: false })
  81.   email!: string
  82.  
  83.   @Column({ type: 'varchar', unique: true, unsigned: true, nullable: false })
  84.   phone!: string
  85.  
  86.   @Column({ type: 'varchar', nullable: false })
  87.   password!: string
  88.  
  89.   @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: new Date() })
  90.   createdAt?: Date
  91.  
  92.   @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: new Date() })
  93.   updatedAt?: Date
  94.  
  95.   @DeleteDateColumn({ name: 'deleted_at', type: 'timestamp', nullable: true })
  96.   deletedAt?: Date
  97. }
  98.  
  99. class DatabaseHooks extends DatabaseSchema {
  100.   @BeforeInsert()
  101.   protected beforeInserthashPassword() {
  102.     this.password = Bcrypt.hashPassword(this.password)
  103.   }
  104.  
  105.   @BeforeUpdate()
  106.   protected beforeUpdateHashPassword() {
  107.     this.password = Bcrypt.hashPassword(this.password)
  108.   }
  109. }
  110.  
  111. @Entity()
  112. export class Users extends DatabaseHooks implements IUsers {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement