Skip to content

Commit

Permalink
upgrade version to v0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Feb 16, 2023
1 parent 1b011b0 commit f4aa6ec
Show file tree
Hide file tree
Showing 18 changed files with 686 additions and 145 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 dobyte

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# mongo-dao-generator

[English Document](README.md)

### 1.介绍

mongo-dao-generator是一个自动化生成MongoDB数据访问对象(Data Access Object)的工具。

### 2.优势

* 支持primitive.ObjectID、primitive.DateTime类型的自动填充。

* 支持int、int8、int16、int32、int64、uint、uint8、uint16、uint32、uint64类型的自增长。

* 提供了对数据库字段的统一生成方案,避免了业务代码中随处可见的数据库字段的问题。

* 提供了包括InsertOne、InsertMany、UpdateOne、UpdateOneByID、UpdateMany、FindOne、FindOneByID、FindMany、DeleteOne、DeleteOneByID、DeleteMany、Count、Aggregate等多种数据库操作接口。

* 提供了对数据库操作接口的扩展能力。

* 提供了分包与不分包两种包解决方案。

* 支持目录和文件名风格的自定义。

### 3.安装

```shell
go intall github.com/dobyte/mongo-dao-generator
```

### 4.用法

```shell
Usage of mongo-dao-generator:
mongo-dao-generator [flags] -model-dir=. -model-names=T,T -dao-dir=./dao
For more information, see:
https://github.com/dobyte/mongo-dao-generator
Flags:
-counter-name string
specify the counter name; default is counter
-dao-dir string
specify the output directory of dao files; must be set
-dao-pkg-path string
specify the package path corresponding to the output directory of the dao files; automatically generated by default
-file-style string
specify the generation style for file; options: kebab | underscore | lower | camel | pascal; default is underscore (default "underscore")
-model-dir string
specify the model directory; must be set
-model-names string
specify the comma-separated list of model name; must be set
-model-pkg-alias string
specify a model package alias; default no alias
-model-pkg-path string
specify the package path corresponding to the model directory; automatically calculated by default
-subpkg-enable
specify whether to enable subpkg; default disable
-subpkg-style string
specify the generation style for subpkg; options: kebab | underscore | lower | camel | pascal; default is kebab (default "kebab")
```

### 5.标签

在模型定义中支持对gen标签的解析,目前支持以下标签解析:

| 标签名称 | | 示例 | 说明 |
| -------- | ---------------------------------------------------------- | ------------------ | -------------------------- |
| autoFill | primitive.ObjectID、primitive.DateTime | gen:"autoFill" | |
| autoIncr | int、int8、int16、int32、int64、uint、uint8、uint16、uint32、uint64 | gen:"autoIncr:uid" | 该自增为原子操作,会在生成代码时同步生成计数器代码。 |

### 6.示例

###### 6-1.创建模型

model/mail.go

```go
package model

import "go.mongodb.org/mongo-driver/bson/primitive"

//go:generate mongo-dao-generator -model-dir=. -model-names=Mail -dao-dir=../dao/
type Mail struct {
ID primitive.ObjectID `bson:"_id" gen:"autoFill"` // 邮件ID
Title string `bson:"title"` // 邮件标题
Content string `bson:"content"` // 邮件内容
Sender int64 `bson:"sender"` // 邮件发送者
Receiver int64 `bson:"receiver"` // 邮件接受者
Status int `bson:"status"` // 邮件状态
SendTime primitive.DateTime `bson:"send_time" gen:"autoFill"` // 发送时间
}
```

###### 6-2.生成dao文件

```bash
% go generate ./...
```
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# mongo-dao-generator

##### [中文文档](README-zh.md)

### 1.Introduction

mongo-dao-generator is a tool for automatically generating MongoDB Data Access Object.

### 2.Advantage

* Supports automatic filling of primitive.ObjectID and primitive.DateTime types.

* Supports automatic increment of int, int8, int16, int32, int64, uint, uint8, uint16, uint32 and uint64 types.

* Provides a unified generation scheme for database fields, avoiding the problem of database fields that can be seen everywhere in business codes.

