platform/lib/services.dart

108 lines
3 KiB
Dart
Raw Normal View History

2016-09-21 04:26:22 +00:00
library angel_mongo.services;
import 'dart:async';
2016-12-03 17:39:48 +00:00
import 'dart:io';
2016-09-21 04:26:22 +00:00
import 'dart:mirrors';
import 'package:angel_framework/angel_framework.dart';
2017-02-13 01:38:24 +00:00
import 'package:angel_framework/common.dart' show Model;
2016-09-21 04:26:22 +00:00
import 'package:json_god/json_god.dart' as god;
import 'package:merge_map/merge_map.dart';
import 'package:mongo_dart/mongo_dart.dart';
part 'mongo_service.dart';
part 'mongo_service_typed.dart';
Map _transformId(Map doc) {
Map result = mergeMap([doc]);
2017-02-13 01:38:24 +00:00
result
..['id'] = doc['_id']
..remove('_id');
2016-09-21 04:26:22 +00:00
2017-02-13 01:38:24 +00:00
return result;
2016-09-21 04:26:22 +00:00
}
_lastItem(DbCollection collection, Function _jsonify, [Map params]) async {
return (await (await collection
2017-02-13 01:38:24 +00:00
.find(where.sortBy('\$natural', descending: true)))
.toList())
2016-09-21 04:26:22 +00:00
.map((x) => _jsonify(x, params))
.first;
}
ObjectId _makeId(id) {
try {
return (id is ObjectId) ? id : new ObjectId.fromHexString(id.toString());
} catch (e) {
2017-02-13 01:38:24 +00:00
throw new AngelHttpException.badRequest();
2016-09-21 04:26:22 +00:00
}
}
2017-02-13 01:38:24 +00:00
const List<String> _SENSITIVE = const ['id', '_id', 'createdAt', 'updatedAt'];
2016-09-21 04:26:22 +00:00
Map _removeSensitive(Map data) {
2017-02-13 01:38:24 +00:00
return data.keys
.where((k) => !_SENSITIVE.contains(k))
.fold({}, (map, key) => map..[key] = data[key]);
2016-09-21 04:26:22 +00:00
}
2017-02-13 01:38:24 +00:00
const List<String> _NO_QUERY = const ['__requestctx', '__responsectx'];
2016-09-21 04:26:22 +00:00
SelectorBuilder _makeQuery([Map params_]) {
Map params = params_ ?? {};
params = params..remove('provider');
SelectorBuilder result = where.exists('_id');
// You can pass a SelectorBuilder as 'query';
2017-02-13 01:38:24 +00:00
if (params['query'] is SelectorBuilder) {
2016-09-21 04:26:22 +00:00
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]);
}
2017-02-13 01:38:24 +00:00
} 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));
}
});
2016-09-21 04:26:22 +00:00
}
}
return result;
}
2017-02-13 01:38:24 +00:00
Map _filterNoQuery(Map data) {
return data.keys.fold({}, (map, key) {
var value = data[key];
if (_NO_QUERY.contains(key) ||
value is RequestContext ||
value is ResponseContext) return map;
if (key is! Map) return map..[key] = value;
return map..[key] = _filterNoQuery(value);
});
}