MongoDbPersistence class

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 this.client or this.collection properties.

Configuration parameters

  • collection: (optional) MongoDB collection name
  • connection:
    • discovery_key: (optional) a key to retrieve the connection from connect.idiscovery.html IDiscovery]
    • host: host name or IP address
    • port: port number (default: 27017)
    • uri: resource URI or connection string with all parameters in it
  • credential:
    • store_key: (optional) a key to retrieve the credentials from auth.icredentialstore.html 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)
    • reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000)
    • max_page_size: (optional) maximum page size (default: 100)
    • replica_set: (optional) name of replica set
    • ssl: (optional) enable SSL connection (default: false)
    • auth_source: (optional) authentication source
    • 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> {

  MyMongoDbPersistence():base('mydata');

  Future<MyData> getByName(String correlationId, String name) {
      var filter = {'name': name};
      var query = mngquery.SelectorBuilder();
      var selector = <String, dynamic>{};
      selector[r'$query'] = filter;
      query.raw(selector);
      var item = await collection.findOne(filter);
      if (item == null) {
        return null;
      }
      item = convertToPublic(item);
      var instance = MyData.fromJson(item);
      return instance;
  });

  Future<MyData> set(String correlatonId, MyData item) {
    if (item == null) {
      return null;
    }
    var jsonMap = json.decode(json.encode(item));
    // Assign unique id
    if (jsonMap['id'] == null) {
      jsonMap['id'] = IdGenerator.nextLong();
    }
    convertFromPublic(jsonMap);
    var filter = {r'$query': {'name': jsonMap['name']}};
    var result = await collection.findAndModify(
        query: filter, update: jsonMap, returnNew: true, upsert: true);
    if (result != null) {
        convertToPublic(result);
        var newItem = MyData.fromJson(result);;
        return newItem;
    }
    return null;
  }

}

var persistence = MyMongoDbPersistence();
persistence.configure(ConfigParams.fromTuples([
    'host', 'localhost',
    'port', 27017
]));

await persitence.open('123');

await persistence.set('123', { name: 'ABC' });
var item = await persistence.getByName('123', 'ABC');
print(item);         // Result: { name: 'ABC' }
Implementers

Constructors

MongoDbPersistence([String collection])
Creates a new instance of the persistence component. [...]

Properties

client ↔ Db
The MongoDB connection object.
read / write
collection ↔ DbCollection
The MongoDb database object. The MongoDb collection object.
read / write
collectionName ↔ String
The MongoDB colleciton object.
read / write
connection MongoDbConnection
The MongoDB connection component.
read / write
databaseName ↔ String
The MongoDB database name.
read / write
dependencyResolver ↔ DependencyResolver
The dependency resolver.
read / write
logger ↔ CompositeLogger
The logger.
read / write
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

clear(String correlationId) → Future
Clears component state. [...]
close(String correlationId) → Future
Closes component and frees used resources. [...]
configure(ConfigParams config) → void
Configures component by passing configuration parameters. [...]
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 [...]
isOpen() → bool
Checks if the component is opened. [...]
open(String correlationId) → Future
Opens the component. [...]
setReferences(IReferences references) → void
Sets references to dependent components. [...]
unsetReferences() → void
Unsets (clears) previously set references to dependent components.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited