Home > database >  Go boltDB query using bolthold
Go boltDB query using bolthold

Time:05-03

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)

CodePudding user response:

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)
         )

  • Related