2016-09-21 04:26:22 +00:00
|
|
|
library angel_mongo.services;
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:merge_map/merge_map.dart';
|
|
|
|
import 'package:mongo_dart/mongo_dart.dart';
|
|
|
|
|
|
|
|
part 'mongo_service.dart';
|
|
|
|
|
2018-10-18 22:58:03 +00:00
|
|
|
Map<String, dynamic> _transformId(Map<String, dynamic> doc) {
|
|
|
|
var result = new Map<String, dynamic>.from(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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:19:20 +00:00
|
|
|
const List<String> _sensitiveFieldNames = const [
|
|
|
|
'id',
|
|
|
|
'_id',
|
|
|
|
'createdAt',
|
|
|
|
'updatedAt'
|
|
|
|
];
|
2017-02-13 01:38:24 +00:00
|
|
|
|
2018-10-18 22:58:03 +00:00
|
|
|
Map<String, dynamic> _removeSensitive(Map<String, dynamic> data) {
|
2017-02-13 01:38:24 +00:00
|
|
|
return data.keys
|
2018-12-10 19:19:20 +00:00
|
|
|
.where((k) => !_sensitiveFieldNames.contains(k))
|
2017-02-13 01:38:24 +00:00
|
|
|
.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'];
|
|
|
|
|
2018-10-18 22:58:03 +00:00
|
|
|
Map<String, dynamic> _filterNoQuery(Map<String, dynamic> data) {
|
2017-02-13 01:38:24 +00:00
|
|
|
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;
|
2018-10-18 22:58:03 +00:00
|
|
|
return map..[key] = _filterNoQuery(value as Map<String, dynamic>);
|
2017-02-13 01:38:24 +00:00
|
|
|
});
|
|
|
|
}
|