mongo 代码生成

概述

mongo 代码生成是通过 goctl 生成指定文档名称的 model 文件,mongo 不像 mysql 一样有建表语句,索引等集中有规律的约束信息,因此 mongo 代码的生成无需类似 sql 或者数据库链接一样的信息。

任务目标

  1. 熟悉 goctl 生成 mongo 代码的命令使用,了解目前支持的指令和功能
  2. 初步了解 goctl 生成 mongo 代码的格式

准备条件

  1. 完成 golang 安装
  2. 完成 goctl 安装

代码生成

  1. # 创建工作空间和工程
  2. $ mkdir -p ~/workspace/model/mongo && ~/workspace/model/mongo
  3. # 生成文档名称为 User 的 mongo 代码
  4. $ goctl model mongo --type user --dir .
  5. # 查看生成的文件
  6. $ ls
  7. error.go usermodel.go usermodelgen.go usertypes.go
  8. # 查看目录树
  9. .
  10. ├── error.go
  11. ├── usermodel.go
  12. ├── usermodelgen.go
  13. └── usertypes.go
  14. 0 directories, 4 files

查看代码

  • 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. }

参考文献