2021-01-17 03:29:50 +00:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
2021-02-26 03:27:00 +00:00
|
|
|
"github.com/sunboyy/repogen/internal/spec"
|
2021-01-17 03:29:50 +00:00
|
|
|
)
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
const constructorBody = ` return &{{.ImplStructName}}{
|
2021-01-17 03:29:50 +00:00
|
|
|
collection: collection,
|
2022-10-18 12:37:50 +00:00
|
|
|
}`
|
2021-01-17 03:29:50 +00:00
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
type constructorBodyData struct {
|
2021-01-23 13:03:16 +00:00
|
|
|
ImplStructName string
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:39:20 +00:00
|
|
|
const insertOneTemplate = ` result, err := r.collection.InsertOne(arg0, arg1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result.InsertedID, nil`
|
|
|
|
|
|
|
|
const insertManyTemplate = ` var entities []interface{}
|
|
|
|
for _, model := range arg1 {
|
|
|
|
entities = append(entities, model)
|
|
|
|
}
|
|
|
|
result, err := r.collection.InsertMany(arg0, entities)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result.InsertedIDs, nil`
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
type mongoFindTemplateData struct {
|
|
|
|
EntityType string
|
|
|
|
QuerySpec querySpec
|
2021-05-03 07:12:58 +00:00
|
|
|
Sorts []findSort
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 07:12:58 +00:00
|
|
|
type findSort struct {
|
2021-02-26 03:27:00 +00:00
|
|
|
BsonTag string
|
|
|
|
Ordering spec.Ordering
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:12:58 +00:00
|
|
|
func (s findSort) OrderNum() int {
|
2021-02-26 03:27:00 +00:00
|
|
|
if s.Ordering == spec.OrderingAscending {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 03:29:50 +00:00
|
|
|
const findOneTemplate = ` var entity {{.EntityType}}
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := r.collection.FindOne(arg0, bson.M{
|
2021-01-26 13:23:52 +00:00
|
|
|
{{.QuerySpec.Code}}
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.FindOne().SetSort(bson.M{
|
|
|
|
{{range $index, $element := .Sorts}} "{{$element.BsonTag}}": {{$element.OrderNum}},
|
|
|
|
{{end}} })).Decode(&entity); err != nil {
|
2021-01-17 03:29:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &entity, nil`
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
const findManyTemplate = ` cursor, err := r.collection.Find(arg0, bson.M{
|
2021-01-19 12:35:54 +00:00
|
|
|
{{.QuerySpec.Code}}
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{
|
|
|
|
{{range $index, $element := .Sorts}} "{{$element.BsonTag}}": {{$element.OrderNum}},
|
|
|
|
{{end}} }))
|
2021-01-17 03:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*{{.EntityType}}
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-17 03:29:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil`
|
2021-01-26 13:23:52 +00:00
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
type mongoUpdateTemplateData struct {
|
2021-02-24 12:02:57 +00:00
|
|
|
Update update
|
|
|
|
QuerySpec querySpec
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateOneTemplate = ` result, err := r.collection.UpdateOne(arg0, bson.M{
|
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
}, bson.M{
|
2021-02-24 12:02:57 +00:00
|
|
|
{{.Update.Code}}
|
2021-01-27 12:15:25 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return result.MatchedCount > 0, err`
|
|
|
|
|
|
|
|
const updateManyTemplate = ` result, err := r.collection.UpdateMany(arg0, bson.M{
|
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
}, bson.M{
|
2021-02-24 12:02:57 +00:00
|
|
|
{{.Update.Code}}
|
2021-01-27 12:15:25 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(result.MatchedCount), err`
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
type mongoDeleteTemplateData struct {
|
|
|
|
QuerySpec querySpec
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
const deleteOneTemplate = ` result, err := r.collection.DeleteOne(arg0, bson.M{
|
2021-01-26 13:23:52 +00:00
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return result.DeletedCount > 0, nil`
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
const deleteManyTemplate = ` result, err := r.collection.DeleteMany(arg0, bson.M{
|
2021-01-26 13:23:52 +00:00
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(result.DeletedCount), nil`
|
2021-02-06 11:05:47 +00:00
|
|
|
|
|
|
|
type mongoCountTemplateData struct {
|
|
|
|
QuerySpec querySpec
|
|
|
|
}
|
|
|
|
|
|
|
|
const countTemplate = ` count, err := r.collection.CountDocuments(arg0, bson.M{
|
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(count), nil`
|