platform/packages/rethinkdb/README.md

97 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

# Protevus RethinkDB
2024-06-30 01:44:37 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_rethinkdb?include_prereleases)
2024-07-06 12:06:27 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/mongo/LICENSE)
2024-06-30 01:44:37 +00:00
This is RethinkDB service for Protevus framework. RethinkDB is an open-source database for the realtime web.
2024-06-30 01:44:37 +00:00
## Installation
Add the following to your `pubspec.yaml`:
```yaml
dependencies:
protevus_rethink: ^8.0.0
2024-06-30 01:44:37 +00:00
```
2024-07-06 10:15:18 +00:00
`belatuk-rethinkdb` driver will be used for connecting to RethinkDB.
2024-06-30 01:44:37 +00:00
## Usage
2024-07-06 10:15:18 +00:00
This library exposes one class: `RethinkService`. By default, these services will 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. Technically not
a problem, as it lowers the number of events that need to be handled on the client side.
2024-06-30 01:44:37 +00:00
## Model
`Model` is class with no real functionality; however, it represents a basic document, and your services should host inherited classes. Other Protevus service providers host `Model` as well, so you will easily be able to modify your application if you ever switch databases.
2024-06-30 01:44:37 +00:00
```dart
class User extends Model {
String username;
String password;
}
main() async {
2024-07-06 10:15:18 +00:00
var r = RethinkDb();
var conn = await r.connect(
db: 'testDB',
host: "localhost",
port: 28015,
user: "admin",
password: "");
app.use('/api/users', RethinkService(conn, r.table('users')));
2024-06-30 01:44:37 +00:00
// Add type de/serialization if you want
2024-07-06 10:15:18 +00:00
app.use('/api/users', TypedService<User>(RethinkService(conn, r.table('users'))));
2024-06-30 01:44:37 +00:00
// You don't have to even use a table...
2024-07-06 10:15:18 +00:00
app.use('/api/pro_users', RethinkService(conn, r.table('users').filter({'membership': 'pro'})));
2024-06-30 01:44:37 +00:00
app.service('api/users').afterCreated.listen((event) {
print("New user: ${event.result}");
});
}
```
## RethinkService
This class interacts with a `Query` (usually a table) and serializes data to and from Maps.
## RethinkTypedService<T>
2024-07-06 10:15:18 +00:00
Does the same as above, but serializes to and from a target class using `belatuk_json_serializer` and it supports reflection.
2024-06-30 01:44:37 +00:00
## Querying
You can query these services as follows:
2024-07-06 10:15:18 +00:00
```curl
2024-06-30 01:44:37 +00:00
/path/to/service?foo=bar
2024-07-06 10:15:18 +00:00
```
2024-06-30 01:44:37 +00:00
2024-07-06 10:15:18 +00:00
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.
2024-06-30 01:44:37 +00:00
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.