Friendlier to concurrency
This commit is contained in:
parent
dff4f228c0
commit
d52fd36e45
6 changed files with 45 additions and 61 deletions
|
@ -12,7 +12,7 @@
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="Dart SDK" level="application" />
|
<orderEntry type="library" name="Dart SDK" level="project" />
|
||||||
<orderEntry type="library" name="Dart Packages" level="project" />
|
<orderEntry type="library" name="Dart Packages" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
|
@ -1,7 +1,7 @@
|
||||||
# angel_mongo
|
# angel_mongo
|
||||||
|
|
||||||
[![version 1.1.5](https://img.shields.io/badge/pub-1.1.5-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mongo)
|
[![Pub](https://img.shields.io/pub/v/angel_mongo.svg)](https://pub.dartlang.org/packages/angel_mongo)
|
||||||
[![build status](https://travis-ci.org/angel-dart/mongo.svg?branch=master)](https://travis-ci.org/angel-dart/mongo)
|
[![build status](https://travis-ci.org/angel-dart/mongo.svg)](https://travis-ci.org/angel-dart/mongo)
|
||||||
|
|
||||||
MongoDB-enabled services for the Angel framework.
|
MongoDB-enabled services for the Angel framework.
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class MongoService extends Service {
|
||||||
: super();
|
: super();
|
||||||
|
|
||||||
SelectorBuilder _makeQuery([Map params_]) {
|
SelectorBuilder _makeQuery([Map params_]) {
|
||||||
Map params = params_ ?? {};
|
Map params = new Map.from(params_ ?? {});
|
||||||
params = params..remove('provider');
|
params = params..remove('provider');
|
||||||
SelectorBuilder result = where.exists('_id');
|
SelectorBuilder result = where.exists('_id');
|
||||||
|
|
||||||
|
@ -82,13 +82,6 @@ class MongoService extends Service {
|
||||||
return _transformId(result);
|
return _transformId(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printDebug(e, st, msg) {
|
|
||||||
if (debug) {
|
|
||||||
stderr.writeln('$msg ERROR: $e');
|
|
||||||
stderr.writeln(st);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List> index([Map params]) async {
|
Future<List> index([Map params]) async {
|
||||||
return await (await collection.find(_makeQuery(params)))
|
return await (await collection.find(_makeQuery(params)))
|
||||||
|
@ -96,17 +89,22 @@ class MongoService extends Service {
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const String _NONCE_KEY = '__angel__mongo__nonce__key__';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future create(data, [Map params]) async {
|
Future create(data, [Map params]) async {
|
||||||
Map item = (data is Map) ? data : god.serializeObject(data);
|
Map item = (data is Map) ? data : god.serializeObject(data);
|
||||||
item = _removeSensitive(item);
|
item = _removeSensitive(item);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
item['createdAt'] = new DateTime.now().toIso8601String();
|
String nonce = (await collection.db.getNonce())['nonce'];
|
||||||
await collection.insert(item);
|
var result = await collection.findAndModify(
|
||||||
return await _lastItem(collection, _jsonify, params);
|
query: where.eq(_NONCE_KEY, nonce),
|
||||||
|
update: item,
|
||||||
|
returnNew: true,
|
||||||
|
upsert: true);
|
||||||
|
return _jsonify(result);
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
printDebug(e, st, 'CREATE');
|
|
||||||
throw new AngelHttpException(e, stackTrace: st);
|
throw new AngelHttpException(e, stackTrace: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,49 +139,43 @@ class MongoService extends Service {
|
||||||
target is Map ? target : god.serializeObject(target),
|
target is Map ? target : god.serializeObject(target),
|
||||||
_removeSensitive(data)
|
_removeSensitive(data)
|
||||||
]);
|
]);
|
||||||
result['updatedAt'] = new DateTime.now().toIso8601String();
|
//result['updatedAt'] = new DateTime.now().toIso8601String();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await collection.update(where.id(_makeId(id)), result);
|
var modified = await collection.findAndModify(
|
||||||
result = _jsonify(result, params);
|
query: where.id(_makeId(id)), update: result, returnNew: true);
|
||||||
result['id'] = id;
|
result = _jsonify(modified, params);
|
||||||
|
result['id'] = _makeId(id).toHexString();
|
||||||
return result;
|
return result;
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
printDebug(e, st, 'MODIFY');
|
//printDebug(e, st, 'MODIFY');
|
||||||
throw new AngelHttpException(e, stackTrace: st);
|
throw new AngelHttpException(e, stackTrace: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future update(id, data, [Map params]) async {
|
Future update(id, data, [Map params]) async {
|
||||||
var target;
|
|
||||||
|
|
||||||
try {
|
|
||||||
target = await read(id, params);
|
|
||||||
} on AngelHttpException catch (e) {
|
|
||||||
if (e.statusCode == HttpStatus.NOT_FOUND)
|
|
||||||
return await create(data, params);
|
|
||||||
else
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map result = _removeSensitive(data);
|
Map result = _removeSensitive(data);
|
||||||
result['_id'] = _makeId(id);
|
result['_id'] = _makeId(id);
|
||||||
result['createdAt'] =
|
/*result['createdAt'] =
|
||||||
target is Map ? target['createdAt'] : target.createdAt;
|
target is Map ? target['createdAt'] : target.createdAt;
|
||||||
|
|
||||||
if (result['createdAt'] is DateTime)
|
if (result['createdAt'] is DateTime)
|
||||||
result['createdAt'] = result['createdAt'].toIso8601String();
|
result['createdAt'] = result['createdAt'].toIso8601String();
|
||||||
|
|
||||||
result['updatedAt'] = new DateTime.now().toIso8601String();
|
result['updatedAt'] = new DateTime.now().toIso8601String();*/
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await collection.update(where.id(_makeId(id)), result);
|
var updated = await collection.findAndModify(
|
||||||
result = _jsonify(result, params);
|
query: where.id(_makeId(id)),
|
||||||
result['id'] = id;
|
update: result,
|
||||||
|
returnNew: true,
|
||||||
|
upsert: true);
|
||||||
|
result = _jsonify(updated, params);
|
||||||
|
result['id'] = _makeId(id).toHexString();
|
||||||
return result;
|
return result;
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
printDebug(e, st, 'UPDATE');
|
//printDebug(e, st, 'UPDATE');
|
||||||
throw new AngelHttpException(e, stackTrace: st);
|
throw new AngelHttpException(e, stackTrace: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,13 +190,14 @@ class MongoService extends Service {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = await read(id, params);
|
// var result = await read(id, params);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await collection.remove(where.id(_makeId(id)).and(_makeQuery(params)));
|
var result = await collection.findAndModify(
|
||||||
return result;
|
query: where.id(_makeId(id)), remove: true);
|
||||||
|
return _jsonify(result);
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
printDebug(e, st, 'REMOVE');
|
//printDebug(e, st, 'REMOVE');
|
||||||
throw new AngelHttpException(e, stackTrace: st);
|
throw new AngelHttpException(e, stackTrace: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ part 'mongo_service.dart';
|
||||||
part 'mongo_service_typed.dart';
|
part 'mongo_service_typed.dart';
|
||||||
|
|
||||||
Map _transformId(Map doc) {
|
Map _transformId(Map doc) {
|
||||||
Map result = mergeMap([doc]);
|
Map result = new Map.from(doc);
|
||||||
result
|
result
|
||||||
..['id'] = doc['_id']
|
..['id'] = doc['_id']
|
||||||
..remove('_id');
|
..remove('_id');
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_mongo
|
name: angel_mongo
|
||||||
version: 1.1.5
|
version: 1.1.6
|
||||||
description: MongoDB-enabled services for the Angel framework.
|
description: MongoDB-enabled services for the Angel framework.
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/angel_mongo
|
homepage: https://github.com/angel-dart/angel_mongo
|
||||||
|
|
|
@ -14,19 +14,10 @@ final headers = {
|
||||||
final Map testGreeting = {'to': 'world'};
|
final Map testGreeting = {'to': 'world'};
|
||||||
|
|
||||||
wireHooked(HookedService hooked) {
|
wireHooked(HookedService hooked) {
|
||||||
hooked
|
hooked.afterAll((HookedServiceEvent event) {
|
||||||
..afterCreated.listen((HookedServiceEvent event) {
|
print("Just ${event.eventName}: ${event.result}");
|
||||||
print("Just created: ${event.result}");
|
print('Params: ${event.params}');
|
||||||
})
|
});
|
||||||
..afterModified.listen((HookedServiceEvent event) {
|
|
||||||
print("Just modified: ${event.result}");
|
|
||||||
})
|
|
||||||
..afterUpdated.listen((HookedServiceEvent event) {
|
|
||||||
print("Just updated: ${event.result}");
|
|
||||||
})
|
|
||||||
..afterRemoved.listen((HookedServiceEvent event) {
|
|
||||||
print("Just removed: ${event.result}");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
@ -92,7 +83,7 @@ main() {
|
||||||
Map read = god.deserialize(response.body);
|
Map read = god.deserialize(response.body);
|
||||||
expect(read['id'], equals(created['id']));
|
expect(read['id'], equals(created['id']));
|
||||||
expect(read['to'], equals('world'));
|
expect(read['to'], equals('world'));
|
||||||
expect(read['createdAt'], isNot(null));
|
//expect(read['createdAt'], isNot(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('modify item', () async {
|
test('modify item', () async {
|
||||||
|
@ -107,7 +98,7 @@ main() {
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
expect(modified['id'], equals(created['id']));
|
expect(modified['id'], equals(created['id']));
|
||||||
expect(modified['to'], equals('Mom'));
|
expect(modified['to'], equals('Mom'));
|
||||||
expect(modified['updatedAt'], isNot(null));
|
//expect(modified['updatedAt'], isNot(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('update item', () async {
|
test('update item', () async {
|
||||||
|
@ -122,7 +113,7 @@ main() {
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
expect(modified['id'], equals(created['id']));
|
expect(modified['id'], equals(created['id']));
|
||||||
expect(modified['to'], equals('Updated'));
|
expect(modified['to'], equals('Updated'));
|
||||||
expect(modified['updatedAt'], isNot(null));
|
//expect(modified['updatedAt'], isNot(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('remove item', () async {
|
test('remove item', () async {
|
||||||
|
@ -146,10 +137,10 @@ main() {
|
||||||
print(response.body);
|
print(response.body);
|
||||||
List<Map> queried = god.deserialize(response.body);
|
List<Map> queried = god.deserialize(response.body);
|
||||||
expect(queried.length, equals(1));
|
expect(queried.length, equals(1));
|
||||||
expect(queried[0].keys.length, equals(3));
|
expect(queried[0].keys.length, equals(2));
|
||||||
expect(queried[0]["id"], equals(world["id"]));
|
expect(queried[0]["id"], equals(world["id"]));
|
||||||
expect(queried[0]["to"], equals(world["to"]));
|
expect(queried[0]["to"], equals(world["to"]));
|
||||||
expect(queried[0]["createdAt"], equals(world["createdAt"]));
|
//expect(queried[0]["createdAt"], equals(world["createdAt"]));
|
||||||
|
|
||||||
/*response = await client.get("$url/api?\$sort.createdAt=-1");
|
/*response = await client.get("$url/api?\$sort.createdAt=-1");
|
||||||
print(response.body);
|
print(response.body);
|
||||||
|
|
Loading…
Reference in a new issue