“typeorm” Respostas de código

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

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: [],
})
Proud Parrot

Respostas semelhantes a “typeorm”

Procure respostas de código populares por idioma

Procurar outros idiomas de código