Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typeorm schema

import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  DeleteDateColumn,
  UpdateDateColumn,
  BeforeInsert,
  BeforeUpdate,
  JoinColumn,
  ManyToOne
} from 'typeorm'

import { IUsers } from '@interfaces/interface.users'
import { Roles } from '@models/model.roles'
import { Bcrypt } from '@libs/lib.bcrypt'

/**
* old style writing model schema using typeorm by default, if you check documentation
**/

@Entity()
export class Users implements IUsers {
  @PrimaryGeneratedColumn({ type: 'integer', unsigned: true })
  id!: number

  @Column({ name: 'last_name', type: 'varchar', nullable: false })
  name!: string

  @Column({ type: 'varchar', unique: true, nullable: false })
  email!: string

  @Column({ type: 'varchar', unique: true, unsigned: true, nullable: false })
  phone!: string

  @Column({ type: 'varchar', nullable: false })
  password!: string

  @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: new Date() })
  createdAt?: Date

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: new Date() })
  updatedAt?: Date

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamp', nullable: true })
  deletedAt?: Date

  @BeforeInsert()
  protected beforeInserthashPassword() {
    this.password = Bcrypt.hashPassword(this.password)
  }

  @BeforeUpdate()
  protected beforeUpdateHashPassword() {
    this.password = Bcrypt.hashPassword(this.password)
  }

  @ManyToOne(() => Roles, (relation) => relation.users, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'role_id' })
  role: Roles
}

/**
* my style writing model schema using typeorm, easy to maintance and easy to read
**/

class DatabaseRelations {
  @ManyToOne(() => Roles, (relation) => relation.users, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'role_id' })
  role: Roles
}

class DatabaseSchema extends DatabaseRelations {
  @PrimaryGeneratedColumn({ type: 'integer', unsigned: true })
  id!: number

  @Column({ name: 'last_name', type: 'varchar', nullable: false })
  name!: string

  @Column({ type: 'varchar', unique: true, nullable: false })
  email!: string

  @Column({ type: 'varchar', unique: true, unsigned: true, nullable: false })
  phone!: string

  @Column({ type: 'varchar', nullable: false })
  password!: string

  @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: new Date() })
  createdAt?: Date

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: new Date() })
  updatedAt?: Date

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamp', nullable: true })
  deletedAt?: Date
}

class DatabaseHooks extends DatabaseSchema {
  @BeforeInsert()
  protected beforeInserthashPassword() {
    this.password = Bcrypt.hashPassword(this.password)
  }

  @BeforeUpdate()
  protected beforeUpdateHashPassword() {
    this.password = Bcrypt.hashPassword(this.password)
  }
}

@Entity()
export class Users extends DatabaseHooks implements IUsers {}
Comment

PREVIOUS NEXT
Code Example
Typescript :: ts builder pattern 
Typescript :: SafeValue must use [property]=binding: 
Typescript :: react functional components setstate callback 
Typescript :: how to define types in typescript 
Typescript :: how to pring events in pygame 
Typescript :: typescript class inheritance 
Typescript :: nuxt 3 nuxtServerInit 
Typescript :: typescript delete value from map 
Typescript :: Scroll, Position 
Typescript :: declare type function typescript 
Typescript :: typescript annotate return type 
Typescript :: fetch tweets 
Typescript :: s3.bucket objects filter top 10 
Typescript :: how to set value to readonly property in typescript 
Typescript :: curl -s "http://google.com?[1-1000]" 
Typescript :: calling funcionts in bash 
Typescript :: convert epoch to normal date | stripe | epoch 
Typescript :: get-dirstats not recognized 
Typescript :: does casting have a higher precedence than dots java 
Typescript :: c++ program to separate unique elements of array 
Typescript :: output products from collection 
Typescript :: Summation with limits in MATLAB 
Typescript :: whats the extension of a markup language 
Typescript :: how to get all posible subb lists in python 
Typescript :: serenity framework break form 
Typescript :: jwt.verify into promise mongoose with typescript 
Typescript :: dynamic index in typescript 
Typescript :: typescript maybe type 
Typescript :: program that will convert input number to farenheit to its equivalent measure in celsius for python 
Typescript :: git remove two commits but not the code 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =