1

I am trying to create a query for boltDB using Go.

I have to have a query that checks for the roles and the tenantID. Basically the item has to always contain the role, also if the item has a tenantID it has to match however if the item does not have a tenantID (if it's nil) it also has to return it.

So I came up with the query below that checks for the role and the tenantID, but doesn't check if the tenantID is nil. Could anyone help me with this? What would I need to add to this query would take into account the tenantID of nil?

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq(tenantID)
Marius
  • 537
  • 1
  • 6
  • 23

1 Answers1

1

You can use Or condition to check if it is equal to tenantIDKey or IsNil

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

You can also you In in place of ContainsAny

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

Chandan
  • 11,465
  • 1
  • 6
  • 25
  • Hey, @Chandan I was using your suggested query and just realized that it does not work as I planned. I have created a new question regarding it. any chance you see the issue there? https://stackoverflow.com/questions/72226242/go-boltdb-query-using-bolthold-3-conditions – Marius May 13 '22 at 08:01