Skip to content

Commit

Permalink
release version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Nov 21, 2022
1 parent b422ab9 commit b25e392
Show file tree
Hide file tree
Showing 12 changed files with 889 additions and 143 deletions.
14 changes: 14 additions & 0 deletions example/dao/counter/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package counter

import (
"example/dao/counter/internal"
"go.mongodb.org/mongo-driver/mongo"
)

type Counter struct {
*internal.Counter
}

func NewCounter(db *mongo.Database) *Counter {
return &Counter{Counter: internal.NewCounter(db)}
}
76 changes: 76 additions & 0 deletions example/dao/counter/internal/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// --------------------------------------------------------------------------------------------
// The following code is automatically generated by the gen-mongo-dao tool.
// Please do not modify this code manually to avoid being overwritten in the next generation.
// For more tool details, please click the link to view https://github.com/dobyte/gen-mongo-dao
// --------------------------------------------------------------------------------------------

package internal

import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type Counter struct {
Columns *Columns
Database *mongo.Database
Collection *mongo.Collection
}

type Model struct {
ID string `bson:"_id"`
Value int64 `bson:"value"`
}

type Columns struct {
ID string
Value string
}

var counterColumns = &Columns{
ID: "_id",
Value: "value",
}

func NewCounter(db *mongo.Database) *Counter {
return &Counter{
Columns: counterColumns,
Database: db,
Collection: db.Collection("counter"),
}
}

// Incr 自增值
func (dao *Counter) Incr(ctx context.Context, key string, incr ...int) (int64, error) {
var (
upsert = true
returnDocument = options.After
counter = &Model{}
value = 1
)

if len(incr) > 0 {
if incr[0] == 0 {
return 0, errors.New("invalid increment value")
}
value = incr[0]
}

rst := dao.Collection.FindOneAndUpdate(ctx, bson.M{
dao.Columns.ID: key,
}, bson.M{"": bson.M{
dao.Columns.Value: value,
}}, &options.FindOneAndUpdateOptions{
Upsert: &upsert,
ReturnDocument: &returnDocument,
})

if err := rst.Decode(counter); err != nil {
return 0, err
}

return counter.Value, nil
}
34 changes: 18 additions & 16 deletions example/dao/internal/mail.go → example/dao/mail/internal/mail.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------
// The following code is automatically generated by the gen-mongo-dao tool.
// Please do not modify this code manually to avoid being overwritten in the next generation.
// For more tool details, please click the link to view https://github.com/dobyte/gen-mongo-dao
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------

package internal

Expand All @@ -16,21 +16,22 @@ import (
"time"
)

type FilterFunc func(cols *MailColumns) interface{}
type UpdateFunc func(cols *MailColumns) interface{}
type FindOneOptionsFunc func(cols *MailColumns) *options.FindOneOptions
type FindManyOptionsFunc func(cols *MailColumns) *options.FindOptions
type UpdateOptionsFunc func(cols *MailColumns) *options.UpdateOptions
type DeleteOptionsFunc func(cols *MailColumns) *options.DeleteOptions
type InsertOneOptionsFunc func(cols *MailColumns) *options.InsertOneOptions
type InsertManyOptionsFunc func(cols *MailColumns) *options.InsertManyOptions
type FilterFunc func(cols *Columns) interface{}
type UpdateFunc func(cols *Columns) interface{}
type FindOneOptionsFunc func(cols *Columns) *options.FindOneOptions
type FindManyOptionsFunc func(cols *Columns) *options.FindOptions
type UpdateOptionsFunc func(cols *Columns) *options.UpdateOptions
type DeleteOptionsFunc func(cols *Columns) *options.DeleteOptions
type InsertOneOptionsFunc func(cols *Columns) *options.InsertOneOptions
type InsertManyOptionsFunc func(cols *Columns) *options.InsertManyOptions

type Mail struct {
Columns *MailColumns
Columns *Columns
Database *mongo.Database
Collection *mongo.Collection
}

type MailColumns struct {
type Columns struct {
ID string
Title string
Content string
Expand All @@ -41,7 +42,7 @@ type MailColumns struct {
SendTime string
}

var mailColumns = &MailColumns{
var mailColumns = &Columns{
ID: "_id",
Title: "title",
Content: "content",
Expand All @@ -55,6 +56,7 @@ var mailColumns = &MailColumns{
func NewMail(db *mongo.Database) *Mail {
return &Mail{
Columns: mailColumns,
Database: db,
Collection: db.Collection("mail"),
}
}
Expand All @@ -65,7 +67,7 @@ func (dao *Mail) InsertOne(ctx context.Context, model *mail.Mail, optionsFunc ..
return nil, errors.New("model is nil")
}

if err := dao.autofill(model); err != nil {
if err := dao.autofill(ctx, model); err != nil {
return nil, err
}

Expand All @@ -87,7 +89,7 @@ func (dao *Mail) InsertMany(ctx context.Context, models []*mail.Mail, optionsFun
documents := make([]interface{}, 0, len(models))
for i := range models {
model := models[i]
if err := dao.autofill(model); err != nil {
if err := dao.autofill(ctx, model); err != nil {
return nil, err
}
documents = append(documents, model)
Expand Down Expand Up @@ -209,7 +211,7 @@ func (dao *Mail) DeleteMany(ctx context.Context, filterFunc FilterFunc, optionsF
}

// autofill when inserting data
func (dao *Mail) autofill(model *mail.Mail) error {
func (dao *Mail) autofill(ctx context.Context, model *mail.Mail) error {
if model.ID.IsZero() {
model.ID = primitive.NewObjectID()
}
Expand Down
4 changes: 2 additions & 2 deletions example/dao/mail.go → example/dao/mail/mail.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dao
package mail

import (
"example/dao/internal"
"example/dao/mail/internal"
"go.mongodb.org/mongo-driver/mongo"
)

Expand Down
Loading

0 comments on commit b25e392

Please sign in to comment.