template.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package template
  2. // Text provides the default template for model to generate
  3. var Text = `package model
  4. import (
  5. "context"
  6. "github.com/globalsign/mgo/bson"
  7. cachec "git.i2edu.net/i2/go-zero/core/stores/cache"
  8. "git.i2edu.net/i2/go-zero/core/stores/mongoc"
  9. )
  10. {{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.Type}}:"{{end}}
  11. type {{.Type}}Model interface{
  12. Insert(ctx context.Context,data *{{.Type}}) error
  13. FindOne(ctx context.Context,id string) (*{{.Type}}, error)
  14. Update(ctx context.Context,data *{{.Type}}) error
  15. Delete(ctx context.Context,id string) error
  16. }
  17. type default{{.Type}}Model struct {
  18. *mongoc.Model
  19. }
  20. func New{{.Type}}Model(url, collection string, c cachec.CacheConf) {{.Type}}Model {
  21. return &default{{.Type}}Model{
  22. Model: mongoc.MustNewModel(url, collection, c),
  23. }
  24. }
  25. func (m *default{{.Type}}Model) Insert(ctx context.Context, data *{{.Type}}) error {
  26. if !data.ID.Valid() {
  27. data.ID = bson.NewObjectId()
  28. }
  29. session, err := m.TakeSession()
  30. if err != nil {
  31. return err
  32. }
  33. defer m.PutSession(session)
  34. return m.GetCollection(session).Insert(data)
  35. }
  36. func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Type}}, error) {
  37. if !bson.IsObjectIdHex(id) {
  38. return nil, ErrInvalidObjectId
  39. }
  40. session, err := m.TakeSession()
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer m.PutSession(session)
  45. var data {{.Type}}
  46. {{if .Cache}}key := prefix{{.Type}}CacheKey + id
  47. err = m.GetCollection(session).FindOneId(&data, key, bson.ObjectIdHex(id))
  48. {{- else}}
  49. err = m.GetCollection(session).FindOneIdNoCache(&data, bson.ObjectIdHex(id))
  50. {{- end}}
  51. switch err {
  52. case nil:
  53. return &data,nil
  54. case mongoc.ErrNotFound:
  55. return nil,ErrNotFound
  56. default:
  57. return nil,err
  58. }
  59. }
  60. func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) error {
  61. session, err := m.TakeSession()
  62. if err != nil {
  63. return err
  64. }
  65. defer m.PutSession(session)
  66. {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex()
  67. return m.GetCollection(session).UpdateId(data.ID, data, key)
  68. {{- else}}
  69. return m.GetCollection(session).UpdateIdNoCache(data.ID, data)
  70. {{- end}}
  71. }
  72. func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) error {
  73. session, err := m.TakeSession()
  74. if err != nil {
  75. return err
  76. }
  77. defer m.PutSession(session)
  78. {{if .Cache}}key := prefix{{.Type}}CacheKey + id
  79. return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key)
  80. {{- else}}
  81. return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id))
  82. {{- end}}
  83. }
  84. `
  85. // Error provides the default template for error definition in mongo code generation.
  86. var Error = `
  87. package model
  88. import "errors"
  89. var ErrNotFound = errors.New("not found")
  90. var ErrInvalidObjectId = errors.New("invalid objectId")
  91. `