51
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TypeORMのFindオプション一覧

Last updated at Posted at 2020-11-07

SQLからの逆引きができるように記述しています。

Equal ( = )

SELECT * FROM "post" WHERE "title" = 'About #1' を実行するコード:

const loadedPosts = await connection.getRepository(Post).find({
    title: "About #1"
})

Not

SELECT * FROM "post" WHERE "title" != 'About #1' を実行するコード:

import {Not} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Not("About #1")
})

LessThan ( < )

SELECT * FROM "post" WHERE "likes" < 10 を実行するコード:

import {LessThan} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: LessThan(10)
});

LessThanOrEqual ( <= )

SELECT * FROM "post" WHERE "likes" <= 10 を実行するコード:

import {LessThanOrEqual} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: LessThanOrEqual(10)
});

MoreThan ( > )

SELECT * FROM "post" WHERE "likes" > 10 を実行するコード:

import {MoreThan} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: MoreThan(10)
});

MoreThanOrEqual ( >= )

SELECT * FROM "post" WHERE "likes" >= 10 を実行するコード:

import {MoreThanOrEqual} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: MoreThanOrEqual(10)
});

Like

SELECT * FROM "post" WHERE "title" LIKE '%out #%' を実行するコード:

import {Like} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Like("%out #%")
});

Between

SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10 を実行するコード:

import {Between} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: Between(1, 10)
});

In

SELECT * FROM "post" WHERE "title" IN ('About #2','About #3') を実行するコード:

import {In} from "typeorm";

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

Not In

SELECT * FROM "post" WHERE "title" NOT IN ('About #2','About #3') を実行するコード:

import {In} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Not(In(["About #2", "About #3"]))
});

Any

SELECT * FROM "post" WHERE "title" = ANY(['About #2','About #3']) を実行するコード:

import {Any} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Any(["About #2", "About #3"])
});

IsNull

SELECT * FROM "post" WHERE "title" IS NULL を実行するコード:

import {IsNull} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: IsNull()
});

Raw

SELECT * FROM "post" WHERE "likes" = "dislikes" - 4 を実行するコード:

import {Raw} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: Raw("dislikes - 4")
});

And , Or

SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee') を実行するコード:

const user = connection.getRepository(User).find({
  where: [
    { firstName: "Timber", lastName: "Saw" },
    { firstName: "Stan", lastName: "Lee" }
  ]
});

📖参考

51
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?