* Provides various database operation interfaces including InsertOne, InsertMany, UpdateOne, UpdateOneByID, UpdateMany, FindOne, FindOneByID, FindMany, DeleteOne, DeleteOneByID, DeleteMany, Count, Aggregate, etc.

* Provides the ability to expand the database operation interface.

* Provides two package solutions: subcontracting and non-subcontracting.

* Supports customization of directory and file name styles.

### 3.Download and install

```shell
go intall github.com/dobyte/mongo-dao-generator
```

### 4.Usage

```shell
Usage of mongo-dao-generator:
mongo-dao-generator [flags] -model-dir=. -model-names=T,T -dao-dir=./dao
For more information, see:
https://github.com/dobyte/mongo-dao-generator
Flags:
-counter-name string
specify the counter name; default is counter
-dao-dir string
specify the output directory of dao files; must be set
-dao-pkg-path string
specify the package path corresponding to the output directory of the dao files; automatically generated by default
-file-style string
specify the generation style for file; options: kebab | underscore | lower | camel | pascal; default is underscore (default "underscore")
-model-dir string
specify the model directory; must be set
-model-names string
specify the comma-separated list of model name; must be set
-model-pkg-alias string
specify a model package alias; default no alias
-model-pkg-path string
specify the package path corresponding to the model directory; automatically calculated by default
-subpkg-enable
specify whether to enable subpkg; default disable
-subpkg-style string
specify the generation style for subpkg; options: kebab | underscore | lower | camel | pascal; default is kebab (default "kebab")
```

### 5.Extension tags

The parsing of gen tags is supported in the model definition, and the following tag parsing is currently supported:

| Tag Name | | Example | Description |
| -------- | ---------------------------------------------------------- | ------------------ | --------------------------------------------------------------------------------------------------- |
| autoFill | primitive.ObjectID、primitive.DateTime | gen:"autoFill" | |
| autoIncr | int、int8、int16、int32、int64、uint、uint8、uint16、uint32、uint64 | gen:"autoIncr:uid" | The increment is atomic and the counter code is generated synchronously when the code is generated. |

### 6.Example

###### 6-1.Create model

model/mail.go

```go
package model

import "go.mongodb.org/mongo-driver/bson/primitive"

//go:generate mongo-dao-generator -model-dir=. -model-names=Mail -dao-dir=../dao/
type Mail struct {
ID primitive.ObjectID `bson:"_id" gen:"autoFill"` // mail id
Title string `bson:"title"` // mail title
Content string `bson:"content"` // mail content
Sender int64 `bson:"sender"` // sender's uid
Receiver int64 `bson:"receiver"` // receiver's uid
Status int `bson:"status"` // mail status
SendTime primitive.DateTime `bson:"send_time" gen:"autoFill"` // send time
}
```

###### 6-2.Generate dao files

```bash
% go generate ./...
```
20 changes: 10 additions & 10 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

type counter struct {
opts *options
modelName string // 模型名称
daoClassName string // DAO类名称
daoVariableName string // DAO变量名
daoPkgPath string // DAO包路径
daoPkgName string // DAO包名称
daoOutputDir string // DAO文件输出目录
daoOutputFile string // DAO文件输出名
daoPrefixName string // DAO前缀名
collectionName string // 数据库集合名
modelName string
daoClassName string
daoVariableName string
daoPkgPath string
daoPkgName string
daoOutputDir string
daoOutputFile string
daoPrefixName string
collectionName string
}

func newCounter(opts *options) *counter {
Expand All @@ -26,7 +26,7 @@ func newCounter(opts *options) *counter {
c.daoClassName = toPascalCase(c.modelName)
c.daoVariableName = toCamelCase(c.modelName)
c.daoOutputFile = fmt.Sprintf("%s.go", toFileName(c.modelName, c.opts.fileNameStyle))
c.collectionName = toUnderScoreCase(c.modelName)
c.collectionName = toUnderscoreCase(c.modelName)

dir := strings.TrimSuffix(opts.daoDir, "/")

Expand Down
Loading

0 comments on commit f4aa6ec

Please sign in to comment.