/
imessage.go
46 lines (43 loc) · 1.35 KB
/
imessage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package model
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
type Imessage struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
Topic string `bson:"topic" json:"topic"`
Rule string `bson:"rule" json:"rule"`
Description string `bson:"description" json:"description"`
Projects []primitive.ObjectID `bson:"projects" json:"projects"`
CreateTime time.Time `bson:"create_time" json:"create_time" farker:"-"`
UpdateTime time.Time `bson:"update_time" json:"update_time" farker:"-"`
}
func SetImessages(ctx context.Context, db *mongo.Database) (err error) {
var ns []string
if ns, err = db.ListCollectionNames(ctx, bson.M{"name": "imessages"}); err != nil {
return
}
var jsonSchema bson.D
if err = LoadJsonSchema("imessage", &jsonSchema); err != nil {
return
}
if len(ns) == 0 {
option := options.CreateCollection().SetValidator(jsonSchema)
if err = db.CreateCollection(ctx, "imessages", option); err != nil {
return
}
} else {
if err = db.RunCommand(ctx, bson.D{
{"collMod", "imessages"},
{"validator", jsonSchema},
{"validationLevel", "strict"},
}).Err(); err != nil {
return
}
}
return
}