persistence

package
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IMongoDbPersistenceOverrides

type IMongoDbPersistenceOverrides[T any] interface {
	DefineSchema()
	ConvertFromPublic(item T) (map[string]any, error)
	ConvertFromPublicPartial(item T) (map[string]any, error)
	ConvertToPublic(item any) (T, error)
}

type IdentifiableMongoDbPersistence

type IdentifiableMongoDbPersistence[T any, K any] struct {
	*MongoDbPersistence[T]
	// contains filtered or unexported fields
}

IdentifiableMongoDbPersistence is abstract persistence component that stores data in MongoDB and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.

In basic scenarios child classes shall only override GetPageByFilter, GetListByFilter or DeleteByFilter operations with specific filter function. All other operations can be used out of the box.

In complex scenarios child classes can implement additional operations by accessing c.Collection properties.

Configuration parameters:
	- collection:                  (optional) MongoDB collection name
	- connection(s):
		- discovery_key:             (optional) a key to retrieve the connection from IDiscovery
		- host:                      host name or IP address
		- port:                      port number (default: 27017)
		- database:                  database name
		- uri:                       resource URI or connection string with all parameters in it
	- credential(s):
		- store_key:                 (optional) a key to retrieve the credentials from ICredentialStore
		- username:                  (optional) user name
		- password:                  (optional) user password
	- options:
		- max_pool_size:             (optional) maximum connection pool size (default: 2)
		- keep_alive:                (optional) enable connection keep alive (default: true)
		- connect_timeout:           (optional) connection timeout in milliseconds (default: 5000)
		- socket_timeout:            (optional) socket timeout in milliseconds (default: 360000)
		- auto_reconnect:            (optional) enable auto reconnection (default: true) (not used)
		- reconnect_interval:        (optional) reconnection interval in milliseconds (default: 1000) (not used)
		- max_page_size:             (optional) maximum page size (default: 100)
		- replica_set:               (optional) name of replica set
		- ssl:                       (optional) enable SSL connection (default: false) (not implements in this release)
		- auth_source:               (optional) authentication source
		- debug:                     (optional) enable debug output (default: false). (not used)

References:
	- *:logger:*:*:1.0           (optional) ILogger components to pass log messages components to pass log messages
	- *:discovery:*:*:1.0        (optional) IDiscovery services
	- *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials

Example:

type MyIdentifiableMongoDbPersistence struct {
	*persist.IdentifiableMongoDbPersistence[test_persistence.Dummy, string]
}

func NewMyIdentifiableMongoDbPersistence() *MyIdentifiableMongoDbPersistence {
	c := &MyIdentifiableMongoDbPersistence{}
	c.IdentifiableMongoDbPersistence = persist.InheritIdentifiableMongoDbPersistence[test_persistence.Dummy, string](c, "dummies")
	return c
}

func composeFilter(filter cdata.FilterParams) any {
	filterObj := bson.M{}

	if name, ok := filter.GetAsNullableString("name"); ok {
		filterObj = bson.M{"name": name}
	}

	return filterObj
}

func (c *MyIdentifiableMongoDbPersistence) GetPageByFilter(ctx context.Context, correlationId string, filter cdata.FilterParams, paging cdata.PagingParams) (page cdata.DataPage[test_persistence.Dummy], err error) {
	return c.IdentifiableMongoDbPersistence.GetPageByFilter(ctx, correlationId, composeFilter(filter), paging,
		bson.M{"key": -1}, nil)
}

func main() {
	persistence := NewMyIdentifiableMongoDbPersistence()
	persistence.Configure(context.Background(), config.NewConfigParamsFromTuples(
		"host", "localhost",
		"port", 27017,
	))

	_ = persistence.Open(context.Background(), "123")
	page, err := persistence.GetPageByFilter(context.Background(), "123", *cdata.NewFilterParamsFromTuples("name", "ABC"), *cdata.NewEmptyPagingParams())
	fmt.Println(page) // Result: { id: "1", name: "ABC" }

	err = persistence.DeleteByFilter(context.Background(), "123", "1")
}

