Skip to content

Commit

Permalink
Lint clean-up and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgiofran committed Aug 3, 2020
1 parent 35b9680 commit 8862a1e
Show file tree
Hide file tree
Showing 33 changed files with 526 additions and 543 deletions.
2 changes: 1 addition & 1 deletion changelog.md → CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### 0.4.1-dev.2.2

* Partial lint clean-up
* Lint clean-up

### 0.4.1-dev.2.1

Expand Down
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Mongo-dart - MongoDB driver for Dart programming language.
# Mongo-dart - MongoDB driver for Dart programming language

[![Pub](https://img.shields.io/pub/v/mongo_dart.svg)](https://pub.dartlang.org/packages/mongo_dart)
[![Build Status](https://travis-ci.org/mongo-dart/mongo_dart.svg?branch=master)](https://travis-ci.org/mongo-dart/mongo_dart)


Server-side driver library for MongoDb implemented in pure Dart.

## Basic usage
Expand All @@ -18,8 +17,7 @@ Server-side driver library for MongoDb implemented in pure Dart.

### Querying


Method `find` returns stream of maps and accept query parameters, usually build by fluent API query builder
Method `find` returns stream of maps and accept query parameters, usually build by fluent API query builder
provided by [mongo_dart_query](https://github.com/vadimtsushko/mongo_dart_query) as top level getter `where`

```dart
Expand All @@ -32,7 +30,7 @@ provided by [mongo_dart_query](https://github.com/vadimtsushko/mongo_dart_query)
await coll
.find(where.gt("my_field", 995).sortBy('my_field'))
.forEach((v) => print(v));
//....
await coll.find(where.sortBy('itemId').skip(300).limit(25)).toList();
Expand All @@ -46,7 +44,6 @@ Method `findOne` take the same parameter and returns `Future` of just one map (m
val = await coll.findOne(where.eq("my_field", 17).fields(['str_field','my_field']));
```


Take notice in these samples that unlike mongo shell such parameters as projection (`fields`), `limit` and `skip`
are passed as part of regular query through query builder

Expand All @@ -71,7 +68,7 @@ You can update whole document with method `save`
await coll.save(v1);
```

or you can perform field level updates with method `update` and top level getter `modify` for ModifierBuilder fluent API
or you can perform field level updates with method `update` and top level getter `modify` for ModifierBuilder fluent API

```dart
Expand All @@ -85,13 +82,11 @@ or you can perform field level updates with method `update` and top level getter
students.remove(where.id(id));
/// or, to remove all documents from collection
students.remove();
```

students.remove();
Simple app on base of [JSON ZIPS dataset] (http://media.mongodb.org/zips.json)
```

Simple app on base of [JSON ZIPS dataset](http://media.mongodb.org/zips.json)

```dart
import 'package:mongo_dart/mongo_dart.dart';
Expand Down Expand Up @@ -159,4 +154,3 @@ main() async {
- [Recent change notes](https://github.com/vadimtsushko/mongo_dart/blob/master/changelog.md)

- Additional [examples](https://github.com/vadimtsushko/mongo_dart/tree/master/example) and [tests](https://github.com/vadimtsushko/mongo_dart/tree/master/test)

80 changes: 32 additions & 48 deletions lib/src/database/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class _UriParameters {
class Db {
final MONGO_DEFAULT_PORT = 27017;
final _log = Logger('Db');
final List<String> _uriList = List<String>();
final List<String> _uriList = <String>[];

State state = State.INIT;
String databaseName;
Expand All @@ -134,6 +134,7 @@ class Db {
AuthenticationScheme _authenticationScheme;
ReadPreference readPreference = ReadPreference.primary;

@override
String toString() => 'Db($databaseName,$_debugInfo)';

/// Db constructor expects [valid mongodb URI] (http://www.mongodb.org/display/DOCS/Connections).
Expand Down Expand Up @@ -205,7 +206,7 @@ class Db {
_authenticationScheme = AuthenticationScheme.MONGODB_CR;
} else {
throw MongoDartError(
"Provided authentication scheme is not supported : $authenticationSchemeName");
'Provided authentication scheme is not supported : $authenticationSchemeName');
}
}

Expand All @@ -220,27 +221,21 @@ class Db {
throw MongoDartError('Db is in the wrong state: $state');
}

if (connection == null) {
connection = _masterConnectionVerified;
}
connection ??= _masterConnectionVerified;

return connection.query(queryMessage);
});
}

executeMessage(MongoMessage message, WriteConcern writeConcern,
void executeMessage(MongoMessage message, WriteConcern writeConcern,
{_Connection connection}) {
if (state != State.OPEN) {
throw MongoDartError('DB is not open. $state');
}

if (connection == null) {
connection = _masterConnectionVerified;
}
connection ??= _masterConnectionVerified;

if (writeConcern == null) {
writeConcern = _writeConcern;
}
writeConcern ??= _writeConcern;

connection.execute(message, writeConcern == WriteConcern.ERRORS_IGNORED);
}
Expand All @@ -253,10 +248,9 @@ class Db {

connection ??= _masterConnectionVerified;

MongoModernMessage response =
await connection.executeModernMessage(message);
var response = await connection.executeModernMessage(message);

Section section = response.sections.firstWhere((Section _section) =>
var section = response.sections.firstWhere((Section _section) =>
_section.payloadType == MongoModernMessage.basePayloadType);
return section.payload.content;
}
Expand All @@ -281,24 +275,22 @@ class Db {

Future<Map<String, dynamic>> executeDbCommand(MongoMessage message,
{_Connection connection}) async {
if (connection == null) {
connection = _masterConnectionVerified;
}
connection ??= _masterConnectionVerified;

Completer<Map<String, dynamic>> result = Completer();
var result = Completer<Map<String, dynamic>>();

var replyMessage = await connection.query(message);
var firstRepliedDocument = replyMessage.documents[0];
var errorMessage = "";
var errorMessage = '';

if (replyMessage.documents.isEmpty) {
errorMessage =
"Error executing Db command, documents are empty $replyMessage";
'Error executing Db command, documents are empty $replyMessage';

print("Error: $errorMessage");
print('Error: $errorMessage');

var m = Map<String, dynamic>();
m["errmsg"] = errorMessage;
var m = <String, dynamic>{};
m['errmsg'] = errorMessage;

result.completeError(m);
} else if (documentIsNotAnError(firstRepliedDocument)) {
Expand Down Expand Up @@ -333,16 +325,14 @@ class Db {
[Map<String, dynamic> selector = const {}, WriteConcern writeConcern]) {
return Future.sync(() {
executeMessage(
MongoRemoveMessage("$databaseName.$collectionName", selector),
MongoRemoveMessage('$databaseName.$collectionName', selector),
writeConcern);
return _getAcknowledgement(writeConcern: writeConcern);
});
}

Future<Map<String, dynamic>> getLastError([WriteConcern writeConcern]) {
if (writeConcern == null) {
writeConcern = _writeConcern;
}
writeConcern ??= _writeConcern;
return executeDbCommand(
DbCommand.createGetLastErrorCommand(this, writeConcern));
}
Expand Down Expand Up @@ -377,27 +367,27 @@ class Db {
/// Analogue to shell's `show dbs`. Helper for `listDatabases` mongodb command.
Future<List> listDatabases() async {
var commandResult = await executeDbCommand(
DbCommand.createQueryAdminCommand({"listDatabases": 1}));
DbCommand.createQueryAdminCommand({'listDatabases': 1}));

var result = [];

for (var each in commandResult["databases"]) {
result.add(each["name"]);
for (var each in commandResult['databases']) {
result.add(each['name']);
}

return result;
}

Stream<Map<String, dynamic>> _listCollectionsCursor(
[Map<String, dynamic> filter = const {}]) {
if (this._masterConnection.serverCapabilities.listCollections) {
if (_masterConnection.serverCapabilities.listCollections) {
return ListCollectionsCursor(this, filter).stream;
} else {
// Using system collections (pre v3.0 API)
Map<String, dynamic> selector = {};
Map selector = <String, dynamic>{};
// If we are limiting the access to a specific collection name
if (filter.containsKey('name')) {
selector["name"] = "${this.databaseName}.${filter['name']}";
selector['name'] = "${databaseName}.${filter['name']}";
}
return Cursor(
this,
Expand All @@ -416,10 +406,10 @@ class Db {
}

Stream<Map<String, dynamic>> _collectionsInfoCursor([String collectionName]) {
Map<String, dynamic> selector = {};
var selector = <String, dynamic>{};
// If we are limiting the access to a specific collection name
if (collectionName != null) {
selector["name"] = "${this.databaseName}.$collectionName";
selector['name'] = '${databaseName}.$collectionName';
}
// Return Cursor
return Cursor(this,
Expand Down Expand Up @@ -541,11 +531,9 @@ class Db {
if (partialFilterExpression != null) {
selector['partialFilterExpression'] = partialFilterExpression;
}
if (name == null) {
name = _createIndexName(keys);
}
name ??= _createIndexName(keys);
selector['name'] = name;
MongoInsertMessage insertMessage = MongoInsertMessage(
var insertMessage = MongoInsertMessage(
'$databaseName.${DbCommand.SYSTEM_INDEX_COLLECTION}', [selector]);
await executeMessage(insertMessage, _writeConcern);
return getLastError();
Expand All @@ -558,7 +546,7 @@ class Db {
}

if (key != null) {
keys = Map();
keys = {};
keys['$key'] = 1;
}

Expand All @@ -581,9 +569,7 @@ class Db {
keys = _setKeys(key, keys);
var indexInfos = await collection(collectionName).getIndexes();

if (name == null) {
name = _createIndexName(keys);
}
name ??= _createIndexName(keys);

if (indexInfos.any((info) => info['name'] == name) ||
// For compatibility reasons, old indexes where created with
Expand Down Expand Up @@ -613,16 +599,14 @@ class Db {
if (!_masterConnection.serverCapabilities.supportsOpMsg) {
return <String, Object>{};
}
ServerStatusOperation operation =
var operation =
ServerStatusOperation(this, options: options);
return operation.execute();
}

Future<Map<String, dynamic>> _getAcknowledgement(
{WriteConcern writeConcern}) {
if (writeConcern == null) {
writeConcern = _writeConcern;
}
writeConcern ??= _writeConcern;

if (writeConcern == WriteConcern.ERRORS_IGNORED) {
return Future.value({'ok': 1.0});
Expand Down
Loading

0 comments on commit 8862a1e

Please sign in to comment.