2021-06-18 11:10:38 +00:00
|
|
|
library angel3_mongo.services;
|
2016-09-21 04:26:22 +00:00
|
|
|
|
|
|
|
import 'dart:async';
|
2021-06-18 11:10:38 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
2021-09-25 06:32:32 +00:00
|
|
|
import 'package:belatuk_merge_map/belatuk_merge_map.dart';
|
2016-09-21 04:26:22 +00:00
|
|
|
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) {
|
2021-06-18 11:10:38 +00:00
|
|
|
var result = 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 {
|
2021-06-18 11:10:38 +00:00
|
|
|
return (id is ObjectId) ? id : ObjectId.fromHexString(id.toString());
|
2016-09-21 04:26:22 +00:00
|
|
|
} catch (e) {
|
2021-06-18 11:10:38 +00:00
|
|
|
throw AngelHttpException.badRequest();
|
2016-09-21 04:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:10:38 +00:00
|
|
|
const List<String> _sensitiveFieldNames = [
|
2018-12-10 19:19:20 +00:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2022-02-27 01:19:15 +00:00
|
|
|
const List<String> _noQuery = ['__requestctx', '__responsectx'];
|
2017-02-13 01:38:24 +00:00
|
|
|
|
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];
|
|
|
|
|
2022-02-27 01:19:15 +00:00
|
|
|
if (_noQuery.contains(key) ||
|
2017-02-13 01:38:24 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|