Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
version: "2"
linters:
enable:
- err113
- errname
- errorlint
- err113
- lll
- stylecheck
linters-settings:
lll:
tab-width: 4
- staticcheck
settings:
lll:
tab-width: 4
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type UserRepository interface {
// display name. If there is a user matches the query, it will return true.
// Error will be returned only when error occurs while accessing the
// database.
UpdateDisplayNameByID(ctx context.Context, displayName string, id primitive.ObjectID) (bool, error)
UpdateDisplayNameByID(ctx context.Context, displayName string, id bson.ObjectID) (bool, error)

// DeleteByCity deletes users that have `city` value match the parameter
// and returns the match count. The error will be returned only when an
Expand Down Expand Up @@ -123,7 +123,7 @@ A `Find` operation also has two modes like `Insert` operation: single-entity and

```go
// FindByID gets a single document by ID
FindByID(ctx context.Context, id primitive.ObjectID) (*Model, error)
FindByID(ctx context.Context, id bson.ObjectID) (*Model, error)

// FindByCity gets all documents that match city parameter
FindByCity(ctx context.Context, city string) ([]*Model, error)
Expand Down Expand Up @@ -169,7 +169,7 @@ This type of update is for changing the whole model, replacing all the fields ex

```go
// UpdateByID updates a single document by ID
UpdateByID(ctx context.Context, model *Model, id primitive.ObjectID) (bool, error)
UpdateByID(ctx context.Context, model *Model, id bson.ObjectID) (bool, error)
```

2. Fields-type update
Expand All @@ -180,7 +180,7 @@ This type of update is for changing only some fields in the model. To write this
// UpdateDisplayNameAndCityByID updates a single document with a new display name and
// city by ID
UpdateDisplayNameAndCityByID(ctx context.Context, displayName string, city string,
id primitive.ObjectID) (bool, error)
id bson.ObjectID) (bool, error)

// UpdateGenderByCity updates Gender field of documents with matching city parameter
UpdateGenderByCity(ctx context.Context, gender Gender, city string) (int, error)
Expand All @@ -191,10 +191,10 @@ The update operator will be default to `$set` operator. In case that you want to
```go
// UpdateConsentHistoryPushByID appends consentHistory to the ConsentHistory field
UpdateConsentHistoryPushByID(ctx context.Context, consentHistory ConsentHistory,
id primitive.ObjectID) (bool, error)
id bson.ObjectID) (bool, error)

// UpdateAgeIncByID increments age value by `incAge`
UpdateAgeIncByID(ctx context.Context, incAge int, id primitive.ObjectID) (bool, error)
UpdateAgeIncByID(ctx context.Context, incAge int, id bson.ObjectID) (bool, error)
```

For all types of updates, repogen determines a single-entity operation or a multiple-entity by checking the first return value. If it is of type `bool`, the method will be single-entity operation. If it is of type `int`, the method will be multiple-entity operation. For single-entity operation, the method returns true if there is a matching document. For multiple-entity operation, the integer return shows the number of matched documents.
Expand All @@ -207,7 +207,7 @@ A `Delete` operation is the very similar to `Find` operation. It has two modes.

```go
// DeleteByID deletes a single document by ID
DeleteByID(ctx context.Context, id primitive.ObjectID) (bool, error)
DeleteByID(ctx context.Context, id bson.ObjectID) (bool, error)

// DeleteByCity deletes all documents that match city parameter
DeleteByCity(ctx context.Context, city string) (int, error)
Expand Down
21 changes: 17 additions & 4 deletions examples/complex-query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

// Replace these values with your own connection option. This connection option is hard-coded for easy
Expand All @@ -23,7 +23,7 @@ var (

func init() {
// create a connection to the database
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
client, err := mongo.Connect(options.Client().ApplyURI(connectionString))
if err != nil {
panic(err)
}
Expand All @@ -37,10 +37,11 @@ func init() {
func main() {
demonstrateFindByExists()
demonstrateFindByNotExists()
demonstrateFindByContactEmail()
}

// demonstrateFindByExists shows how find method in repogen works. It receives
// query parameters through method arguments and returns matched result
// query parameters through method arguments and returns matched result.
func demonstrateFindByExists() {
users, err := userComparatorRepository.FindByContactExists(context.Background())
if err != nil {
Expand All @@ -61,3 +62,15 @@ func demonstrateFindByNotExists() {

fmt.Printf("FindByNotExists: found users = %+v\n", users)
}

// demonstrateFindByContactEmail shows how find method in repogen works. It
// receives query parameters through method arguments and returns matched
// result.
func demonstrateFindByContactEmail() {
users, err := userOtherRepository.FindByContactEmail(context.Background(), "sunboyy@gmail.com")
if err != nil {
panic(err)
}

fmt.Printf("FindByContactEmail: found users = %+v\n", users)
}
16 changes: 8 additions & 8 deletions examples/complex-query/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"

"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/v2/bson"
)

type Gender string
Expand All @@ -14,13 +14,13 @@ const (
)

type UserModel struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
Username string `bson:"username" json:"username"`
Gender Gender `bson:"gender" json:"gender"`
Age int `bson:"age" json:"age"`
City string `bson:"city" json:"city"`
Contact *UserContactModel `bson:"contact,omitempty" json:"contact"`
Banned bool `bson:"banned" json:"banned"`
ID bson.ObjectID `bson:"_id,omitempty" json:"id"`
Username string `bson:"username" json:"username"`
Gender Gender `bson:"gender" json:"gender"`
Age int `bson:"age" json:"age"`
City string `bson:"city" json:"city"`
Contact *UserContactModel `bson:"contact,omitempty" json:"contact"`
Banned bool `bson:"banned" json:"banned"`
}

type UserContactModel struct {
Expand Down
109 changes: 61 additions & 48 deletions examples/complex-query/user_comparator_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading