简体   繁体   中英

TypeORM activeRecord style query for “where (a & b) or (c & d)”?

I want to use TypeORM to run a query like

... WHERE (a=1 && b=true) OR (a=2 && b=false)

I see a bunch of references to doing this in the QueryBuilder style , but I need to know how to do this using the ActiveRecord style.

You can do this by providing an array of objects to the where: property:

Item.find({
    where: [
        { user: { id: userId }, confirmed: "true" },
        { user: { id: userId }, status: "active" }
    ]
});

The query above would find items that belong to a user where

  • user matches user id AND is confirmed, or...
  • user matches user id AND is active

I do not know about TypeORM, however I found this on ruby on rail active query methods:

https://guides.rubyonrails.org/active_record_querying.html#conditions

raw code: ...where("orders_count = ? AND locked = ?", params[:orders], false)
Example Code: ...where(["(a= ? and b= ?) Or (a=? and b=?)", 1, true, 2, false])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM