diff --git a/core/interfaces.go b/core/interfaces.go index 21ae258a..c98a5e9b 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -98,6 +98,9 @@ type Storage interface { RegisterStorageListener(listener storage.Listener) PerformTransaction(func(context storage.TransactionContext) error, int64) error + FindGlobalConfig(context storage.TransactionContext, key string) (*model.GlobalConfigEntry, error) + SaveGlobalConfig(context storage.TransactionContext, globalConfig model.GlobalConfigEntry) error + FindConfig(configType string, appID string, orgID string) (*model.Config, error) FindConfigByID(id string) (*model.Config, error) FindConfigs(configType *string) ([]model.Config, error) diff --git a/core/model/globalConfig.go b/core/model/globalConfig.go index 8c0f90a6..e167a226 100644 --- a/core/model/globalConfig.go +++ b/core/model/globalConfig.go @@ -16,6 +16,7 @@ package model // GlobalConfigEntry represents global config entry. type GlobalConfigEntry struct { + ID string `bson:"_id"` Key string `bson:"key"` - Text map[string]interface{} `bson:"data"` + Data map[string]interface{} `bson:"data"` } diff --git a/driven/storage/adapter.go b/driven/storage/adapter.go index 5d496946..a610faeb 100644 --- a/driven/storage/adapter.go +++ b/driven/storage/adapter.go @@ -195,6 +195,33 @@ func (a *Adapter) loadConfigs() ([]model.Config, error) { return configs, nil } +// FindGlobalConfig finds global config by key +func (a *Adapter) FindGlobalConfig(context TransactionContext, key string) (*model.GlobalConfigEntry, error) { + var err error + + filter := bson.D{ + bson.E{Key: "key", Value: key}, + } + + var globalConfig model.GlobalConfigEntry + err = a.db.globalConfigs.FindOneWithContext(context, filter, &globalConfig, nil) + if err != nil { + return nil, err + } + + return &globalConfig, nil +} + +// FindGlobalConfig saves global config +func (a *Adapter) SaveGlobalConfig(context TransactionContext, globalConfig model.GlobalConfigEntry) error { + filter := bson.D{primitive.E{Key: "_id", Value: globalConfig.ID}} + err := a.db.globalConfigs.ReplaceOneWithContext(context, filter, globalConfig, nil) + if err != nil { + return err + } + return nil +} + // FindConfig finds the config for the specified type, appID, and orgID func (a *Adapter) FindConfig(configType string, appID string, orgID string) (*model.Config, error) { return a.getCachedConfig("", configType, appID, orgID) diff --git a/driven/storage/adapter_example.go b/driven/storage/adapter_example.go index 9fdd7b92..8c27b6b3 100644 --- a/driven/storage/adapter_example.go +++ b/driven/storage/adapter_example.go @@ -28,7 +28,7 @@ func (a *Adapter) FindExample(orgID string, appID string, id string) (*model.Exa filter := bson.M{"org_id": orgID, "app_id": appID, "_id": id} var data *model.Example - err := a.db.examples.FindOne(a.context, filter, &data, nil) + err := a.db.examples.FindOneWithContext(a.context, filter, &data, nil) if err != nil { return nil, errors.WrapErrorAction(logutils.ActionFind, model.TypeExample, filterArgs(filter), err) } diff --git a/driven/storage/collection.go b/driven/storage/collection.go index 6a1f0ecb..ffc23e9d 100644 --- a/driven/storage/collection.go +++ b/driven/storage/collection.go @@ -54,7 +54,11 @@ func (collWrapper *collectionWrapper) Find(ctx context.Context, filter interface return err } -func (collWrapper *collectionWrapper) FindOne(ctx context.Context, filter interface{}, result interface{}, findOptions *options.FindOneOptions) error { +func (collWrapper *collectionWrapper) FindOne(filter interface{}, result interface{}, findOptions *options.FindOneOptions) error { + return collWrapper.FindOneWithContext(context.Background(), filter, result, findOptions) +} + +func (collWrapper *collectionWrapper) FindOneWithContext(ctx context.Context, filter interface{}, result interface{}, findOptions *options.FindOneOptions) error { if ctx == nil { ctx = context.Background() } @@ -76,7 +80,11 @@ func (collWrapper *collectionWrapper) FindOne(ctx context.Context, filter interf return nil } -func (collWrapper *collectionWrapper) ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error { +func (collWrapper *collectionWrapper) ReplaceOne(filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error { + return collWrapper.ReplaceOneWithContext(context.Background(), filter, replacement, replaceOptions) +} + +func (collWrapper *collectionWrapper) ReplaceOneWithContext(ctx context.Context, filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error { if ctx == nil { ctx = context.Background() }