“TypOmM in” Code-Antworten

TypOmM in

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

Typorm

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 {}
Restu Wahyu Saputra

Typorm TOSQL

const sql = connection.createQueryBuilder()
    .select("user")
    .from(User, "user")
    .where("user.id = :id", { id: 1 })
    .getSql();
foloinfo

Wo in Typorm

		const getAllProjectTelmarNotNull: Projects[] = await this.projectModel
				.createQueryBuilder('projects')
				.where('projects.telemarketing_id NOTNULL')
				.andWhere('projects.latest_status = :latest_status', { latest_status: 'NEW' })
				.andWhere('projects.telemarketing_id IN (:...telemarketing_id)', { telemarketing_id: telemarketings })
				.getMany()
Restu Wahyu Saputra

Ähnliche Antworten wie “TypOmM in”

Fragen ähnlich wie “TypOmM in”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen