From 7fb59ee694e31bc50613bc90875e182304b0e560 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 21 Sep 2016 00:26:22 -0400 Subject: [PATCH] Isomorphic --- README.md | 19 ++++---- lib/angel_mongo.dart | 92 +----------------------------------- lib/model.dart | 13 +++++ lib/mongo_service.dart | 2 +- lib/mongo_service_typed.dart | 2 +- lib/services.dart | 81 +++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 7 files changed, 109 insertions(+), 102 deletions(-) create mode 100644 lib/model.dart create mode 100644 lib/services.dart diff --git a/README.md b/README.md index e361f9cd..eb3b6faa 100644 --- a/README.md +++ b/README.md @@ -19,20 +19,21 @@ This library exposes three main classes: `Model`, `MongoService` and `MongoTyped `Model` is class with no real functionality; however, it represents a basic MongoDB document, and your services should host inherited classes. ```dart -@Hooked() class User extends Model { String username; String password; } -Db db = new Db('mongodb://localhost:27017/local'); -await db.open(); - -app.use('/api/users', new MongoTypedService(db.collection("users"))); - -app.service('api/users').afterCreated.listen((HookedServiceEvent event) { - print("New user: ${event.result}"); -}); +main() async { + Db db = new Db('mongodb://localhost:27017/local'); + await db.open(); + + app.use('/api/users', new MongoTypedService(db.collection("users"))); + + app.service('api/users').afterCreated.listen((event) { + print("New user: ${event.result}"); + }); +} ``` ## MongoService diff --git a/lib/angel_mongo.dart b/lib/angel_mongo.dart index 3924e2f6..654e4b6d 100644 --- a/lib/angel_mongo.dart +++ b/lib/angel_mongo.dart @@ -1,92 +1,4 @@ library angel_mongo; -import 'dart:async'; -import 'dart:mirrors'; -import 'package:angel_framework/angel_framework.dart'; -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'; - -/// A data type that can be serialized to MongoDB. -class Model { - /// This instance's ID. - String id; - - /// The time at which this instance was created. - DateTime createdAt; - - /// The time at which this instance was last updated. - DateTime updatedAt; -} - -Map _transformId(Map doc) { - Map result = mergeMap([doc]); - result['id'] = doc['_id']; - - return result..remove('_id'); -} - -_lastItem(DbCollection collection, Function _jsonify, [Map params]) async { - return (await (await collection - .find(where.sortBy('\$natural', descending: true))) - .toList()) - .map((x) => _jsonify(x, params)) - .first; -} - -ObjectId _makeId(id) { - try { - return (id is ObjectId) ? id : new ObjectId.fromHexString(id.toString()); - } catch (e) { - throw new AngelHttpException.BadRequest(); - } -} - -Map _removeSensitive(Map data) { - return data - ..remove('id') - ..remove('_id') - ..remove('createdAt') - ..remove('updatedAt'); -} - -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'] != null && 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 is String) { - result = result.and(where.eq(key, params[key])); - } - } - - return result; -} +export 'model.dart'; +export 'services.dart'; \ No newline at end of file diff --git a/lib/model.dart b/lib/model.dart new file mode 100644 index 00000000..cf2d5152 --- /dev/null +++ b/lib/model.dart @@ -0,0 +1,13 @@ +library angel_mongo.model; + +/// A data type that can be serialized to MongoDB. +class Model { + /// This instance's ID. + String id; + + /// The time at which this instance was created. + DateTime createdAt; + + /// The time at which this instance was last updated. + DateTime updatedAt; +} \ No newline at end of file diff --git a/lib/mongo_service.dart b/lib/mongo_service.dart index b38a1515..b2d35308 100644 --- a/lib/mongo_service.dart +++ b/lib/mongo_service.dart @@ -1,4 +1,4 @@ -part of angel_mongo; +part of angel_mongo.services; /// Manipulates data from MongoDB as Maps. class MongoService extends Service { diff --git a/lib/mongo_service_typed.dart b/lib/mongo_service_typed.dart index 4a995ad1..736d3f17 100644 --- a/lib/mongo_service_typed.dart +++ b/lib/mongo_service_typed.dart @@ -1,4 +1,4 @@ -part of angel_mongo; +part of angel_mongo.services; /// Manipulates data from MongoDB by serializing BSON from and deserializing BSON to a target class. class MongoTypedService extends Service { diff --git a/lib/services.dart b/lib/services.dart new file mode 100644 index 00000000..0b2f3cc5 --- /dev/null +++ b/lib/services.dart @@ -0,0 +1,81 @@ +library angel_mongo.services; + +import 'dart:async'; +import 'dart:mirrors'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:json_god/json_god.dart' as god; +import 'package:merge_map/merge_map.dart'; +import 'package:mongo_dart/mongo_dart.dart'; +import 'model.dart'; + +part 'mongo_service.dart'; + +part 'mongo_service_typed.dart'; + +Map _transformId(Map doc) { + Map result = mergeMap([doc]); + result['id'] = doc['_id']; + + return result..remove('_id'); +} + +_lastItem(DbCollection collection, Function _jsonify, [Map params]) async { + return (await (await collection + .find(where.sortBy('\$natural', descending: true))) + .toList()) + .map((x) => _jsonify(x, params)) + .first; +} + +ObjectId _makeId(id) { + try { + return (id is ObjectId) ? id : new ObjectId.fromHexString(id.toString()); + } catch (e) { + throw new AngelHttpException.BadRequest(); + } +} + +Map _removeSensitive(Map data) { + return data + ..remove('id') + ..remove('_id') + ..remove('createdAt') + ..remove('updatedAt'); +} + +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'] != null && 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 is String) { + result = result.and(where.eq(key, params[key])); + } + } + + return result; +} diff --git a/pubspec.yaml b/pubspec.yaml index 6225bea7..6d453fab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_mongo -version: 1.0.0-dev+1 +version: 1.0.0-dev+2 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_mongo