This commit is contained in:
thosakwe 2017-02-22 17:02:17 -05:00
parent 228b413ef0
commit f4186e4c96
3 changed files with 27 additions and 19 deletions

View file

@ -1,6 +1,6 @@
# rethink
[![version 1.0.0](https://img.shields.io/badge/pub-1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink)
[![version 1.0.1](https://img.shields.io/badge/pub-1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink)
[![build status](https://travis-ci.org/angel-dart/rethink.svg?branch=master)](https://travis-ci.org/angel-dart/rethink)
RethinkDB-enabled services for the Angel framework.
@ -13,6 +13,8 @@ dependencies:
angel_rethink: ^1.0.0
```
`package:rethinkdb_driver` will be installed as well.
# Usage
This library exposes one class: `RethinkService`. By default, these services will even
listen to [changefeeds](https://www.rethinkdb.com/docs/changefeeds/ruby/) from the database,
@ -50,7 +52,7 @@ main() async {
```
## RethinkService
This class interacts with a `Table` (from `package:rethinkdb_driver`) and serializes data to and from Maps.
This class interacts with a `Query` (usually a table) and serializes data to and from Maps.
## RethinkTypedService<T>
Does the same as above, but serializes to and from a target class using `package:json_god` and its support for reflection.

View file

@ -38,17 +38,18 @@ class RethinkService extends Service {
this.listenForChanges: true})
: super() {}
RqlQuery _getQuery(RqlQuery query, Map params) {
RqlQuery buildQuery(RqlQuery initialQuery, Map params) {
if (params != null)
params['broadcast'] =
params.containsKey('broadcast') ? params['broadcast'] : false;
params['broadcast'] = params.containsKey('broadcast')
? params['broadcast']
: (listenForChanges != true);
var q = _getQueryInner(query, params);
var q = _getQueryInner(initialQuery, params);
if (params?.containsKey('reql') == true && params['reql'] is QueryCallback)
q = params['reql'](q);
return q ?? query;
return q ?? initialQuery;
}
RqlQuery _getQueryInner(RqlQuery query, Map params) {
@ -93,15 +94,22 @@ class RethinkService extends Service {
return result;
}
Map _serialize(data) {
_serialize(data) {
if (data is Map)
return data;
else if (data is Iterable)
return data.map(_serialize).toList();
else
return god.serializeObject(data);
}
Map _squeeze(Map data) {
_squeeze(data) {
if (data is Map)
return data.keys.fold<Map>({}, (map, k) => map..[k.toString()] = data[k]);
else if (data is Iterable)
return data.map(_squeeze).toList();
else
return data;
}
void onHooked(HookedService hookedService) {
@ -138,13 +146,13 @@ class RethinkService extends Service {
@override
Future index([Map params]) async {
var query = _getQuery(table, params);
var query = buildQuery(table, params);
return await _sendQuery(query);
}
@override
Future read(id, [Map params]) async {
var query = _getQuery(table.get(id?.toString()), params);
var query = buildQuery(table.get(id?.toString()), params);
var found = await _sendQuery(query);
print('Found for $id: $found');
@ -157,13 +165,11 @@ class RethinkService extends Service {
@override
Future create(data, [Map params]) async {
if (table is! Table)
throw new StateError(
'RethinkServices can only create data within tables.');
if (table is! Table) throw new AngelHttpException.methodNotAllowed();
var d = _serialize(data);
var q = table as Table;
var query = _getQuery(q.insert(_squeeze(d)), params);
var query = buildQuery(q.insert(_squeeze(d)), params);
return await _sendQuery(query);
}
@ -172,7 +178,7 @@ class RethinkService extends Service {
@override
Future update(id, data, [Map params]) async {
var query = _getQuery(table.get(id?.toString()), params).update(data);
var query = buildQuery(table.get(id?.toString()), params).update(data);
await _sendQuery(query);
return await read(id, params);
}
@ -186,7 +192,7 @@ class RethinkService extends Service {
return await _sendQuery(table.delete());
} else {
var prior = await read(id, params);
var query = _getQuery(table.get(id), params).delete();
var query = buildQuery(table.get(id), params).delete();
await _sendQuery(query);
return prior;
}

View file

@ -1,5 +1,5 @@
name: angel_rethink
version: 1.0.0
version: 1.0.1
description: RethinkDB-enabled services for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
environment: