typeorm find 옵션

codeing999·2023년 7월 10일
0

NestJS

목록 보기
5/9

공식 문서 : https://typeorm.io/find-options

모든 find로 시작하는 함수에 가능한 옵션들

select

  • select할 컬럼을 명시
userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})

relations

  • 설명만 볼 땐 뭔가 했는데 예시를 보니 여러 테이블 join 기능. left join만 지원하나?
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
})
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: {
            videoAttributes: true,
        },
    },
})

위 코드가 아래 쿼리와 같다고 한다

SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"

SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"
LEFT JOIN "video_attributes" ON "video_attributes"."id" = "videos"."video_attributesId"

where

  • 조건
userRepository.find({
    where: {
        firstName: "Timber",
        lastName: "Saw",
    },
})

relations와 같이 쓴 경우

userRepository.find({
    relations: {
        project: true,
    },
    where: {
        project: {
            name: "TypeORM",
            initials: "TORM",
        },
    },
})

이 것은

SELECT * FROM "user"
LEFT JOIN "project" ON "project"."id" = "user"."projectId"
WHERE "project"."name" = 'TypeORM' AND "project"."initials" = 'TORM'

와 같다고 한다.

여러 조건을 OR하는 경우

userRepository.find({
    where: [
        { firstName: "Timber", lastName: "Saw" },
        { firstName: "Stan", lastName: "Lee" },
    ],
})
  • 이렇게 배열로 감싼 다음 나열하면 OR인거고. 배열 바깥으로 빼서 나열하면 AND인가 봄

order

userRepository.find({
    order: {
        name: "ASC",
        id: "DESC",
    },
})
  • sql의 order by

withDeleted

skip 과 take

skip

userRepository.find({
    skip: 5,
})
  • sql의 offset

take

userRepository.find({
    take: 10,
})
  • sql의 limit

skip과 take는 같이 쓰여야 한다

userRepository.find({
    order: {
        columnName: "ASC",
    },
    skip: 0,
    take: 10,
})

cache

lock

Not

import { Not } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Not("About #1"),
})

Lessthan, LessThanOrEqual, MoreThan, MoreThanOrEuqal

import { LessThan } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: LessThan(10),
})
  • likes라는 변수가 10보다 작은 조건

Equal

import { Equal } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Equal("About #2"),
})

Like

import { Like } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Like("%out #%"),
})

ILike

  • sql에도 똑같은게 있나본데 sql에서도 안써봄

Between

  • 컬럼명이 키값이다 보니 중복되지 못하는데, LessThanOrEqual과 MoreThanOrEqual을 동시에 써야하는 경우 Between으로 하나로 합쳐야 하는 것 같다. 이건 근데 ~~이상 ~~이하로 양쪽 다 포함인데, 만약에 한군데라도 미포함이면 어떻게 해야하는 걸까. 이거 쓰기 전에 미리 처리해줘야할까.
import { Between } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Between(1, 10),
})

In

import { In } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: In(["About #2", "About #3"]),
})

Any

  • 이거도 sql에서도 안써봄

IsNull

import { IsNull } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: IsNull(),
})

ArrayContains

ArrayContainedBy

ArrayOverlap

Raw

profile
코딩 공부 ing..

0개의 댓글