Merge pull request #136 from dukefirehawk/feature/rethinkdb
Feature/rethinkdb
This commit is contained in:
commit
3f049465d1
25 changed files with 780 additions and 16 deletions
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"image": "dart:3.0",
|
"image": "dart:3.4",
|
||||||
"forwardPorts": [3000,5000],
|
"forwardPorts": [3000,5000],
|
||||||
"features": {
|
"features": {
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,6 +99,27 @@
|
||||||
docker logs docker-mongo-1 -f
|
docker logs docker-mongo-1 -f
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## rethinkDB
|
||||||
|
|
||||||
|
### Starting the rethinkDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-rethinkdb.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the rethinkDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-rethinkdb.yml stop
|
||||||
|
docker compose -f docker-compose-rethinkdb.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the rethinkDB container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-rethinkdb-1 -f
|
||||||
|
```
|
||||||
|
|
||||||
## Redis
|
## Redis
|
||||||
|
|
||||||
### Starting the Redis container
|
### Starting the Redis container
|
||||||
|
|
|
@ -6,8 +6,8 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- 27017:27017
|
- 27017:27017
|
||||||
environment:
|
environment:
|
||||||
#MONGO_INITDB_ROOT_USERNAME: root
|
MONGO_INITDB_ROOT_USERNAME: root
|
||||||
#MONGO_INITDB_ROOT_PASSWORD: example
|
MONGO_INITDB_ROOT_PASSWORD: Qwerty
|
||||||
MONGO_INITDB_DATABASE: local
|
MONGO_INITDB_DATABASE: local
|
||||||
volumes:
|
volumes:
|
||||||
- "mongo:/data/db"
|
- "mongo:/data/db"
|
||||||
|
@ -22,10 +22,9 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- 8081:8081
|
- 8081:8081
|
||||||
environment:
|
environment:
|
||||||
#ME_CONFIG_MONGODB_ADMINUSERNAME: root
|
ME_CONFIG_MONGODB_ADMINUSERNAME: root
|
||||||
#ME_CONFIG_MONGODB_ADMINPASSWORD: example
|
ME_CONFIG_MONGODB_ADMINPASSWORD: Qwerty
|
||||||
#ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
|
ME_CONFIG_MONGODB_URL: mongodb://root:Qwerty@mongo:27017/
|
||||||
ME_CONFIG_MONGODB_URL: mongodb://mongo:27017/
|
|
||||||
ME_CONFIG_BASICAUTH: false
|
ME_CONFIG_BASICAUTH: false
|
||||||
networks:
|
networks:
|
||||||
- webnet
|
- webnet
|
||||||
|
|
19
docker/docker-compose-rethinkdb.yml
Normal file
19
docker/docker-compose-rethinkdb.yml
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
services:
|
||||||
|
rethinkdb:
|
||||||
|
image: rethinkdb:latest
|
||||||
|
restart: "no"
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
- "28015:28015"
|
||||||
|
- "29015:29015"
|
||||||
|
volumes:
|
||||||
|
- "rethinkdb:/data"
|
||||||
|
networks:
|
||||||
|
- webnet
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
rethinkdb:
|
||||||
|
driver: local
|
||||||
|
|
||||||
|
networks:
|
||||||
|
webnet:
|
|
@ -1,5 +1,9 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 8.2.1
|
||||||
|
|
||||||
|
* Updated error handling
|
||||||
|
|
||||||
## 8.2.0
|
## 8.2.0
|
||||||
|
|
||||||
* Require Dart >= 3.3
|
* Require Dart >= 3.3
|
||||||
|
|
|
@ -97,7 +97,7 @@ abstract class BaseAngelClient extends Angel {
|
||||||
} on AngelHttpException {
|
} on AngelHttpException {
|
||||||
rethrow;
|
rethrow;
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
_log.severe('Authentication failed');
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,6 +202,8 @@ abstract class BaseAngelClient extends Angel {
|
||||||
}
|
}
|
||||||
|
|
||||||
class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
|
final _log = Logger('BaseAngelService');
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final BaseAngelClient app;
|
final BaseAngelClient app;
|
||||||
final Uri baseUrl;
|
final Uri baseUrl;
|
||||||
|
@ -294,6 +296,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onIndexed.hasListener) {
|
if (_onIndexed.hasListener) {
|
||||||
_onIndexed.addError(e, st);
|
_onIndexed.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -327,6 +330,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onRead.hasListener) {
|
if (_onRead.hasListener) {
|
||||||
_onRead.addError(e, st);
|
_onRead.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -356,6 +360,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onCreated.hasListener) {
|
if (_onCreated.hasListener) {
|
||||||
_onCreated.addError(e, st);
|
_onCreated.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,6 +393,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onModified.hasListener) {
|
if (_onModified.hasListener) {
|
||||||
_onModified.addError(e, st);
|
_onModified.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -420,6 +426,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onUpdated.hasListener) {
|
if (_onUpdated.hasListener) {
|
||||||
_onUpdated.addError(e, st);
|
_onUpdated.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -451,6 +458,7 @@ class BaseAngelService<Id, Data> extends Service<Id, Data?> {
|
||||||
if (_onRemoved.hasListener) {
|
if (_onRemoved.hasListener) {
|
||||||
_onRemoved.addError(e, st);
|
_onRemoved.addError(e, st);
|
||||||
} else {
|
} else {
|
||||||
|
_log.severe(st);
|
||||||
throw failure(response, error: e, stack: st);
|
throw failure(response, error: e, stack: st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 8.2.1
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
* Updated example to use MongoDB authentication
|
||||||
|
|
||||||
## 8.2.0
|
## 8.2.0
|
||||||
|
|
||||||
* Require Dart >= 3.3
|
* Require Dart >= 3.3
|
||||||
|
|
|
@ -66,11 +66,22 @@ See the tests for more usage examples.
|
||||||
|
|
||||||
## **Important Notes**
|
## **Important Notes**
|
||||||
|
|
||||||
When running with locally installed instance of MongoDB or docker based MongoDB, the following connection string is not supported by the underlying MongoDB driver yet. Best option at the moment is to run MongoDB with the authentication off or use MongoDB Atlas.
|
When connecting to a locally installed instance of MongoDB or docker based MongoDB with authentication enabled, the following connection string is not supported yet.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
var db = Db('mongodb://<username>:<password>@localhost:27017/local');
|
var db = Db('mongodb://<username>:<password>@localhost:27017/testDB');
|
||||||
|
await db.open();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Use the following instead. By default the user access information is stored in `admin` database.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
var db = Db('mongodb://localhost:27017/testDB');
|
||||||
|
await db.open();
|
||||||
|
await db.authenticate("<username>", "<password>", authDb: "admin");
|
||||||
|
```
|
||||||
|
|
||||||
|
Where
|
||||||
|
|
||||||
* `<username>` is MongoDB username
|
* `<username>` is MongoDB username
|
||||||
* `<password>` is MongoDB password
|
* `<password>` is MongoDB password
|
||||||
|
|
|
@ -5,8 +5,9 @@ import 'package:mongo_dart/mongo_dart.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
var app = Angel(reflector: MirrorsReflector());
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
var db = Db('mongodb://root:example@localhost:27017/local');
|
var db = Db('mongodb://localhost:27017/testDB');
|
||||||
await db.open();
|
await db.open();
|
||||||
|
await db.authenticate("root", "Qwerty", authDb: "admin");
|
||||||
|
|
||||||
var service = app.use('/api/users', MongoService(db.collection('users')));
|
var service = app.use('/api/users', MongoService(db.collection('users')));
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
name: angel3_mongo
|
name: angel3_mongo
|
||||||
version: 8.2.0
|
version: 8.2.1
|
||||||
description: MongoDB-enabled services for the Angel3 framework. Well-tested.
|
description: This is MongoDB-enabled service for the Angel3 framework.
|
||||||
homepage: https://angel3-framework.web.app/
|
homepage: https://angel3-framework.web.app/
|
||||||
repository: https://github.com/dart-backend/angel/tree/master/packages/mongo
|
repository: https://github.com/dart-backend/angel/tree/master/packages/mongo
|
||||||
environment:
|
environment:
|
||||||
sdk: '>=3.3.0 <4.0.0'
|
sdk: '>=3.3.0 <4.0.0'
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
angel3_framework: ^8.0.0
|
angel3_framework: ^8.4.0
|
||||||
angel3_container: ^8.0.0
|
angel3_container: ^8.2.0
|
||||||
belatuk_json_serializer: ^7.1.0
|
belatuk_json_serializer: ^7.1.0
|
||||||
belatuk_merge_map: ^5.1.0
|
belatuk_merge_map: ^5.1.0
|
||||||
mongo_dart: ^0.10.3
|
mongo_dart: ^0.10.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
http: ^1.0.0
|
http: ^1.0.0
|
||||||
test: ^1.24.0
|
test: ^1.24.0
|
||||||
|
|
|
@ -26,7 +26,7 @@ void main() {
|
||||||
Angel app;
|
Angel app;
|
||||||
late AngelHttp transport;
|
late AngelHttp transport;
|
||||||
late http.Client client;
|
late http.Client client;
|
||||||
var db = Db('mongodb://localhost:27017/testingDB');
|
var db = Db('mongodb://localhost:27017/testDB');
|
||||||
|
|
||||||
late DbCollection testData;
|
late DbCollection testData;
|
||||||
String? url;
|
String? url;
|
||||||
|
@ -38,6 +38,8 @@ void main() {
|
||||||
transport = AngelHttp(app);
|
transport = AngelHttp(app);
|
||||||
client = http.Client();
|
client = http.Client();
|
||||||
await db.open();
|
await db.open();
|
||||||
|
await db.authenticate("root", "Qwerty", authDb: "admin");
|
||||||
|
|
||||||
testData = db.collection('testData');
|
testData = db.collection('testData');
|
||||||
// Delete anything before we start
|
// Delete anything before we start
|
||||||
await testData.remove(<String, dynamic>{});
|
await testData.remove(<String, dynamic>{});
|
||||||
|
|
89
packages/rethinkdb/.gitignore
vendored
Normal file
89
packages/rethinkdb/.gitignore
vendored
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
# See https://www.dartlang.org/tools/private-files.html
|
||||||
|
|
||||||
|
# Files and directories created by pub
|
||||||
|
.buildlog
|
||||||
|
.packages
|
||||||
|
.project
|
||||||
|
.pub/
|
||||||
|
build/
|
||||||
|
**/packages/
|
||||||
|
.dart_tool
|
||||||
|
|
||||||
|
# Files created by dart2js
|
||||||
|
# (Most Dart developers will use pub build to compile Dart, use/modify these
|
||||||
|
# rules if you intend to use dart2js directly
|
||||||
|
# Convention is to use extension '.dart.js' for Dart compiled to Javascript to
|
||||||
|
# differentiate from explicit Javascript files)
|
||||||
|
*.dart.js
|
||||||
|
*.part.js
|
||||||
|
*.js.deps
|
||||||
|
*.js.map
|
||||||
|
*.info.json
|
||||||
|
|
||||||
|
# Directory created by dartdoc
|
||||||
|
doc/api/
|
||||||
|
|
||||||
|
# Don't commit pubspec lock file
|
||||||
|
# (Library packages only! Remove pattern if developing an application package)
|
||||||
|
pubspec.lock
|
||||||
|
### Dart template
|
||||||
|
# See https://www.dartlang.org/tools/private-files.html
|
||||||
|
|
||||||
|
# Files and directories created by pub
|
||||||
|
|
||||||
|
# Files created by dart2js
|
||||||
|
# (Most Dart developers will use pub build to compile Dart, use/modify these
|
||||||
|
# rules if you intend to use dart2js directly
|
||||||
|
# Convention is to use extension '.dart.js' for Dart compiled to Javascript to
|
||||||
|
# differentiate from explicit Javascript files)
|
||||||
|
|
||||||
|
# Directory created by dartdoc
|
||||||
|
|
||||||
|
# Don't commit pubspec lock file
|
||||||
|
# (Library packages only! Remove pattern if developing an application package)
|
||||||
|
### JetBrains template
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff:
|
||||||
|
.idea/workspace.xml
|
||||||
|
.idea/tasks.xml
|
||||||
|
.idea/dictionaries
|
||||||
|
.idea/vcs.xml
|
||||||
|
.idea/jsLibraryMappings.xml
|
||||||
|
|
||||||
|
# Sensitive or high-churn files:
|
||||||
|
.idea/dataSources.ids
|
||||||
|
.idea/dataSources.xml
|
||||||
|
.idea/dataSources.local.xml
|
||||||
|
.idea/sqlDataSources.xml
|
||||||
|
.idea/dynamic.xml
|
||||||
|
.idea/uiDesigner.xml
|
||||||
|
|
||||||
|
# Gradle:
|
||||||
|
.idea/gradle.xml
|
||||||
|
.idea/libraries
|
||||||
|
|
||||||
|
# Mongo Explorer plugin:
|
||||||
|
.idea/mongoSettings.xml
|
||||||
|
|
||||||
|
## File-based project format:
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
## Plugin-specific files:
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
/out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
12
packages/rethinkdb/AUTHORS.md
Normal file
12
packages/rethinkdb/AUTHORS.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Primary Authors
|
||||||
|
===============
|
||||||
|
|
||||||
|
* __[Thomas Hii](dukefirehawk.apps@gmail.com)__
|
||||||
|
|
||||||
|
Thomas is the current maintainer of the code base. He has refactored and migrated the
|
||||||
|
code base to support NNBD.
|
||||||
|
|
||||||
|
* __[Tobe O](thosakwe@gmail.com)__
|
||||||
|
|
||||||
|
Tobe has written much of the original code prior to NNBD migration. He has moved on and
|
||||||
|
is no longer involved with the project.
|
35
packages/rethinkdb/CHANGELOG.md
Normal file
35
packages/rethinkdb/CHANGELOG.md
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
# Change Log
|
||||||
|
|
||||||
|
## 8.0.0
|
||||||
|
|
||||||
|
* Require Dart >= 3.3
|
||||||
|
* Updated `lints` to 4.0.0
|
||||||
|
|
||||||
|
## 7.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 6.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 5.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 4.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 3.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 2.0.0
|
||||||
|
|
||||||
|
* Migrated to support Dart >= 2.12 NNBD
|
||||||
|
|
||||||
|
## 1.1.0
|
||||||
|
|
||||||
|
* Moved to `package:rethinkdb_driver`
|
||||||
|
* Fixed references to old hooked event names
|
29
packages/rethinkdb/LICENSE
Normal file
29
packages/rethinkdb/LICENSE
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
BSD 3-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2021, dukefirehawk.com
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
94
packages/rethinkdb/README.md
Normal file
94
packages/rethinkdb/README.md
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
# Angel3 RethinkDB
|
||||||
|
|
||||||
|
[![version 1.0.7](https://img.shields.io/badge/pub-1.0.7-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 service for the Angel3 framework.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add the following to your `pubspec.yaml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
dependencies:
|
||||||
|
angel3_rethink: ^8.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
`belatuk-rethinkdb` driver will be used for connecting to RethinkDB.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## 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 = RethinkDb();
|
||||||
|
var conn = await r.connect(
|
||||||
|
db: 'testDB',
|
||||||
|
host: "localhost",
|
||||||
|
port: 28015,
|
||||||
|
user: "admin",
|
||||||
|
password: "");
|
||||||
|
|
||||||
|
app.use('/api/users', RethinkService(conn, r.table('users')));
|
||||||
|
|
||||||
|
// Add type de/serialization if you want
|
||||||
|
app.use('/api/users', TypedService<User>(RethinkService(conn, r.table('users'))));
|
||||||
|
|
||||||
|
// You don't have to even use a table...
|
||||||
|
app.use('/api/pro_users', RethinkService(conn, r.table('users').filter({'membership': 'pro'})));
|
||||||
|
|
||||||
|
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>
|
||||||
|
|
||||||
|
Does the same as above, but serializes to and from a target class using `belatuk_json_serializer` and it supports reflection.
|
||||||
|
|
||||||
|
## Querying
|
||||||
|
|
||||||
|
You can query these services as follows:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
/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.
|
1
packages/rethinkdb/analysis_options.yaml
Normal file
1
packages/rethinkdb/analysis_options.yaml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
include: package:lints/recommended.yaml
|
23
packages/rethinkdb/example/example.dart
Normal file
23
packages/rethinkdb/example/example.dart
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import 'package:angel3_framework/angel3_framework.dart';
|
||||||
|
import 'package:angel3_rethinkdb/angel3_rethinkdb.dart';
|
||||||
|
import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
RethinkDb r = RethinkDb();
|
||||||
|
var conn = await r.connect(
|
||||||
|
db: 'testDB',
|
||||||
|
host: "localhost",
|
||||||
|
port: 28015,
|
||||||
|
user: "admin",
|
||||||
|
password: "");
|
||||||
|
|
||||||
|
Angel app = Angel();
|
||||||
|
app.use('/todos', RethinkService(conn, r.table('todos')));
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) async {
|
||||||
|
print('Whoops: $e');
|
||||||
|
};
|
||||||
|
|
||||||
|
app.logger = Logger.detached('angel')..onRecord.listen(print);
|
||||||
|
}
|
1
packages/rethinkdb/lib/angel3_rethinkdb.dart
Normal file
1
packages/rethinkdb/lib/angel3_rethinkdb.dart
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export 'src/rethink_service.dart';
|
262
packages/rethinkdb/lib/src/rethink_service.dart
Normal file
262
packages/rethinkdb/lib/src/rethink_service.dart
Normal file
|
@ -0,0 +1,262 @@
|
||||||
|
import 'dart:async';
|
||||||
|
//import 'dart:io';
|
||||||
|
import 'package:angel3_framework/angel3_framework.dart';
|
||||||
|
import 'package:belatuk_json_serializer/belatuk_json_serializer.dart' as god;
|
||||||
|
import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
|
||||||
|
|
||||||
|
// Extends a RethinkDB query.
|
||||||
|
typedef QueryCallback = RqlQuery Function(RqlQuery query);
|
||||||
|
|
||||||
|
/// Queries a single RethinkDB table or query.
|
||||||
|
class RethinkService extends Service<String, Map<String, dynamic>> {
|
||||||
|
/// If set to `true`, clients can remove all items by passing a `null` `id` to `remove`.
|
||||||
|
///
|
||||||
|
/// `false` by default.
|
||||||
|
final bool allowRemoveAll;
|
||||||
|
|
||||||
|
/// If set to `true`, parameters in `req.query` are applied to the database query.
|
||||||
|
final bool allowQuery;
|
||||||
|
|
||||||
|
final bool debug;
|
||||||
|
|
||||||
|
/// If set to `true`, then a HookedService mounted over this instance
|
||||||
|
/// will fire events when RethinkDB pushes events.
|
||||||
|
///
|
||||||
|
/// Good for scaling. ;)
|
||||||
|
final bool listenForChanges;
|
||||||
|
|
||||||
|
final Connection connection;
|
||||||
|
|
||||||
|
/// Doesn't actually have to be a table, just a RethinkDB query.
|
||||||
|
///
|
||||||
|
/// However, a table is the most common usecase.
|
||||||
|
final RqlQuery table;
|
||||||
|
|
||||||
|
RethinkService(this.connection, this.table,
|
||||||
|
{this.allowRemoveAll = false,
|
||||||
|
this.allowQuery = true,
|
||||||
|
this.debug = false,
|
||||||
|
this.listenForChanges = true})
|
||||||
|
: super();
|
||||||
|
|
||||||
|
RqlQuery buildQuery(RqlQuery initialQuery, Map params) {
|
||||||
|
params['broadcast'] = params.containsKey('broadcast')
|
||||||
|
? params['broadcast']
|
||||||
|
: (listenForChanges != true);
|
||||||
|
|
||||||
|
var q = _getQueryInner(initialQuery, params);
|
||||||
|
|
||||||
|
if (params.containsKey('reql') == true && params['reql'] is QueryCallback) {
|
||||||
|
q = params['reql'](q) as RqlQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
RqlQuery _getQueryInner(RqlQuery query, Map params) {
|
||||||
|
if (!params.containsKey('query')) {
|
||||||
|
return query;
|
||||||
|
} else {
|
||||||
|
if (params['query'] is RqlQuery) {
|
||||||
|
return params['query'] as RqlQuery;
|
||||||
|
} else if (params['query'] is QueryCallback) {
|
||||||
|
return params['query'](table) as RqlQuery;
|
||||||
|
} else if (params['query'] is! Map || allowQuery != true) {
|
||||||
|
return query;
|
||||||
|
} else {
|
||||||
|
var q = params['query'] as Map;
|
||||||
|
return q.keys.fold<RqlQuery>(query, (out, key) {
|
||||||
|
var val = q[key];
|
||||||
|
|
||||||
|
if (val is RequestContext ||
|
||||||
|
val is ResponseContext ||
|
||||||
|
key == 'provider' ||
|
||||||
|
val is Providers) {
|
||||||
|
return out;
|
||||||
|
} else {
|
||||||
|
return out.filter({key.toString(): val});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future _sendQuery(RqlQuery query) async {
|
||||||
|
var result = await query.run(connection);
|
||||||
|
|
||||||
|
if (result is Cursor) {
|
||||||
|
return await result.toList();
|
||||||
|
} else if (result is Map && result['generated_keys'] is List) {
|
||||||
|
if (result['generated_keys'].length == 1) {
|
||||||
|
return await read(result['generated_keys'].first);
|
||||||
|
}
|
||||||
|
//return await Future.wait(result['generated_keys'].map(read));
|
||||||
|
return await result['generated_keys'].map(read);
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic _serialize(data) {
|
||||||
|
if (data is Map) {
|
||||||
|
return data;
|
||||||
|
} else if (data is Iterable) {
|
||||||
|
return data.map(_serialize).toList();
|
||||||
|
} else {
|
||||||
|
return god.serializeObject(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic _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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onHooked(HookedService hookedService) {
|
||||||
|
if (listenForChanges == true) {
|
||||||
|
listenToQuery(table, hookedService);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future listenToQuery(RqlQuery query, HookedService hookedService) async {
|
||||||
|
var feed =
|
||||||
|
await query.changes({'include_types': true}).run(connection) as Feed;
|
||||||
|
|
||||||
|
Future<dynamic> onData(dynamic event) {
|
||||||
|
if (event != null && event is Map) {
|
||||||
|
var type = event['type']?.toString();
|
||||||
|
var newVal = event['new_val'];
|
||||||
|
var oldVal = event['old_val'];
|
||||||
|
|
||||||
|
if (type == 'add') {
|
||||||
|
// Create
|
||||||
|
|
||||||
|
hookedService.fireEvent(
|
||||||
|
hookedService.afterCreated,
|
||||||
|
RethinkDbHookedServiceEvent(
|
||||||
|
true, null, null, this, HookedServiceEvent.created,
|
||||||
|
result: newVal));
|
||||||
|
} else if (type == 'change') {
|
||||||
|
// Update
|
||||||
|
hookedService.fireEvent(
|
||||||
|
hookedService.afterCreated,
|
||||||
|
RethinkDbHookedServiceEvent(
|
||||||
|
true, null, null, this, HookedServiceEvent.updated,
|
||||||
|
result: newVal, id: oldVal['id'], data: newVal));
|
||||||
|
} else if (type == 'remove') {
|
||||||
|
// Remove
|
||||||
|
hookedService.fireEvent(
|
||||||
|
hookedService.afterCreated,
|
||||||
|
RethinkDbHookedServiceEvent(
|
||||||
|
true, null, null, this, HookedServiceEvent.removed,
|
||||||
|
result: oldVal, id: oldVal['id']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
feed.listen(onData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<Map<String, dynamic>>> index([Map? params]) async {
|
||||||
|
var query = buildQuery(table, params ?? {});
|
||||||
|
return await _sendQuery(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> read(String id, [Map? params]) async {
|
||||||
|
var query = buildQuery(table.get(id.toString()), params ?? {});
|
||||||
|
var found = await _sendQuery(query);
|
||||||
|
//print('Found for $id: $found');
|
||||||
|
|
||||||
|
if (found == null) {
|
||||||
|
throw AngelHttpException.notFound(message: 'No record found for ID $id');
|
||||||
|
} else {
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> create(Map data, [Map? params]) async {
|
||||||
|
if (table is! Table) throw AngelHttpException.methodNotAllowed();
|
||||||
|
|
||||||
|
var d = _serialize(data);
|
||||||
|
var q = table as Table;
|
||||||
|
var query = buildQuery(q.insert(_squeeze(d)), params ?? {});
|
||||||
|
return await _sendQuery(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> modify(String id, Map data,
|
||||||
|
[Map? params]) async {
|
||||||
|
var d = _serialize(data);
|
||||||
|
|
||||||
|
if (d is Map && d.containsKey('id')) {
|
||||||
|
try {
|
||||||
|
await read(d['id'], params);
|
||||||
|
} on AngelHttpException catch (e) {
|
||||||
|
if (e.statusCode == 404) {
|
||||||
|
return await create(data, params);
|
||||||
|
} else {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var query = buildQuery(table.get(id.toString()), params ?? {}).update(d);
|
||||||
|
await _sendQuery(query);
|
||||||
|
return await read(id, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> update(String id, Map data,
|
||||||
|
[Map? params]) async {
|
||||||
|
var d = _serialize(data);
|
||||||
|
|
||||||
|
if (d is Map && d.containsKey('id')) {
|
||||||
|
try {
|
||||||
|
await read(d['id'], params);
|
||||||
|
} on AngelHttpException catch (e) {
|
||||||
|
if (e.statusCode == 404) {
|
||||||
|
return await create(data, params);
|
||||||
|
} else {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d is Map && !d.containsKey('id')) d['id'] = id.toString();
|
||||||
|
var query = buildQuery(table.get(id.toString()), params ?? {}).replace(d);
|
||||||
|
await _sendQuery(query);
|
||||||
|
return await read(id, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> remove(String? id, [Map? params]) async {
|
||||||
|
if (id == null ||
|
||||||
|
id == 'null' &&
|
||||||
|
(allowRemoveAll == true ||
|
||||||
|
params?.containsKey('provider') != true)) {
|
||||||
|
return await _sendQuery(table.delete());
|
||||||
|
} else {
|
||||||
|
var prior = await read(id, params);
|
||||||
|
var query = buildQuery(table.get(id), params ?? {}).delete();
|
||||||
|
await _sendQuery(query);
|
||||||
|
return prior;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RethinkDbHookedServiceEvent
|
||||||
|
extends HookedServiceEvent<dynamic, dynamic, RethinkService> {
|
||||||
|
RethinkDbHookedServiceEvent(super.isAfter, super.request, super.response,
|
||||||
|
super.service, super.eventName,
|
||||||
|
{super.id, super.data, super.params, super.result});
|
||||||
|
}
|
20
packages/rethinkdb/pubspec.yaml
Normal file
20
packages/rethinkdb/pubspec.yaml
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
name: angel3_rethinkdb
|
||||||
|
version: 8.0.0
|
||||||
|
description: This is RethinkDB-enabled service for the Angel3 framework.
|
||||||
|
environment:
|
||||||
|
sdk: ">=3.3.0 <4.0.0"
|
||||||
|
homepage: https://angel3-framework.web.app/
|
||||||
|
repository: https://github.com/dart-backend/angel/tree/master/packages/rethinkdb
|
||||||
|
dependencies:
|
||||||
|
angel3_framework: ^8.4.0
|
||||||
|
angel3_container: ^8.2.0
|
||||||
|
belatuk_json_serializer: ^7.0.0
|
||||||
|
belatuk_rethinkdb: ^1.0.0
|
||||||
|
|
||||||
|
dev_dependencies:
|
||||||
|
angel3_client: ^8.2.0
|
||||||
|
angel3_test: ^8.2.0
|
||||||
|
logging: ^1.2.0
|
||||||
|
test: ^1.25.0
|
||||||
|
lints: ^4.0.0
|
||||||
|
|
7
packages/rethinkdb/test/README.md
Normal file
7
packages/rethinkdb/test/README.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# Tests
|
||||||
|
|
||||||
|
The tests expect you to have installed RethinkDB. You must have a `testDB` database available, and a server ready at the default port. Also, the tests expect a table named `todos`. To create the table, run the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart test/bootstrap.dart
|
||||||
|
```
|
18
packages/rethinkdb/test/bootstrap.dart
Normal file
18
packages/rethinkdb/test/bootstrap.dart
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var r = RethinkDb();
|
||||||
|
await r
|
||||||
|
.connect(
|
||||||
|
db: 'testDB',
|
||||||
|
host: "localhost",
|
||||||
|
port: 28015,
|
||||||
|
user: "admin",
|
||||||
|
password: "")
|
||||||
|
.then((conn) {
|
||||||
|
r.tableCreate('todos').run(conn);
|
||||||
|
print('Done');
|
||||||
|
exit(0);
|
||||||
|
});
|
||||||
|
}
|
10
packages/rethinkdb/test/common.dart
Normal file
10
packages/rethinkdb/test/common.dart
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
class Todo {
|
||||||
|
String? title;
|
||||||
|
bool completed;
|
||||||
|
|
||||||
|
Todo({this.title, this.completed = false});
|
||||||
|
|
||||||
|
Map toJson() {
|
||||||
|
return {'title': title, 'completed': completed == true};
|
||||||
|
}
|
||||||
|
}
|
91
packages/rethinkdb/test/generic_test.dart
Normal file
91
packages/rethinkdb/test/generic_test.dart
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
import 'package:angel3_client/angel3_client.dart' as c;
|
||||||
|
import 'package:angel3_container/mirrors.dart';
|
||||||
|
import 'package:angel3_framework/angel3_framework.dart';
|
||||||
|
import 'package:angel3_rethinkdb/angel3_rethinkdb.dart';
|
||||||
|
import 'package:angel3_test/angel3_test.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Angel app;
|
||||||
|
late TestClient client;
|
||||||
|
RethinkDb r;
|
||||||
|
late c.Service todoService;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
r = RethinkDb();
|
||||||
|
var conn = await r.connect(
|
||||||
|
db: 'testDB',
|
||||||
|
host: "localhost",
|
||||||
|
port: 28015,
|
||||||
|
user: "admin",
|
||||||
|
password: "");
|
||||||
|
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.use('/todos', RethinkService(conn, r.table('todos')));
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) async {
|
||||||
|
print('Whoops: $e');
|
||||||
|
};
|
||||||
|
|
||||||
|
app.logger = Logger.detached('angel')..onRecord.listen(print);
|
||||||
|
|
||||||
|
client = await connectTo(app);
|
||||||
|
todoService = client.service('todos');
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => client.close());
|
||||||
|
|
||||||
|
test('index', () async {
|
||||||
|
var result = await todoService.index();
|
||||||
|
print('Response: $result');
|
||||||
|
expect(result, isList);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create+read', () async {
|
||||||
|
var todo = Todo(title: 'Clean your room');
|
||||||
|
var creation = await todoService.create(todo.toJson());
|
||||||
|
print('Creation: $creation');
|
||||||
|
|
||||||
|
var id = creation['id'];
|
||||||
|
var result = await todoService.read(id);
|
||||||
|
|
||||||
|
print('Response: $result');
|
||||||
|
expect(result, isMap);
|
||||||
|
expect(result['id'], equals(id));
|
||||||
|
expect(result['title'], equals(todo.title));
|
||||||
|
expect(result['completed'], equals(todo.completed));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('modify', () async {
|
||||||
|
var todo = Todo(title: 'Clean your room');
|
||||||
|
var creation = await todoService.create(todo.toJson());
|
||||||
|
print('Creation: $creation');
|
||||||
|
|
||||||
|
var id = creation['id'];
|
||||||
|
var result = await todoService.modify(id, {'title': 'Eat healthy'});
|
||||||
|
|
||||||
|
print('Response: $result');
|
||||||
|
expect(result, isMap);
|
||||||
|
expect(result['id'], equals(id));
|
||||||
|
expect(result['title'], equals('Eat healthy'));
|
||||||
|
expect(result['completed'], equals(todo.completed));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('remove', () async {
|
||||||
|
var todo = Todo(title: 'Clean your room');
|
||||||
|
var creation = await todoService.create(todo.toJson());
|
||||||
|
print('Creation: $creation');
|
||||||
|
|
||||||
|
var id = creation['id'];
|
||||||
|
var result = await todoService.remove(id);
|
||||||
|
|
||||||
|
print('Response: $result');
|
||||||
|
expect(result, isMap);
|
||||||
|
expect(result['id'], equals(id));
|
||||||
|
expect(result['title'], equals(todo.title));
|
||||||
|
expect(result['completed'], equals(todo.completed));
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue