|
|
@ -15,147 +15,160 @@ mongo的生成不同于mysql,mysql可以从scheme_information库中读取到
|
|
|
|
而mongo是文档型数据库,我们暂时无法从db中读取某一条记录来实现字段信息获取,就算有也不一定是完整信息(某些字段可能是omitempty修饰,可有可无), 这里采用type自己编写+代码生成方式实现
|
|
|
|
而mongo是文档型数据库,我们暂时无法从db中读取某一条记录来实现字段信息获取,就算有也不一定是完整信息(某些字段可能是omitempty修饰,可有可无), 这里采用type自己编写+代码生成方式实现
|
|
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
为 User 生成 mongo model
|
|
|
|
假设我们需要生成一个usermodel.go的代码文件,其包含用户信息字段有
|
|
|
|
```bahs
|
|
|
|
|
|
|
|
$ goctl model mongo -t User -c --dir .
|
|
|
|
|字段名称|字段类型|
|
|
|
|
|
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|_id|bson.ObejctId|
|
|
|
|
|
|
|
|
|name|string|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 编写types.go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
$ vim types.go
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```golang
|
|
|
|
### 生成示例代码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### usermodel.go
|
|
|
|
|
|
|
|
```go
|
|
|
|
package model
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
//go:generate goctl model mongo -t User
|
|
|
|
import (
|
|
|
|
import "github.com/globalsign/mgo/bson"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/monc"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
var _ UserModel = (*customUserModel)(nil)
|
|
|
|
ID bson.ObjectId `bson:"_id"`
|
|
|
|
|
|
|
|
Name string `bson:"name"`
|
|
|
|
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, c cache.CacheConf) UserModel {
|
|
|
|
|
|
|
|
conn := monc.MustNewModel(url, db, collection, c)
|
|
|
|
|
|
|
|
return &customUserModel{
|
|
|
|
|
|
|
|
defaultUserModel: newDefaultUserModel(conn),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
* 命令行生成 在types.go所在文件夹执行命令
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
$ goctl model mongo -t User -style gozero
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
* 在types.go中添加`//go:generate`,然后点击执行按钮即可生成,内容示例如下:
|
|
|
|
|
|
|
|
```golang
|
|
|
|
|
|
|
|
//go:generate goctl model mongo -t User
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 生成示例代码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* usermodel.go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```golang
|
|
|
|
#### usermodelgen.go
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
|
|
|
// Code generated by goctl. DO NOT EDIT!
|
|
|
|
package model
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/globalsign/mgo/bson"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/monc"
|
|
|
|
cachec "github.com/zeromicro/go-zero/core/stores/cache"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/mongoc"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type UserModel interface {
|
|
|
|
var prefixUserCacheKey = "cache:user:"
|
|
|
|
Insert(data *User, ctx context.Context) error
|
|
|
|
|
|
|
|
FindOne(id string, ctx context.Context) (*User, error)
|
|
|
|
type userModel interface {
|
|
|
|
Update(data *User, ctx context.Context) error
|
|
|
|
Insert(ctx context.Context, data *User) error
|
|
|
|
Delete(id string, ctx context.Context) 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 {
|
|
|
|
type defaultUserModel struct {
|
|
|
|
*mongoc.Model
|
|
|
|
conn *monc.Model
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewUserModel(url, collection string, c cachec.CacheConf) UserModel {
|
|
|
|
func newDefaultUserModel(conn *monc.Model) *defaultUserModel {
|
|
|
|
return &defaultUserModel{
|
|
|
|
return &defaultUserModel{conn: conn}
|
|
|
|
Model: mongoc.MustNewModel(url, collection, c),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Insert(data *User, ctx context.Context) error {
|
|
|
|
func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
|
|
|
|
if !data.ID.Valid() {
|
|
|
|
if !data.ID.IsZero() {
|
|
|
|
data.ID = bson.NewObjectId()
|
|
|
|
data.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
data.CreateAt = time.Now()
|
|
|
|
|
|
|
|
data.UpdateAt = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
session, err := m.TakeSession()
|
|
|
|
key := prefixUserCacheKey + data.ID.Hex()
|
|
|
|
if err != nil {
|
|
|
|
_, err := m.conn.InsertOne(ctx, key, data)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer m.PutSession(session)
|
|
|
|
func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
|
|
|
|
return m.GetCollection(session).Insert(data)
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOne(id string, ctx context.Context) (*User, error) {
|
|
|
|
|
|
|
|
if !bson.IsObjectIdHex(id) {
|
|
|
|
|
|
|
|
return nil, ErrInvalidObjectId
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session, err := m.TakeSession()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, ErrInvalidObjectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer m.PutSession(session)
|
|
|
|
|
|
|
|
var data User
|
|
|
|
var data User
|
|
|
|
|
|
|
|
key := prefixUserCacheKey + data.ID.Hex()
|
|
|
|
err = m.GetCollection(session).FindOneIdNoCache(&data, bson.ObjectIdHex(id))
|
|
|
|
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return &data, nil
|
|
|
|
return &data, nil
|
|
|
|
case mongoc.ErrNotFound:
|
|
|
|
case monc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Update(data *User, ctx context.Context) error {
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
|
|
|
|
session, err := m.TakeSession()
|
|
|
|
data.UpdateAt = time.Now()
|
|
|
|
if err != nil {
|
|
|
|
key := prefixUserCacheKey + data.ID.Hex()
|
|
|
|
|
|
|
|
_, err := m.conn.ReplaceOne(ctx, key, bson.M{"_id": data.ID}, data)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer m.PutSession(session)
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
|
|
|
|
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
return m.GetCollection(session).UpdateIdNoCache(data.ID, data)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Delete(id string, ctx context.Context) error {
|
|
|
|
|
|
|
|
session, err := m.TakeSession()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ErrInvalidObjectId
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
key := prefixUserCacheKey + id
|
|
|
|
|
|
|
|
_, err = m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer m.PutSession(session)
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id))
|
|
|
|
#### usertypes.go
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* error.go
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```golang
|
|
|
|
#### error.go
|
|
|
|
|
|
|
|
```go
|
|
|
|
package model
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
import (
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
var (
|
|
|
|
var ErrInvalidObjectId = errors.New("invalid objectId")
|
|
|
|
ErrNotFound = mon.ErrNotFound
|
|
|
|
|
|
|
|
ErrInvalidObjectId = errors.New("invalid objectId")
|
|
|
|
|
|
|
|
)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 文件目录预览
|
|
|
|
### 文件目录预览
|
|
|
@ -163,48 +176,33 @@ type User struct {
|
|
|
|
```text
|
|
|
|
```text
|
|
|
|
.
|
|
|
|
.
|
|
|
|
├── error.go
|
|
|
|
├── error.go
|
|
|
|
├── types.go
|
|
|
|
├── usermodel.go
|
|
|
|
└── usermodel.go
|
|
|
|
├── usermodelgen.go
|
|
|
|
|
|
|
|
└── usertypes.go
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 命令预览
|
|
|
|
## 命令预览
|
|
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
```text
|
|
|
|
NAME:
|
|
|
|
Generate mongo model
|
|
|
|
goctl model - generate model code
|
|
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
USAGE:
|
|
|
|
goctl model mongo [flags]
|
|
|
|
goctl model command [command options] [arguments...]
|
|
|
|
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
COMMANDS:
|
|
|
|
--branch string The branch of the remote repo, it does work with --remote
|
|
|
|
mysql generate mysql model
|
|
|
|
-c, --cache Generate code with cache [optional]
|
|
|
|
mongo generate mongo model
|
|
|
|
-d, --dir string The target dir
|
|
|
|
|
|
|
|
-h, --help help for mongo
|
|
|
|
OPTIONS:
|
|
|
|
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
|
|
|
|
--help, -h show help
|
|
|
|
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
|
|
|
|
```
|
|
|
|
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
|
|
|
|
|
|
|
|
--style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
|
|
|
|
```text
|
|
|
|
-t, --type strings Specified model type name
|
|
|
|
NAME:
|
|
|
|
|
|
|
|
goctl model mongo - generate mongo model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
|
|
|
|
goctl model mongo [command options] [arguments...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
|
|
|
|
--type value, -t value specified model type name
|
|
|
|
|
|
|
|
--cache, -c generate code with cache [optional]
|
|
|
|
|
|
|
|
--dir value, -d value the target dir
|
|
|
|
|
|
|
|
--style value the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
> 温馨提示
|
|
|
|
> 温馨提示
|
|
|
|
>
|
|
|
|
>
|
|
|
|
> `--type` 支持slice传值,示例 `goctl model mongo -t=User -t=Class`
|
|
|
|
> `--type` 支持slice传值,示例 `goctl model mongo -t=User -t=Class`
|
|
|
|
## 注意事项
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
types.go本质上与xxxmodel.go无关,只是将type定义部分交给开发人员自己编写了,在xxxmodel.go中,mongo文档的存储结构必须包含
|
|
|
|
|
|
|
|
`_id`字段,对应到types中的field为`ID`,model中的findOne,update均以data.ID来进行操作的,当然,如果不符合你的命名风格,你也 可以修改模板,只要保证`id`
|
|
|
|
|
|
|
|
在types中的field名称和模板中一致就行。
|
|
|
|
|
|
|
|