package characters import ( "log" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo" ) var collection *mgo.Collection func find(query interface{}) (models.Character, error) { character := models.Character{} err := collection.Find(query).One(&character) if err != nil { return models.Character{}, err } return character, nil } func list(query interface{}) ([]models.Character, error) { characters := make([]models.Character, 0, 64) err := collection.Find(query).All(&characters) if err != nil { return nil, err } return characters, nil } func init() { store.HandleInit(func(db *mgo.Database) { collection = db.C("common.characters") collection.EnsureIndexKey("name") collection.EnsureIndexKey("shortName") collection.EnsureIndexKey("author") err := collection.EnsureIndex(mgo.Index{ Key: []string{"nicks"}, Unique: true, DropDups: true, }) if err != nil { log.Fatalln("init common.characters:", err) } err = collection.EnsureIndex(mgo.Index{ Key: []string{"$text:description"}, }) if err != nil { log.Fatalln("init common.characters:", err) } }) }