platform/README.md

89 lines
2.8 KiB
Markdown
Raw Normal View History

2017-02-21 16:52:55 +00:00
# rethink
2017-02-22 03:13:08 +00:00
2017-03-07 21:23:09 +00:00
[![version 1.0.5](https://img.shields.io/badge/pub-1.0.5-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink)
2017-02-22 03:13:08 +00:00
[![build status](https://travis-ci.org/angel-dart/rethink.svg?branch=master)](https://travis-ci.org/angel-dart/rethink)
2017-02-21 16:52:55 +00:00
RethinkDB-enabled services for the Angel framework.
2017-02-22 03:13:08 +00:00
# Installation
Add the following to your `pubspec.yaml`:
```yaml
dependencies:
angel_rethink: ^1.0.0
```
2017-03-02 04:29:29 +00:00
`package:rethinkdb_driver2` will be installed as well.
2017-02-22 22:02:17 +00:00
2017-02-22 03:13:08 +00:00
# Usage
2017-02-22 03:15:50 +00:00
This library exposes one class: `RethinkService`. By default, these services will even
2017-02-22 03:13:08 +00:00
listen to [changefeeds](https://www.rethinkdb.com/docs/changefeeds/ruby/) from the database,
which makes them very suitable for WebSocket use.
However, only `CREATED`, `UPDATED` and `REMOVED` events will be fired. This is technically not
a problem, as it lowers the numbers of events you have to handle on the client side. ;)
## Model
`Model` is class with no real functionality; however, it represents a basic document, and your services should host inherited classes.
Other Angel service providers host `Model` as well, so you will easily be able to modify your application if you ever switch databases.
```dart
class User extends Model {
String username;
String password;
}
main() async {
var r = new RethinkDb();
var conn = await r.connect();
app.use('/api/users', new RethinkService(conn, r.table('users')));
// Add type de/serialization if you want
app.use('/api/users', new TypedService<User>(new RethinkService(conn, r.table('users'))));
// You don't have to even use a table...
app.use('/api/pro_users', new RethinkService(conn, r.table('users').filter({'membership': 'pro'})));
app.service('api/users').afterCreated.listen((event) {
print("New user: ${event.result}");
});
}
```
## RethinkService
2017-02-22 22:02:17 +00:00
This class interacts with a `Query` (usually a table) and serializes data to and from Maps.
2017-02-22 03:13:08 +00:00
## RethinkTypedService<T>
Does the same as above, but serializes to and from a target class using `package:json_god` and its support for reflection.
## Querying
You can query these services as follows:
/path/to/service?foo=bar
The above will query the database to find records where 'foo' equals 'bar'.
The former will sort result in ascending order of creation, and so will the latter.
You can use advanced queries:
```dart
// Pass an actual query...
service.index({'query': r.table('foo').filter(...)});
// Or, a function that creates a query from a table...
service.index({'query': (table) => table.getAll('foo')});
// Or, a Map, which will be transformed into a `filter` query:
service.index({'query': {'foo': 'bar', 'baz': 'quux'}});
```
You can also apply sorting by adding a `reql` parameter on the server-side.
```dart
service.index({'reql': (query) => query.sort(...)});
```
See the tests for more usage examples.