123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package template
- // Text provides the default template for model to generate
- var Text = `package model
- import (
- "context"
- "github.com/globalsign/mgo/bson"
- cachec "git.i2edu.net/i2/go-zero/core/stores/cache"
- "git.i2edu.net/i2/go-zero/core/stores/mongoc"
- )
- {{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.Type}}:"{{end}}
- type {{.Type}}Model interface{
- Insert(ctx context.Context,data *{{.Type}}) error
- FindOne(ctx context.Context,id string) (*{{.Type}}, error)
- Update(ctx context.Context,data *{{.Type}}) error
- Delete(ctx context.Context,id string) error
- }
- type default{{.Type}}Model struct {
- *mongoc.Model
- }
- func New{{.Type}}Model(url, collection string, c cachec.CacheConf) {{.Type}}Model {
- return &default{{.Type}}Model{
- Model: mongoc.MustNewModel(url, collection, c),
- }
- }
- func (m *default{{.Type}}Model) Insert(ctx context.Context, data *{{.Type}}) error {
- if !data.ID.Valid() {
- data.ID = bson.NewObjectId()
- }
- session, err := m.TakeSession()
- if err != nil {
- return err
- }
- defer m.PutSession(session)
- return m.GetCollection(session).Insert(data)
- }
- func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Type}}, error) {
- if !bson.IsObjectIdHex(id) {
- return nil, ErrInvalidObjectId
- }
- session, err := m.TakeSession()
- if err != nil {
- return nil, err
- }
- defer m.PutSession(session)
- var data {{.Type}}
- {{if .Cache}}key := prefix{{.Type}}CacheKey + id
- err = m.GetCollection(session).FindOneId(&data, key, bson.ObjectIdHex(id))
- {{- else}}
- err = m.GetCollection(session).FindOneIdNoCache(&data, bson.ObjectIdHex(id))
- {{- end}}
- switch err {
- case nil:
- return &data,nil
- case mongoc.ErrNotFound:
- return nil,ErrNotFound
- default:
- return nil,err
- }
- }
- func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) error {
- session, err := m.TakeSession()
- if err != nil {
- return err
- }
- defer m.PutSession(session)
- {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex()
- return m.GetCollection(session).UpdateId(data.ID, data, key)
- {{- else}}
- return m.GetCollection(session).UpdateIdNoCache(data.ID, data)
- {{- end}}
- }
- func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) error {
- session, err := m.TakeSession()
- if err != nil {
- return err
- }
- defer m.PutSession(session)
- {{if .Cache}}key := prefix{{.Type}}CacheKey + id
- return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key)
- {{- else}}
- return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id))
- {{- end}}
- }
- `
- // Error provides the default template for error definition in mongo code generation.
- var Error = `
- package model
- import "errors"
- var ErrNotFound = errors.New("not found")
- var ErrInvalidObjectId = errors.New("invalid objectId")
- `
|