pip_services3_mongodb 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 81

Pip.Services Logo #

Pip.Services MongoDB components for Dart #

This component library is a part of the Pip.Services project. It contains the following MongoDB components:

  • MongoDbConnectionResolver
  • MongoDbConnection
  • MongoDbPersistence
  • IdentifiableMongoDbPersistence

Quick Links:

Acknowledgements #

The library is created and maintained by

  • Sergey Seroukhov
  • Levichev Dmitry.

The documentation is written by

  • Mark Makarychev
  • Levichev Dmitry.

MongoDB components for Pip.Services in Dart Changelog #

1.0.0 #

  • Initial version, created by Sergey Seroukhov and Dmitry Levichev

1.0.1 #

  • Small optimization of code conversion from/to public methods, created by Dmitry Levichev

1.0.2 #

  • Fix test and CRUD methods in IdentifiableMongoDbPersistence.

1.0.3 #

  • Add getCountbyFilterEx method in IdentifiableMongodbPersisstence

1.0.4 #

  • Add suffix for method deleteByFilter in IdentifiableMongodbPersisstence

example/README.md

Examples for MongoDB persistence #

This component library is a part of the Pip.Services project. It contains the following MongoDB components:

  • MongoDbConnectionResolver
  • Example:
  class Test {
  var connectionResolver = MongoDbConnectionResolver();

  @override
  void configure(ConfigParams config) {
    connectionResolver.configure(config);
    ...
  }

   @override
  void setReferences(IReferences references) {
    connectionResolver.setReferences(references);
    ...
  }

   @override
  Future open(String correlationId) async {
    String uri;
    try {
      uri = await connectionResolver.resolve(correlationId);
    } catch (err) {
      logger.error(correlationId, ApplicationException().wrap(err),
          'Failed to resolve MongoDb connection');
    }
    ...
  }
}
  • MongoDbConnection
  • Example:
    MongoDbConnection connection;

    var mongoUri = Platform.environment['MONGO_URI'];
    var mongoHost = Platform.environment['MONGO_HOST'] ?? 'localhost';
    var mongoPort = Platform.environment['MONGO_PORT'] ?? '27017';
    var mongoDatabase = Platform.environment['MONGO_DB'] ?? 'test';
   
      var dbConfig = ConfigParams.fromTuples([
        'connection.uri',  mongoUri,
        'connection.host', mongoHost,
        'connection.port', mongoPort,
        'connection.database', mongoDatabase
      ]);

      connection = MongoDbConnection();
      connection.configure(dbConfig);

      await connection.open(null);
  • MongoDbPersistence
  • 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' }
  • IdentifiableMongoDbPersistence
  • 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'); 

In the help for each class there is a general example of its use. Also one of the quality sources are the source code for the tests.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pip_services3_mongodb: ^1.0.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pip_services3_mongodb/pip_services3_mongodb.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native on android

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on ios

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on linux

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on macos

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on windows

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-web on web

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime js

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Package not compatible with runtime native-aot

Because:

  • package:pip_services3_mongodb/pip_services3_mongodb.dart that imports:
  • package:pip_services3_mongodb/src/persistence/persistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/IdentifiableMongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbPersistence.dart that imports:
  • package:pip_services3_mongodb/src/persistence/MongoDbConnection.dart that imports:
  • package:pip_services3_mongodb/src/connect/MongoDbConnectionResolver.dart that imports:
  • package:pip_services3_components/pip_services3_components.dart that imports:
  • package:pip_services3_components/src/Component.dart that imports:
  • package:pip_services3_components/src/count/CompositeCounters.dart that imports:
  • package:pip_services3_commons/pip_services3_commons.dart that imports:
  • package:pip_services3_commons/src/validate/validate.dart that imports:
  • package:pip_services3_commons/src/validate/ProjectionParamsSchema.dart that imports:
  • package:pip_services3_commons/src/validate/ArraySchema.dart that imports:
  • package:pip_services3_commons/src/reflect/ObjectReader.dart that imports:
  • package:pip_services3_commons/src/reflect/PropertyReflector.dart that imports:
  • dart:mirrors

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
mongo_dart ^0.4.0 0.4.0
mongo_dart_query ^0.4.1 0.4.1
pip_services3_commons >=1.0.4 <2.0.0 1.0.11
pip_services3_components >=1.0.2 <2.0.0 1.0.6
pip_services3_data >=1.0.0 <2.0.0 1.0.6
Transitive dependencies
async 2.4.2
bson 0.3.2
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
fixnum 0.10.11
logging 0.11.4
meta 1.2.2 1.3.0-nullsafety
more 1.18.2 2.5.0
mustache4dart2 0.1.0
path 1.7.0
pool 1.4.0
source_span 1.7.0
sprintf 4.0.2
stack_trace 1.9.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
uuid 2.2.0
yaml 2.2.1
Dev dependencies
test >=1.14.2 <2.0.0