package posts import ( "log" "sync" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo" ) var collection *mgo.Collection var mutex sync.RWMutex func find(query interface{}) (models.Post, error) { post := models.Post{} err := collection.Find(query).One(&post) if err != nil { return models.Post{}, err } return post, nil } func list(query interface{}, limit int, sort ...string) ([]models.Post, error) { size := 64 if limit > 0 { size = limit } posts := make([]models.Post, 0, size) err := collection.Find(query).Limit(limit).Sort(sort...).All(&posts) if err != nil { return nil, err } return posts, nil } func init() { store.HandleInit(func(db *mgo.Database) { collection = db.C("logbot3.posts") collection.EnsureIndexKey("logId") collection.EnsureIndexKey("time") collection.EnsureIndexKey("kind") collection.EnsureIndexKey("position") err := collection.EnsureIndex(mgo.Index{ Key: []string{"$text:text"}, }) if err != nil { log.Fatalln("init logbot3.logs:", err) } }) }