typeorm 함수

codeing999·2023년 7월 10일
0

NestJS

목록 보기
4/9

공식 문서 : https://typeorm.io/repository-api

create

const user = repository.create() // same as const user = new User();
const user = repository.create({
    id: 1,
    firstName: "Timber",
    lastName: "Saw",
}) // same as const user = new User(); user.firstName = "Timber"; user.lastName = "Saw";
  • insert하는 것이 아니고, 새 인스턴스를 만드는 함수.

save

  • 하나 혹은 여러 엔티티를 save. 단순 insert가 아님. 이미 존재한다면 update하고 존재하지 않으면 insert한다.
  • undefiend인 속성은 스킵함으로써 부분적 업데이트도 지원

remove

  • 하나 혹은 여러 엔티티를 remove.
  • 지워진 엔티티들을 리턴
await repository.remove(user)
await repository.remove([category1, category2, category3])

insert

  • 하나 혹은 여러 엔티티를 insert.
await repository.insert({
    firstName: "Timber",
    lastName: "Timber",
})

await repository.insert([
    {
        firstName: "Foo",
        lastName: "Bar",
    },
    {
        firstName: "Rizz",
        lastName: "Rak",
    },
])

update

  • 첫번째 인자가 조건, 두번째 인자가 업데이트할 내용
await repository.update({ age: 18 }, { category: "ADULT" })
// executes UPDATE user SET category = ADULT WHERE age = 18

await repository.update(1, { firstName: "Rizzrak" })
// executes UPDATE user SET firstName = Rizzrak WHERE id = 1

upsert

  • 이거도 save랑 마찬가지로 이미 있으면 update하고 없으면 insert하는 함수같은데 차이는 아직 자세히 잘 모르겠다. 이미 있다고 판단할 컬럼을 명시하는 것이 차이일지도. 일단 나중에 알아보기.
await repository.upsert(
    [
        { externalId: "abc123", firstName: "Rizzrak" },
        { externalId: "bca321", firstName: "Karzzir" },
    ],
    ["externalId"],
)
/** executes
 *  INSERT INTO user
 *  VALUES
 *      (externalId = abc123, firstName = Rizzrak),
 *      (externalId = cba321, firstName = Karzzir),
 *  ON CONFLICT (externalId) DO UPDATE firstName = EXCLUDED.firstName
 **/

delete

  • 주어진 조건의 엔티티들을 delete.
  • 숫자만 넣은 건 id로 삭제.
await repository.delete(1)
await repository.delete([1, 2, 3])
await repository.delete({ firstName: "Timber" })

increment & decrement

  • 주어진 조건에 맞는 특정 컬럼을 해당 값만큼 증가/감소
  • 아래는 firtstName이 Timber인 레코드의 age를 3 증가/감소
await repository.increment({ firstName: "Timber" }, "age", 3)

await repository.decrement({ firstName: "Timber" }, "age", 3)

count

countBy

sum

average

minimum

maximum

find

  • 주어진 조건에 맞는 엔티티들을 find.
const timbers = await repository.find({
    where: {
        firstName: "Timber",
    },
})

findBy

  • find랑 비슷한 것 같은데 where구문을 생략한 것 차이인가?
const timbers = await repository.findBy({
    firstName: "Timber",
})

findAndCount

  • find하면서 동시에 조건에 맞는 엔티티수를 카운트.
const [timbers, timbersCount] = await repository.findAndCount({
    where: {
        firstName: "Timber",
    },
})

findAndCountBy

  • 이거도 where 생략된거 정도 차이?
const [timbers, timbersCount] = await repository.findAndCountBy({
    firstName: "Timber",
})

findOne

  • 매치되는 첫번째 엔티티 하나만 find.
const timber = await repository.findOne({
    where: {
        firstName: "Timber",
    },
})

findOneBy

  • 이거도 where 생략된거 정도 차이?
const timber = await repository.findOneBy({ firstName: "Timber" })

findeOneOrFail

findeOneByOrFail

query

  • raw SQL 쿼리 실행
const rawData = await repository.query(`SELECT * FROM USERS`)

clear

profile
코딩 공부 ing..

0개의 댓글