github.com/reggieriser/pop@v4.13.1+incompatible/scopes.go (about)

     1  package pop
     2  
     3  // ScopeFunc applies a custom operation on a given `Query`
     4  type ScopeFunc func(q *Query) *Query
     5  
     6  // Scope the query by using a `ScopeFunc`
     7  //
     8  //	func ByName(name string) ScopeFunc {
     9  //		return func(q *Query) *Query {
    10  //			return q.Where("name = ?", name)
    11  //		}
    12  //	}
    13  //
    14  //	func WithDeleted(q *pop.Query) *pop.Query {
    15  //		return q.Where("deleted_at is null")
    16  //	}
    17  //
    18  //	c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
    19  func (q *Query) Scope(sf ScopeFunc) *Query {
    20  	return sf(q)
    21  }
    22  
    23  // Scope the query by using a `ScopeFunc`
    24  //
    25  //	func ByName(name string) ScopeFunc {
    26  //		return func(q *Query) *Query {
    27  //			return q.Where("name = ?", name)
    28  //		}
    29  //	}
    30  //
    31  //	func WithDeleted(q *pop.Query) *pop.Query {
    32  //		return q.Where("deleted_at is null")
    33  //	}
    34  //
    35  //	c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
    36  func (c *Connection) Scope(sf ScopeFunc) *Query {
    37  	return Q(c).Scope(sf)
    38  }