51 lines
960 B
Go
51 lines
960 B
Go
package mdb
|
|
|
|
import "strings"
|
|
|
|
type User struct {
|
|
ID uint64
|
|
Name string
|
|
Email string
|
|
Admin bool
|
|
Blocked bool
|
|
}
|
|
|
|
type Users struct {
|
|
*Collection[User]
|
|
ByEmail Index[User] // Unique index on (Email).
|
|
ByName Index[User] // Index on (Name).
|
|
ByBlocked Index[User] // Partial index on (Blocked,Email).
|
|
}
|
|
|
|
func NewUserCollection(db *Database) Users {
|
|
users := Users{}
|
|
|
|
users.Collection = NewCollection[User](db, "Users", nil)
|
|
|
|
users.ByEmail = NewUniqueIndex(
|
|
users.Collection,
|
|
"ByEmail",
|
|
func(lhs, rhs *User) int {
|
|
return strings.Compare(lhs.Email, rhs.Email)
|
|
})
|
|
|
|
users.ByName = NewIndex(
|
|
users.Collection,
|
|
"ByName",
|
|
func(lhs, rhs *User) int {
|
|
return strings.Compare(lhs.Name, rhs.Name)
|
|
})
|
|
|
|
users.ByBlocked = NewPartialIndex(
|
|
users.Collection,
|
|
"ByBlocked",
|
|
func(lhs, rhs *User) int {
|
|
return strings.Compare(lhs.Email, rhs.Email)
|
|
},
|
|
func(item *User) bool {
|
|
return item.Blocked
|
|
})
|
|
|
|
return users
|
|
}
|