func InheritIdentifiableMongoDbPersistence

func InheritIdentifiableMongoDbPersistence[T any, K any](overrides IMongoDbPersistenceOverrides[T], collection string) *IdentifiableMongoDbPersistence[T, K]

InheritIdentifiableMongoDbPersistence is creates a new instance of the persistence component.

Parameters:
	- collection string (optional) a collection name.
Returns: *IdentifiableMongoDbPersistence[T, K] new created IdentifiableMongoDbPersistence component

func (*IdentifiableMongoDbPersistence[T, K]) Configure

func (c *IdentifiableMongoDbPersistence[T, K]) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure is configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config  *cconf.ConfigParams configuration parameters to be set.

func (*IdentifiableMongoDbPersistence[T, K]) Create

func (c *IdentifiableMongoDbPersistence[T, K]) Create(ctx context.Context, correlationId string,
	item T) (result T, err error)

Create was creates a data item.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- item any an item to be created.
Returns: result any, err error created item and error, if they are occurred

func (*IdentifiableMongoDbPersistence[T, K]) DeleteById

func (c *IdentifiableMongoDbPersistence[T, K]) DeleteById(ctx context.Context, correlationId string,
	id K) (item T, err error)

DeleteById is deleted a data item by it's unique id.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- id K id of the item to be deleted
Returns: item T, err error deleted item and error, if they are occurred

func (*IdentifiableMongoDbPersistence[T, K]) DeleteByIds

func (c *IdentifiableMongoDbPersistence[T, K]) DeleteByIds(ctx context.Context, correlationId string,
	ids []K) error

DeleteByIds is deletes multiple data items by their unique ids.

Parameters:
	- ctx context.Context
	- correlationId string (optional) transaction id to Trace execution through call chain.
	- ids []K ids of data items to be deleted.
Returns: error or nil for success.

func (*IdentifiableMongoDbPersistence[T, K]) GetListByIds

func (c *IdentifiableMongoDbPersistence[T, K]) GetListByIds(ctx context.Context, correlationId string,
	ids []K) (items []T, err error)

GetListByIds is gets a list of data items retrieved by given unique ids.

Parameters:
	- ctx context.Context
	- correlationId  string (optional) transaction id to Trace execution through call chain.
	- ids  []K ids of data items to be retrieved
Returns: items []T, err error a data list and error, if they are occurred.

func (*IdentifiableMongoDbPersistence[T, K]) GetOneById

func (c *IdentifiableMongoDbPersistence[T, K]) GetOneById(ctx context.Context, correlationId string,
	id K) (item T, err error)

GetOneById is gets a data item by its unique id.

Parameters:
	- ctx context.Context
	- correlationId     (optional) transaction id to Trace execution through call chain.
	- id                an id of data item to be retrieved.
Returns: item T, err error a data and error, if they are occurred.

func (*IdentifiableMongoDbPersistence[T, K]) Set

func (c *IdentifiableMongoDbPersistence[T, K]) Set(ctx context.Context, correlationId string,
	item T) (result T, err error)

Set is sets a data item. If the data item exists it updates it, otherwise it create a new data item.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- item T an item to be set.
Returns: result any, err error updated item and error, if they occurred

func (*IdentifiableMongoDbPersistence[T, K]) Update

func (c *IdentifiableMongoDbPersistence[T, K]) Update(ctx context.Context, correlationId string,
	item T) (result T, err error)

Update is updates a data item.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- item T an item to be updated.
Returns: result any, err error updated item and error, if they are occurred

func (*IdentifiableMongoDbPersistence[T, K]) UpdatePartially

func (c *IdentifiableMongoDbPersistence[T, K]) UpdatePartially(ctx context.Context, correlationId string,
	id K, data cdata.AnyValueMap) (item T, err error)

