jldb/mdb/db-users_test.go

51 lines
963 B
Go
Raw Permalink Normal View History

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]
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
}