Isomorphic
This commit is contained in:
parent
43ef011025
commit
7fb59ee694
7 changed files with 109 additions and 102 deletions
19
README.md
19
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<User>(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<User>(db.collection("users")));
|
||||
|
||||
app.service('api/users').afterCreated.listen((event) {
|
||||
print("New user: ${event.result}");
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
## MongoService
|
||||
|
|
|
@ -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';
|
13
lib/model.dart
Normal file
13
lib/model.dart
Normal file
|
@ -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;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
part of angel_mongo;
|
||||
part of angel_mongo.services;
|
||||
|
||||
/// Manipulates data from MongoDB as Maps.
|
||||
class MongoService extends Service {
|
||||
|
|
|
@ -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<T> extends Service {
|
||||
|
|
81
lib/services.dart
Normal file
81
lib/services.dart
Normal file
|
@ -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;
|
||||
}
|
|
@ -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 <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_mongo
|
||||
|
|
Loading…
Reference in a new issue