UpdatePartially is updates only few selected fields in a data item.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- id K an id of data item to be updated.
	- data cdata.AnyValueMap a map with fields to be updated.
Returns: item any, err error updated item and error, if they are occurred

type MongoDbPersistence

type MongoDbPersistence[T any] struct {
	Overrides IMongoDbPersistenceOverrides[T]

	// The dependency resolver.
	DependencyResolver *crefer.DependencyResolver
	// The logger.
	Logger clog.CompositeLogger
	// The MongoDB connection component.
	Connection *conn.MongoDbConnection
	// The MongoDB connection object.
	Client *mongodrv.Client
	// The MongoDB database name.
	DatabaseName string
	// The MongoDB colleciton object.
	CollectionName string
	//  The MongoDb database object.
	Db *mongodrv.Database
	// The MongoDb collection object.
	Collection *mongodrv.Collection

	// Defines general JSON convertors
	JsonConvertor    cconv.IJSONEngine[T]
	JsonMapConvertor cconv.IJSONEngine[map[string]any]
	// contains filtered or unexported fields
}

MongoDbPersistence abstract persistence component that stores data in MongoDB using plain driver.

This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing c.Db or c.Collection properties.

Configuration parameters:
	- collection:                  (optional) MongoDB collection name
	- connection(s):
		- discovery_key:             (optional) a key to retrieve the connection from IDiscovery
		- host:                      host name or IP address
		- port:                      port number (default: 27017)
		- database:                  database name
		- uri:                       resource URI or connection string with all parameters in it
	- credential(s):
		- store_key:                 (optional) a key to retrieve the credentials from ICredentialStore
		- username:                  (optional) user name
		- password:                  (optional) user password
	- options:
		- max_pool_size:             (optional) maximum connection pool size (default: 2)
		- keep_alive:                (optional) enable connection keep alive (default: true)
		- connect_timeout:           (optional) connection timeout in milliseconds (default: 5000)
		- socket_timeout:            (optional) socket timeout in milliseconds (default: 360000)
		- auto_reconnect:            (optional) enable auto reconnection (default: true) (not used)
		- reconnect_interval:        (optional) reconnection interval in milliseconds (default: 1000) (not used)
		- max_page_size:             (optional) maximum page size (default: 100)
		- replica_set:               (optional) name of replica set
		- ssl:                       (optional) enable SSL connection (default: false) (not implements in this release)
		- auth_source:               (optional) authentication source
		- debug:                     (optional) enable debug output (default: false). (not used)
References:
	- *:logger:*:*:1.0           (optional) ILogger components to pass log messages
	- *:discovery:*:*:1.0        (optional) IDiscovery services
	- *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials

Example:

type MyMongoDbPersistence struct {
	*persistence.MongoDbPersistence[MyData]
}

func NewMyMongoDbPersistence() *MyMongoDbPersistence {
	c := &MyMongoDbPersistence{}
	c.MongoDbPersistence = persistence.InheritMongoDbPersistence[MyData](c, "my_data")
	return c
}

func (c *MyMongoDbPersistence) GetByName(ctx context.Context, correlationId string, name string) (count int64, err error) {
	return c.MongoDbPersistence.GetCountByFilter(ctx, correlationId, bson.M{"name": name})
}

func (c *MyMongoDbPersistence) Set(ctx context.Context, correlationId string,
	item MyData) (result MyData, err error) {
	var defaultValue MyData

	newItem, err := c.Overrides.ConvertFromPublic(item)
	if err != nil {
		return defaultValue, err
	}

	id := newItem["_id"]
	filter := bson.M{"_id": id}
	var options mngoptions.FindOneAndReplaceOptions
	retDoc := mngoptions.After
	options.ReturnDocument = &retDoc
	upsert := true
	options.Upsert = &upsert

	res := c.Collection.FindOneAndReplace(ctx, filter, newItem, &options)
	if err := res.Err(); err != nil {
		if errors.Is(err, mongo.ErrNoDocuments) {
			return result, nil
		}
		return result, err
	}

	c.Logger.Trace(ctx, correlationId, "Set in %s with id = %s", c.CollectionName, id)
	var docPointer map[string]any
	if err := res.Decode(&docPointer); err != nil {
		if errors.Is(err, mongo.ErrNoDocuments) {
			return result, nil
		}
		return result, err
	}

	return c.Overrides.ConvertToPublic(docPointer)
}

