IdentifiableMongoDbPersistence<T extends IIdentifiable<K> , K> class
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 IDiscoveryhost
: host name or IP addressport
: 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 ICredentialStoreusername
: (optional) user namepassword
: (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)reconnect_interval
: (optional) reconnection interval in milliseconds (default: 1000)max_page_size
: (optional) maximum page size (default: 100)replica_set
: (optional) name of replica setssl
: (optional) enable SSL connection (default: false)auth_source
: (optional) authentication sourceauth_user
: (optional) authentication user nameauth_password
: (optional) authentication user passworddebug
: (optional) enable debug output (default: false).
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
class MyMongoDbPersistence extends MongoDbPersistence<MyData, String> {
MyMongoDbPersistence(): base('mydata', new MyDataMongoDbSchema());
dynamic _composeFilter(FilterParams) {
filter = filter ?? new FilterParams();
var criteria = [];
var name = filter.getAsNullableString('name');
if (name != null)
criteria.add({'name': name });
return criteria.isNotNul ? {r'$and': criteria } : null;
}
Future<DataPage<MyData>> getPageByFilter(String correlationId, FilterParams filter, PagingParams paging) async {
return base.getPageByFilter(correlationId, _composeFilter(filter), paging, null);
}
}
var persistence = MyMongoDbPersistence();
persistence.configure(ConfigParams.fromTuples([
'host', 'localhost',
'port', 27017
]));
await persitence.open('123');
var item = await persistence.create('123', { 'id': '1', 'name': 'ABC' });
var page = await persistence.getPageByFilter('123', FilterParams.fromTuples(['name', 'ABC']), null);
print(page.data); // Result: { id: '1', name: 'ABC' }
item = await persistence.deleteById('123', '1');
- Inheritance
- Object
- MongoDbPersistence<
T> - IdentifiableMongoDbPersistence
- Implemented types
Constructors
- IdentifiableMongoDbPersistence(String collection)
- Creates a new instance of the persistence component. [...]
Properties
- client ↔ Db
-
The MongoDB connection object.
read / write, inherited
- collection ↔ DbCollection
-
The MongoDb database object.
The MongoDb collection object.
read / write, inherited
- collectionName ↔ String
-
The MongoDB colleciton object.
read / write, inherited
- connection ↔ MongoDbConnection
-
The MongoDB connection component.
read / write, inherited
- databaseName ↔ String
-
The MongoDB database name.
read / write, inherited
- dependencyResolver ↔ DependencyResolver
-
The dependency resolver.
read / write, inherited
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- logger ↔ CompositeLogger
-
The logger.
read / write, inherited
- maxPageSize ↔ int
-
read / write, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Methods
-
clear(
String correlationId) → Future -
Clears component state. [...]
inherited
-
close(
String correlationId) → Future -
Closes component and frees used resources. [...]
inherited
-
configure(
ConfigParams config) → void -
Configures component by passing configuration parameters. [...]
override
-
convertFromPublic(
dynamic item, {bool createUid: false}) → Map< String, dynamic> -
Convert object value from public to internal format. [...]
inherited
-
convertFromPublicPartial(
Map< String, dynamic> item) → Map<String, dynamic> -
Converts the given object from the public partial format. [...]
inherited
-
convertToPublic(
Map< String, dynamic> item) → dynamic -
Converts object value from internal to public format. [...]
inherited
-
create(
String correlationId, T item) → Future< T> -
Creates a data item. [...]
override
-
deleteByFilterEx(
String correlationId, Map< String, dynamic> filter) → Future -
Deletes data items that match to a given filter. [...]
inherited
-
deleteById(
String correlationId, K id) → Future< T> -
Deleted a data item by it's unique id. [...]
override
-
deleteByIds(
String correlationId, List< K> ids) → Future - Deletes multiple data items by their unique ids. [...]
-
ensureIndex(
dynamic keys, {String key, bool unique: false, bool sparse: false, bool background: false, bool dropDups: false, Map< String, dynamic> partialFilterExpression, String name}) → void -
Adds index definition to create it on opening [...]
inherited
-
getCountByFilterEx(
String correlationId, Map< String, dynamic> filter) → Future<int> -
Gets a count of data items retrieved by a given filter. [...]
inherited
-
getListByFilterEx(
String correlationId, Map< String, dynamic> filter, Map<String, dynamic> sort) → Future<List< T> > -
Gets a list of data items retrieved by a given filter and sorted according to sort parameters. [...]
inherited
-
getListByIds(
String correlationId, List< K> ids) → Future<List< T> > - Gets a list of data items retrieved by given unique ids. [...]
-
getOneById(
String correlationId, K id) → Future< T> -
Gets a data item by its unique id. [...]
override
-
getOneRandom(
String correlationId, Map< String, dynamic> filter) → Future<T> -
Gets a random item from items that match to a given filter. [...]
inherited
-
getPageByFilterEx(
String correlationId, Map< String, dynamic> filter, PagingParams paging, Map<String, dynamic> sort) → Future<DataPage< T> > -
Gets a page of data items retrieved by a given filter and sorted according to sort parameters. [...]
inherited
-
isOpen(
) → bool -
Checks if the component is opened. [...]
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
open(
String correlationId) → Future -
Opens the component. [...]
inherited
-
set(
String correlationId, T item) → Future< T> -
Sets a data item. If the data item exists it updates it,
otherwise it create a new data item. [...]
override
-
setReferences(
IReferences references) → void -
Sets references to dependent components. [...]
inherited
-
toString(
) → String -
Returns a string representation of this object.
inherited
-
unsetReferences(
) → void -
Unsets (clears) previously set references to dependent components.
inherited
-
update(
String correlationId, T item) → Future< T> -
Updates a data item. [...]
override
-
updatePartially(
String correlationId, K id, AnyValueMap data) → Future< T> - Updates only few selected fields in a data item. [...]
Operators
-
operator ==(
Object other) → bool -
The equality operator. [...]
inherited