Go boltDB query using bolthold 3 conditions

80 views Asked by At

So I had this question before and I had an answer below the question which worked, but I just realized that the query I came out with does not work as planned.

Basically now if it works like this

(if the roleskey contains any of the roles in slice) and (if the tenantID is an empty string) or (if tenantIDKey is equal to tenantID)

But what I need is

(if the roleskey contains any of the roles in slice) AND (if the tenantID is an empty string OR if tenantIDKey is equal to tenantID)

Here is my current query:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

Does anyone know how to solve this?

1

There are 1 answers

2
Chandan On BEST ANSWER

Try:

query := bolthold.
         Where(tenantIDKey).Eq("").
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         ).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).ContainsAny("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).In("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)