func main() {
	persistence := NewMyMongoDbPersistence()
	persistence.Configure(context.Background(), config.NewConfigParamsFromTuples(
		"host", "localhost",
		"port", 27017,
	))

	_ = persistence.Open(context.Background(), "123")
	persistence.Set(context.Background(), "123", MyData{Id: "123", Name: "ABC"})
	item, err := persistence.GetByName(context.Background(), "123", "ABC")
	fmt.Println(item) // Result: { name: "ABC" }
}

func InheritMongoDbPersistence

func InheritMongoDbPersistence[T any](overrides IMongoDbPersistenceOverrides[T], collection string) *MongoDbPersistence[T]

InheritMongoDbPersistence are creates a new instance of the persistence component.

Parameters:
	- overrides IMongoDbPersistenceOverrides overrided mongodb persistence
	- collection  string a collection name.

Returns: *MongoDbPersistence new created MongoDbPersistence component

func (*MongoDbPersistence[T]) Clear

func (c *MongoDbPersistence[T]) Clear(ctx context.Context, correlationId string) error

Clear method are clears component state.

Parameters:
	- ctx context.Context
	- correlationId string (optional) transaction id to trace execution through call chain.
Returns: error or nil when no errors occurred.

func (*MongoDbPersistence[T]) Close

func (c *MongoDbPersistence[T]) Close(ctx context.Context, correlationId string) error

Close methods closes component and frees used resources.

Parameters:
	- ctx context.Context
	- correlationId string (optional) transaction id to trace execution through call chain.
Returns: error or nil when no errors occured.

func (*MongoDbPersistence[T]) Configure

func (c *MongoDbPersistence[T]) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure method is configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config  *cconf.ConfigParams configuration parameters to be set.

func (*MongoDbPersistence[T]) ConvertFromPublic

func (c *MongoDbPersistence[T]) ConvertFromPublic(value T) (map[string]any, error)

ConvertFromPublic method help convert object (map) from public view by replaced "Id" to "_id" field

Parameters:
	- item *any converted item

func (*MongoDbPersistence[T]) ConvertFromPublicPartial

func (c *MongoDbPersistence[T]) ConvertFromPublicPartial(item T) (map[string]any, error)

ConvertFromPublicPartial method help convert object (map) from public view by replaced "Id" to "_id" field

Parameters:
	- item *any converted item

func (*MongoDbPersistence[T]) ConvertToPublic

func (c *MongoDbPersistence[T]) ConvertToPublic(value any) (T, error)

ConvertToPublic method is convert object (map) to public view by replaced "_id" to "Id" field

Parameters:
	- item *any converted item

func (*MongoDbPersistence[T]) Create

func (c *MongoDbPersistence[T]) Create(ctx context.Context, correlationId string, item T) (result T, err error)

Create was creates a data item.

Parameters:
	- ctx context.Context
	- correlation_id string (optional) transaction id to Trace execution through call chain.
	- item any an item to be created.
Returns: result any, err error created item and error, if they are occurred

func (*MongoDbPersistence[T]) DefineSchema

func (c *MongoDbPersistence[T]) DefineSchema()

DefineSchema for the collection. This method shall be overloaded in child classes

func (*MongoDbPersistence[T]) DeleteByFilter

func (c *MongoDbPersistence[T]) DeleteByFilter(ctx context.Context, correlationId string, filter any) error

