From f4186e4c9605bbb31496d6f976f35c33d72036c2 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 22 Feb 2017 17:02:17 -0500 Subject: [PATCH] 1.0.1 --- README.md | 6 ++++-- lib/src/rethink_service.dart | 38 +++++++++++++++++++++--------------- pubspec.yaml | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4ed3e14b..54f7c5db 100644 --- a/README.md +++ b/README.md @@ -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 Does the same as above, but serializes to and from a target class using `package:json_god` and its support for reflection. diff --git a/lib/src/rethink_service.dart b/lib/src/rethink_service.dart index 98e98097..aad667cc 100644 --- a/lib/src/rethink_service.dart +++ b/lib/src/rethink_service.dart @@ -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) { - return data.keys.fold({}, (map, k) => map..[k.toString()] = data[k]); + _squeeze(data) { + if (data is Map) + return data.keys.fold({}, (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; } diff --git a/pubspec.yaml b/pubspec.yaml index aab3367b..ae6900c0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 environment: