GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

  1. package posts
  2. import (
  3. "log"
  4. "sync"
  5. "git.aiterp.net/rpdata/api/internal/store"
  6. "git.aiterp.net/rpdata/api/models"
  7. "github.com/globalsign/mgo"
  8. )
  9. var collection *mgo.Collection
  10. var mutex sync.RWMutex
  11. func find(query interface{}) (models.Post, error) {
  12. post := models.Post{}
  13. err := collection.Find(query).One(&post)
  14. if err != nil {
  15. return models.Post{}, err
  16. }
  17. return post, nil
  18. }
  19. func list(query interface{}, limit int, sort ...string) ([]models.Post, error) {
  20. size := 64
  21. if limit > 0 {
  22. size = limit
  23. }
  24. posts := make([]models.Post, 0, size)
  25. err := collection.Find(query).Limit(limit).Sort(sort...).All(&posts)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return posts, nil
  30. }
  31. func init() {
  32. store.HandleInit(func(db *mgo.Database) {
  33. collection = db.C("logbot3.posts")
  34. collection.EnsureIndexKey("logId")
  35. collection.EnsureIndexKey("time")
  36. collection.EnsureIndexKey("kind")
  37. collection.EnsureIndexKey("position")
  38. err := collection.EnsureIndex(mgo.Index{
  39. Key: []string{"$text:text"},
  40. })
  41. if err != nil {
  42. log.Fatalln("init logbot3.logs:", err)
  43. }
  44. })
  45. }