This commit is contained in:
thosakwe 2017-02-22 20:03:30 -05:00
parent e4a697e9c7
commit fbad320e83
4 changed files with 56 additions and 49 deletions

View file

@ -1,6 +1,6 @@
# angel_mongo
[![version 1.1.3](https://img.shields.io/badge/pub-1.1.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mongo)
[![version 1.1.4](https://img.shields.io/badge/pub-1.1.4-brightgreen.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)
MongoDB-enabled services for the Angel framework.

View file

@ -8,12 +8,65 @@ class MongoService extends Service {
///
/// `false` by default.
final bool allowRemoveAll;
/// If set to `true`, parameters in `req.query` are applied to the database query.
final bool allowQuery;
final bool debug;
MongoService(DbCollection this.collection,
{this.allowRemoveAll: false, this.debug: true})
{this.allowRemoveAll: false, this.allowQuery: true, this.debug: true})
: super();
SelectorBuilder _makeQuery([Map params_]) {
Map params = params_ ?? {};
params = params..remove('provider');
SelectorBuilder result = where.exists('_id');
// You can pass a SelectorBuilder as 'query';
if (params['query'] is SelectorBuilder) {
return params['query'];
}
for (var key in params.keys) {
if (key == r'$sort' ||
key == r'$query' &&
(allowQuery == true || !params.containsKey('provider'))) {
if (params[key] is Map) {
// If they send a map, then we'll sort by every key in the map
for (String fieldName in params[key].keys.where((x) => x is String)) {
var sorter = params[key][fieldName];
if (sorter is num) {
result = result.sortBy(fieldName, descending: sorter == -1);
} else if (sorter is String) {
result = result.sortBy(fieldName, descending: sorter == "-1");
} else if (sorter is SelectorBuilder) {
result = result.and(sorter);
}
}
} else if (params[key] is String && key == r'$sort') {
// If they send just a string, then we'll sort
// by that, ascending
result = result.sortBy(params[key]);
}
} else if (key == 'query' &&
(allowQuery == true || !params.containsKey('provider'))) {
Map query = params[key];
query.forEach((key, v) {
var value = v is Map ? _filterNoQuery(v) : v;
if (!_NO_QUERY.contains(key) &&
value is! RequestContext &&
value is! ResponseContext) {
result = result.and(where.eq(key, value));
}
});
}
}
return result;
}
_jsonify(Map doc, [Map params]) {
Map result = {};

View file

@ -48,52 +48,6 @@ Map _removeSensitive(Map data) {
const List<String> _NO_QUERY = const ['__requestctx', '__responsectx'];
SelectorBuilder _makeQuery([Map params_]) {
Map params = params_ ?? {};
params = params..remove('provider');
SelectorBuilder result = where.exists('_id');
// You can pass a SelectorBuilder as 'query';
if (params['query'] is SelectorBuilder) {
return params['query'];
}
for (var key in params.keys) {
if (key == r'$sort' || key == r'$query') {
if (params[key] is Map) {
// If they send a map, then we'll sort by every key in the map
for (String fieldName in params[key].keys.where((x) => x is String)) {
var sorter = params[key][fieldName];
if (sorter is num) {
result = result.sortBy(fieldName, descending: sorter == -1);
} else if (sorter is String) {
result = result.sortBy(fieldName, descending: sorter == "-1");
} else if (sorter is SelectorBuilder) {
result = result.and(sorter);
}
}
} else if (params[key] is String && key == r'$sort') {
// If they send just a string, then we'll sort
// by that, ascending
result = result.sortBy(params[key]);
}
} else if (key == 'query') {
Map query = params[key];
query.forEach((key, v) {
var value = v is Map ? _filterNoQuery(v) : v;
if (!_NO_QUERY.contains(key) &&
value is! RequestContext &&
value is! ResponseContext) {
result = result.and(where.eq(key, value));
}
});
}
}
return result;
}
Map _filterNoQuery(Map data) {
return data.keys.fold({}, (map, key) {
var value = data[key];

View file

@ -1,5 +1,5 @@
name: angel_mongo
version: 1.1.3
version: 1.1.4
description: MongoDB-enabled services for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_mongo