“typeorm in” Kode Jawaban

typeorm in

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

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

Typeorm ToSQL

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

dimana di typeorm

		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

Jawaban yang mirip dengan “typeorm in”

Pertanyaan yang mirip dengan “typeorm in”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya