Add UpdateSet, Relaxed in client, Fix tests
This commit is contained in:
52
main.go
52
main.go
@@ -40,6 +40,10 @@ type IMongoClient interface {
|
||||
|
||||
// GetCollection returns the requested collection within an account. It will create it if it doesn't exist.
|
||||
GetCollection(database, name string) *mongo.Collection
|
||||
|
||||
SetRelaxed()
|
||||
|
||||
SetStrict()
|
||||
}
|
||||
|
||||
type Timeseries struct {
|
||||
@@ -81,6 +85,16 @@ type MongoClient struct {
|
||||
DBPrefix string
|
||||
// Registry holds critical information about collection's structure, like schema and indexes
|
||||
Registry map[string]*CollectionDefinition
|
||||
// Relaxed if set to true will not enforce schema
|
||||
Relaxed bool
|
||||
}
|
||||
|
||||
func (c *MongoClient) SetRelaxed() {
|
||||
c.Relaxed = true
|
||||
}
|
||||
|
||||
func (c *MongoClient) SetStrict() {
|
||||
c.Relaxed = false
|
||||
}
|
||||
|
||||
func (c *MongoClient) GetIdPrefix(name string) string {
|
||||
@@ -114,6 +128,13 @@ func (c *MongoClient) AddDefinition(data map[string]any) {
|
||||
if len(cd.Name) > 0 {
|
||||
c.Registry[cd.Name] = &cd
|
||||
}
|
||||
|
||||
if cd.Views != nil {
|
||||
for k, _ := range cd.Views {
|
||||
log.Printf("Registering alias %s to %s", k, cd.Name)
|
||||
c.Registry[k] = &cd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MongoClient) DropDatabase_DANGER(database string) bool {
|
||||
@@ -139,6 +160,9 @@ func (c *MongoClient) GetCollection(database, name string) *mongo.Collection {
|
||||
|
||||
db := c.Client.Database(database)
|
||||
|
||||
// To make sure we create the right collection when dealing with aliases.
|
||||
actualName := name
|
||||
|
||||
// 1. List existing collections
|
||||
names, err := db.ListCollectionNames(context.TODO(), bson.D{})
|
||||
|
||||
@@ -149,32 +173,41 @@ func (c *MongoClient) GetCollection(database, name string) *mongo.Collection {
|
||||
}
|
||||
|
||||
// 2. If collection exist return it, otherwise create it and then return it
|
||||
if slices.Contains(names, name) {
|
||||
return db.Collection(name)
|
||||
if slices.Contains(names, actualName) {
|
||||
return db.Collection(actualName)
|
||||
} else {
|
||||
opts := options.CreateCollection()
|
||||
|
||||
// maybe get from schema
|
||||
cdef, ok := c.Registry[name]
|
||||
cdef, ok := c.Registry[actualName]
|
||||
if ok {
|
||||
log.Printf("Schema found for %s; will use it", name)
|
||||
|
||||
actualName = cdef.Name
|
||||
|
||||
log.Printf("Schema found for %s; will use it", actualName)
|
||||
|
||||
ApplyTimeSeries(cdef, opts)
|
||||
ApplySchema(cdef, opts)
|
||||
} else {
|
||||
log.Printf("No schema for %s", name)
|
||||
log.Printf("No schema for %s", actualName)
|
||||
|
||||
if c.Relaxed == false {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := db.CreateCollection(context.TODO(), name, opts); err != nil {
|
||||
if err := db.CreateCollection(context.TODO(), actualName, opts); err != nil {
|
||||
log.Printf("Failed to create collection: %#v", err)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
collection := db.Collection(name)
|
||||
|
||||
collection := db.Collection(actualName)
|
||||
c.CreateIndexes(collection, cdef)
|
||||
c.CreateViews(db, cdef)
|
||||
|
||||
if c.CreateViews(db, cdef) {
|
||||
collection = db.Collection(name)
|
||||
}
|
||||
|
||||
return collection
|
||||
}
|
||||
@@ -216,6 +249,7 @@ func ApplySchema(cdef *CollectionDefinition, opts *options.CreateCollectionOptio
|
||||
var client *MongoClient = &MongoClient{
|
||||
Limit: 10,
|
||||
Registry: make(map[string]*CollectionDefinition, 0),
|
||||
Relaxed: false,
|
||||
}
|
||||
|
||||
func GetMongoClient() *MongoClient {
|
||||
|
||||
Reference in New Issue
Block a user