DeleteByFilter is deletes data items that match to a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) deleteByFilter method from child class that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context
	- correlationId  string (optional) transaction id to Trace execution through call chain.
	- filter any (optional) a filter BSON object.
Returns: error or nil for success.

func (*MongoDbPersistence[T]) EnsureIndex

func (c *MongoDbPersistence[T]) EnsureIndex(keys any, options *mongoopt.IndexOptions)

EnsureIndex method are adds index definition to create it on opening

Parameters:
	- keys any index keys (fields)
	- options *mongoopt.IndexOptions index options

func (*MongoDbPersistence[T]) GetCountByFilter

func (c *MongoDbPersistence[T]) GetCountByFilter(ctx context.Context, correlationId string, filter any) (count int64, err error)

GetCountByFilter is gets a count of data items retrieved by a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetCountByFilter method from child type that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context
	- correlationId  string (optional) transaction id to Trace execution through call chain.
	- filter any
Returns: count int, err error a data count or error, if they are occurred

func (*MongoDbPersistence[T]) GetListByFilter

func (c *MongoDbPersistence[T]) GetListByFilter(ctx context.Context, correlationId string,
	filter any, sort any, sel any) (items []T, err error)

GetListByFilter is gets a list of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetListByFilter method from child type that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context
	- correlationId string (optional) transaction id to Trace execution through call chain.
	- filter any (optional) a filter BSON object
	- sort any (optional) sorting BSON object
	- select any (optional) projection BSON object
Returns: items []any, err error data list and error, if they are occurred

func (*MongoDbPersistence[T]) GetOneRandom

func (c *MongoDbPersistence[T]) GetOneRandom(ctx context.Context, correlationId string,
	filter any) (item T, err error)

GetOneRandom is gets a random item from items that match to a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) getOneRandom method from child class that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context
	- correlationId string (optional) transaction id to Trace execution through call chain.
	- filter any (optional) a filter BSON object
Returns: item any, err error random item and error, if theq are occured

func (*MongoDbPersistence[T]) GetPageByFilter

func (c *MongoDbPersistence[T]) GetPageByFilter(ctx context.Context, correlationId string,
	filter any, paging cdata.PagingParams, sort any, sel any) (page cdata.DataPage[T], err error)

GetPageByFilter is gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetPageByFilter method from child type that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context
	- correlationId  string (optional) transaction id to Trace execution through call chain.
	- filter any (optional) a filter JSON object
	- paging cdata.PagingParams (optional) paging parameters
	- sort any (optional) sorting BSON object
	- select  any (optional) projection BSON object
Returns: page cdata.DataPage[T], err error a data page or error, if they are occurred

func (*MongoDbPersistence[T]) IsOpen

func (c *MongoDbPersistence[T]) IsOpen() bool

IsOpen method is checks if the component is opened.

Returns: true if the component has been opened and false otherwise.

func (*MongoDbPersistence[T]) IsTerminated

func (c *MongoDbPersistence[T]) IsTerminated() bool

IsTerminated checks if the wee need to terminate process before close component.

Returns: true if you need terminate your processes.

func (*MongoDbPersistence[T]) Open

func (c *MongoDbPersistence[T]) Open(ctx context.Context, correlationId string) error

Open method is opens the component.

Parameters:
	- ctx context.Context
	- correlationId  string (optional) transaction id to trace execution through call chain.
Returns: error or nil when no errors occured.

func (*MongoDbPersistence[T]) SetReferences

func (c *MongoDbPersistence[T]) SetReferences(ctx context.Context, references crefer.IReferences)

SetReferences method are sets references to dependent components.

Parameters:
	- ctx context.Context
	- references crefer.IReferences references to locate the component dependencies.

func (*MongoDbPersistence[T]) UnsetReferences

func (c *MongoDbPersistence[T]) UnsetReferences()

UnsetReferences method is unsets (clears) previously set references to dependent components.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL