Class IdentifiableMongoDbPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
java.lang.Object
org.pipservices3.mongodb.persistence.MongoDbPersistence<T>
org.pipservices3.mongodb.persistence.IdentifiableMongoDbPersistence<T,K>
- All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable
,org.pipservices3.commons.config.IReconfigurable
,org.pipservices3.commons.refer.IReferenceable
,org.pipservices3.commons.run.ICleanable
,org.pipservices3.commons.run.IClosable
,org.pipservices3.commons.run.IOpenable
public class IdentifiableMongoDbPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K> extends MongoDbPersistence<T>
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 this._collection
and this._model
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)
- 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: 5 sec)
- auto_reconnect: (optional) enable auto reconnection (default: true)
- max_page_size: (optional) maximum page size (default: 100)
- debug: (optional) enable debug output (default: false).
### 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 ###
class MyMongoDbPersistence extends MongoDbPersistence<MyData, String> {
public MyMongoDbPersistence() {
super("mydata", MyData.class);
}
private Bson composeFilter(FilterParams filter) {
filter = filter != null ? filter : new FilterParams();
ArrayList<Bson> filters = new ArrayList<Bson>();
String name = filter.getAsNullableString('name');
if (name != null)
filters.add(Filters.eq("name", name));
return Filters.and(filters);
}
public getPageByFilter(String correlationId, FilterParams filter, PagingParams paging) {
super.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null);
}
}
MyMongoDbPersistence persistence = new MyMongoDbPersistence();
persistence.configure(ConfigParams.fromTuples(
"host", "localhost",
"port", 27017
));
persitence.open("123");
persistence.create("123", new MyData("1", "ABC"));
DataPage<MyData> mydata = persistence.getPageByFilter(
"123",
FilterParams.fromTuples("name", "ABC"),
null,
null);
System.out.println(mydata.getData().toString()); // Result: { id: "1", name: "ABC" }
persistence.deleteById("123", "1");
...
-
Field Summary
Fields Modifier and Type Field Description protected int
_maxPageSize
Fields inherited from class org.pipservices3.mongodb.persistence.MongoDbPersistence
_collection, _collectionName, _connection, _connectionResolver, _database, _documentClass, _lock, _logger, _options
-
Constructor Summary
Constructors Constructor Description IdentifiableMongoDbPersistence(String collectionName, Class<T> documentClass)
Creates a new instance of the persistence component. -
Method Summary
Modifier and Type Method Description void
configure(org.pipservices3.commons.config.ConfigParams config)
Configures component by passing configuration parameters.protected void
deleteByFilter(String correlationId, org.bson.conversions.Bson filter)
Deletes data items that match to a given filter.T
deleteById(String correlationId, K id)
Deleted a data item by it's unique id.void
deleteByIds(String correlationId, K[] ids)
Deletes multiple data items by their unique ids.protected List<T>
getListByFilter(String correlationId, org.bson.conversions.Bson filter, org.bson.conversions.Bson sort)
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.List<T>
getListByIds(String correlationId, K[] ids)
Gets a list of data items retrieved by given unique ids.T
getOneById(String correlationId, K id)
Gets a data item by its unique id.protected T
getOneRandom(String correlationId, org.bson.conversions.Bson filter)
Gets a random item from items that match to a given filter.protected org.pipservices3.commons.data.DataPage<T>
getPageByFilter(String correlationId, org.bson.conversions.Bson filter, org.pipservices3.commons.data.PagingParams paging, org.bson.conversions.Bson sort)
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.T
set(String correlationId, T newItem)
Sets a data item.T
update(String correlationId, T newItem)
Updates a data item.T
updatePartially(String correlationId, K id, org.pipservices3.commons.data.AnyValueMap data)
Updates only few selected fields in a data item.T
create(String correlationId, T item)
Creates a data item.Methods inherited from class org.pipservices3.mongodb.persistence.MongoDbPersistence
checkOpened, clear, close, isOpen, open, setReferences
-
Field Details
-
_maxPageSize
protected int _maxPageSize
-
-
Constructor Details
-
IdentifiableMongoDbPersistence
Creates a new instance of the persistence component.- Parameters:
collectionName
- (optional) a collection name.documentClass
- the default class to cast any documents returned from the database into
-
-
Method Details
-
configure
public void configure(org.pipservices3.commons.config.ConfigParams config)Configures component by passing configuration parameters.- Specified by:
configure
in interfaceorg.pipservices3.commons.config.IConfigurable
- Overrides:
configure
in classMongoDbPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>>
- Parameters:
config
- configuration parameters to be set.
-
getPageByFilter
protected org.pipservices3.commons.data.DataPage<T> getPageByFilter(String correlationId, org.bson.conversions.Bson filter, org.pipservices3.commons.data.PagingParams paging, org.bson.conversions.Bson sort) throws org.pipservices3.commons.errors.ApplicationExceptionGets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter JSON objectpaging
- (optional) paging parameterssort
- (optional) sorting JSON object- Returns:
- data page of results by filter.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getListByFilter
protected List<T> getListByFilter(String correlationId, org.bson.conversions.Bson filter, org.bson.conversions.Bson sort) throws org.pipservices3.commons.errors.ApplicationExceptionGets a list of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public getListByFilter method from child class that receives FilterParams and converts them into a filter function.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter JSON objectsort
- (optional) sorting JSON object- Returns:
- a data list of result by filter.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getListByIds
public List<T> getListByIds(String correlationId, K[] ids) throws org.pipservices3.commons.errors.ApplicationExceptionGets a list of data items retrieved by given unique ids.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.ids
- ids of data items to be retrieved- Returns:
- a data list of results by ids.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getOneById
public T getOneById(String correlationId, K id) throws org.pipservices3.commons.errors.ApplicationExceptionGets a data item by its unique id.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.id
- an id of data item to be retrieved.- Returns:
- a data item by id.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getOneRandom
protected T getOneRandom(String correlationId, org.bson.conversions.Bson filter) throws org.pipservices3.commons.errors.ApplicationExceptionGets a random item from items that match to a given filter. This method shall be called by a public getOneRandom method from child class that receives FilterParams and converts them into a filter function.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter JSON object- Returns:
- a random item by filter.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
create
public T create(String correlationId, T item) throws org.pipservices3.commons.errors.ApplicationExceptionCreates a data item.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.item
- an item to be created.- Returns:
- created item.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
set
public T set(String correlationId, T newItem) throws org.pipservices3.commons.errors.ApplicationExceptionSets a data item. If the data item exists it updates it, otherwise it create a new data item.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.newItem
- a item to be set.- Returns:
- updated item.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
update
public T update(String correlationId, T newItem) throws org.pipservices3.commons.errors.ApplicationExceptionUpdates a data item.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.newItem
- an item to be updated.- Returns:
- updated item.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
updatePartially
public T updatePartially(String correlationId, K id, org.pipservices3.commons.data.AnyValueMap data) throws org.pipservices3.commons.errors.ApplicationExceptionUpdates only few selected fields in a data item.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.id
- an id of data item to be updated.data
- a map with fields to be updated.- Returns:
- updated item.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
deleteById
public T deleteById(String correlationId, K id) throws org.pipservices3.commons.errors.ApplicationExceptionDeleted a data item by it's unique id.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.id
- an id of the item to be deleted- Returns:
- deleted item.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
deleteByFilter
protected void deleteByFilter(String correlationId, org.bson.conversions.Bson filter) throws org.pipservices3.commons.errors.ApplicationExceptionDeletes data items that match to a given filter. This method shall be called by a public deleteByFilter method from child class that receives FilterParams and converts them into a filter function.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter JSON object.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
deleteByIds
public void deleteByIds(String correlationId, K[] ids) throws org.pipservices3.commons.errors.ApplicationExceptionDeletes multiple data items by their unique ids.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.ids
- ids of data items to be deleted.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-