Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

typeorm

npm install typeorm -g
typeorm init --name MyProject --database mysql
Comment

typeorm

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

typeorm IN

.where("user.name IN (:...names)", { names: [ "Timber", "Cristal", "Lina" ] })
Comment

typeorm

import "reflect-metadata"
import { DataSource } from "typeorm"
import { User } from "./entity/User"

export const AppDataSource = new DataSource({
    type: "pg",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "testdb",
    synchronize: true,
    logging: false,
    entities: [User],
    migrations: [],
    subscribers: [],
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: airbnb and eslint react native 
Javascript :: arrow function in es6 
Javascript :: js create nested object from array 
Javascript :: web storage api 
Javascript :: why node_modules are not installed anymore 
Javascript :: react native svg size 
Javascript :: javascript if one line 
Javascript :: array iterator javascript 
Javascript :: react native image picker 
Javascript :: what is prototype in javascript 
Javascript :: create and save xml file in javascript 
Javascript :: The Lodash Array Remove Method 
Javascript :: get array element by index javascript 
Javascript :: gsap react 
Javascript :: jquerey dropdown button 
Javascript :: readline nodejs 
Javascript :: How To Use Multiple Styles in REACT 
Javascript :: simple chat app 
Javascript :: ArduinoJson.h 
Javascript :: js set to array 
Javascript :: ternary javascript 
Javascript :: how to pass props in react 
Javascript :: javascript Adding New Elements 
Javascript :: node md5 decrypt 
Javascript :: javascript validator 
Javascript :: useeffect cleanup function 
Javascript :: Check Whether Variable Is String Or Number In JavaScript 
Javascript :: javaScript throw statement 
Javascript :: static in javascript 
Javascript :: binding style vuejs 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =