2023-10-13 09:43:27 +00:00
|
|
|
package mdb
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID uint64
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
Admin bool
|
|
|
|
Blocked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Users struct {
|
|
|
|
*Collection[User]
|
2023-11-17 10:06:56 +00:00
|
|
|
ByEmail *Index[User] // Unique index on (Email).
|
|
|
|
ByName *Index[User] // Index on (Name).
|
|
|
|
ByBlocked *Index[User] // Partial index on (Blocked,Email).
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|