mono code generation

Overview

The mono code is generated by goctl generating model files with the specified document name. The mono does not have form statements like mysql, indexes, and so forth, so mono code generation does not need information like sql or database links.

Task Targets

  1. familiar with goctl generating mono code usage and learn about currently supported instructions and features
  2. Preliminary understanding of the format of goctl generating mono code

Preparing

  1. Complete golang installation
  2. Complete goctl installation

Code Generation

  1. # Create workspaces and projects
  2. $ mkdir -p ~/workspace/model/mongo && ~/workspace/model/mongo
  3. # Generate mongo code with document name User
  4. $ goctl model mongo --type user --dir .
  5. # View generated files
  6. $ ls
  7. error.go usermodel.go usermodelgen.go usertypes.go
  8. # view directory tree
  9. .
  10. ├── error.go
  11. ├── usermodel.go
  12. ├── usermodelgen.go
  13. └── usertypes.go
  14. 0 directories, 4 files

View code

  • error.go
  • usermodel.go
  • usermodelgen.go
  • usertypes.go
  1. package model
  2. import (
  3. "errors"
  4. "github.com/zeromicro/go-zero/core/stores/mon"
  5. )
  6. var (
  7. ErrNotFound = mon.ErrNotFound
  8. ErrInvalidObjectId = errors.New("invalid objectId")
  9. )
  1. package model
  2. import "github.com/zeromicro/go-zero/core/stores/mon"
  3. var _ UserModel = (*customUserModel)(nil)
  4. type (
  5. // UserModel is an interface to be customized, add more methods here,
  6. // and implement the added methods in customUserModel.
  7. UserModel interface {
  8. userModel
  9. }
  10. customUserModel struct {
  11. *defaultUserModel
  12. }
  13. )
  14. // NewUserModel returns a model for the mongo.
  15. func NewUserModel(url, db, collection string) UserModel {
  16. conn := mon.MustNewModel(url, db, collection)
  17. return &customUserModel{
  18. defaultUserModel: newDefaultUserModel(conn),
  19. }
  20. }
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/stores/mon"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. type userModel interface {
  11. Insert(ctx context.Context, data *User) error
  12. FindOne(ctx context.Context, id string) (*User, error)
  13. Update(ctx context.Context, data *User) error
  14. Delete(ctx context.Context, id string) error
  15. }
  16. type defaultUserModel struct {
  17. conn *mon.Model
  18. }
  19. func newDefaultUserModel(conn *mon.Model) *defaultUserModel {
  20. return &defaultUserModel{conn: conn}
  21. }
  22. func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
  23. if data.ID.IsZero() {
  24. data.ID = primitive.NewObjectID()
  25. data.CreateAt = time.Now()
  26. data.UpdateAt = time.Now()
  27. }
  28. _, err := m.conn.InsertOne(ctx, data)
  29. return err
  30. }
  31. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  32. oid, err := primitive.ObjectIDFromHex(id)
  33. if err != nil {
  34. return nil, ErrInvalidObjectId
  35. }
  36. var data User
  37. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  38. switch err {
  39. case nil:
  40. return &data, nil
  41. case mon.ErrNotFound:
  42. return nil, ErrNotFound
  43. default:
  44. return nil, err
  45. }
  46. }
  47. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  48. data.UpdateAt = time.Now()
  49. _, err := m.conn.ReplaceOne(ctx, bson.M{"_id": data.ID}, data)
  50. return err
  51. }
  52. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  53. oid, err := primitive.ObjectIDFromHex(id)
  54. if err != nil {
  55. return ErrInvalidObjectId
  56. }
  57. _, err = m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  58. return err
  59. }
  1. package model
  2. import (
  3. "time"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. )
  6. type User struct {
  7. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  8. // TODO: Fill your own fields
  9. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  10. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  11. }

References