mongo 代码生成
概述
mongo 代码生成是通过 goctl 生成指定文档名称的 model 文件,mongo 不像 mysql 一样有建表语句,索引等集中有规律的约束信息,因此 mongo 代码的生成无需类似 sql 或者数据库链接一样的信息。
任务目标
- 熟悉 goctl 生成 mongo 代码的命令使用,了解目前支持的指令和功能
- 初步了解 goctl 生成 mongo 代码的格式
准备条件
代码生成
# 创建工作空间和工程
$ mkdir -p ~/workspace/model/mongo && ~/workspace/model/mongo
# 生成文档名称为 User 的 mongo 代码
$ goctl model mongo --type user --dir .
# 查看生成的文件
$ ls
error.go usermodel.go usermodelgen.go usertypes.go
# 查看目录树
.
├── error.go
├── usermodel.go
├── usermodelgen.go
└── usertypes.go
0 directories, 4 files
查看代码
- error.go
- usermodel.go
- usermodelgen.go
- usertypes.go
package model
import (
"errors"
"github.com/zeromicro/go-zero/core/stores/mon"
)
var (
ErrNotFound = mon.ErrNotFound
ErrInvalidObjectId = errors.New("invalid objectId")
)
package model
import "github.com/zeromicro/go-zero/core/stores/mon"
var _ UserModel = (*customUserModel)(nil)
type (
// UserModel is an interface to be customized, add more methods here,
// and implement the added methods in customUserModel.
UserModel interface {
userModel
}
customUserModel struct {
*defaultUserModel
}
)
// NewUserModel returns a model for the mongo.
func NewUserModel(url, db, collection string) UserModel {
conn := mon.MustNewModel(url, db, collection)
return &customUserModel{
defaultUserModel: newDefaultUserModel(conn),
}
}
// Code generated by goctl. DO NOT EDIT.
package model
import (
"context"
"time"
"github.com/zeromicro/go-zero/core/stores/mon"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type userModel interface {
Insert(ctx context.Context, data *User) error
FindOne(ctx context.Context, id string) (*User, error)
Update(ctx context.Context, data *User) error
Delete(ctx context.Context, id string) error
}
type defaultUserModel struct {
conn *mon.Model
}
func newDefaultUserModel(conn *mon.Model) *defaultUserModel {
return &defaultUserModel{conn: conn}
}
func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
}
_, err := m.conn.InsertOne(ctx, data)
return err
}
func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, ErrInvalidObjectId
}
var data User
err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
switch err {
case nil:
return &data, nil
case mon.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
data.UpdateAt = time.Now()
_, err := m.conn.ReplaceOne(ctx, bson.M{"_id": data.ID}, data)
return err
}
func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return ErrInvalidObjectId
}
_, err = m.conn.DeleteOne(ctx, bson.M{"_id": oid})
return err
}
package model
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type User struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
// TODO: Fill your own fields
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
}