Updated RethinkDB

This commit is contained in:
Thomas Hii 2024-07-03 00:13:02 +08:00
parent a0ade9d3a3
commit 094fe740c0
15 changed files with 175 additions and 67 deletions

View file

@ -1,5 +1,5 @@
{ {
"image": "dart:3.0", "image": "dart:3.4",
"forwardPorts": [3000,5000], "forwardPorts": [3000,5000],
"features": { "features": {
} }

View file

@ -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

View file

@ -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

View file

@ -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);
} }
} }

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 8.2.1
* Updated README
## 8.2.0 ## 8.2.0
* Require Dart >= 3.3 * Require Dart >= 3.3

View file

@ -5,8 +5,10 @@ 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');
//var db = Db('mongodb://root:Qwerty@localhost:27017/testDB');
await db.open(); await db.open();
//await db.authenticate("root", "Qwerty");
var service = app.use('/api/users', MongoService(db.collection('users'))); var service = app.use('/api/users', MongoService(db.collection('users')));

View file

@ -6,8 +6,8 @@ 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

View file

@ -26,7 +26,8 @@ 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');
//var db = Db('mongodb://root:Qwerty@localhost:27017/testDB');
late DbCollection testData; late DbCollection testData;
String? url; String? url;
@ -38,6 +39,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");
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
View 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

View file

@ -5,7 +5,12 @@ import 'package:logging/logging.dart';
void main() async { void main() async {
RethinkDb r = RethinkDb(); RethinkDb r = RethinkDb();
var conn = await r.connect(); var conn = await r.connect(
db: 'testDB',
host: "localhost",
port: 28015,
user: "admin",
password: "");
Angel app = Angel(); Angel app = Angel();
app.use('/todos', RethinkService(conn, r.table('todos'))); app.use('/todos', RethinkService(conn, r.table('todos')));

View file

@ -8,7 +8,7 @@ import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
typedef QueryCallback = RqlQuery Function(RqlQuery query); typedef QueryCallback = RqlQuery Function(RqlQuery query);
/// Queries a single RethinkDB table or query. /// Queries a single RethinkDB table or query.
class RethinkService extends Service { class RethinkService extends Service<String, Map<String, dynamic>> {
/// If set to `true`, clients can remove all items by passing a `null` `id` to `remove`. /// If set to `true`, clients can remove all items by passing a `null` `id` to `remove`.
/// ///
/// `false` by default. /// `false` by default.
@ -136,23 +136,24 @@ class RethinkService extends Service {
if (type == 'add') { if (type == 'add') {
// Create // Create
hookedService.fireEvent( hookedService.fireEvent(
hookedService.afterCreated, hookedService.afterCreated,
HookedServiceEvent( RethinkDbHookedServiceEvent(
true, null, null, this, HookedServiceEvent.created, true, null, null, this, HookedServiceEvent.created,
result: newVal)); result: newVal));
} else if (type == 'change') { } else if (type == 'change') {
// Update // Update
hookedService.fireEvent( hookedService.fireEvent(
hookedService.afterCreated, hookedService.afterCreated,
HookedServiceEvent( RethinkDbHookedServiceEvent(
true, null, null, this, HookedServiceEvent.updated, true, null, null, this, HookedServiceEvent.updated,
result: newVal, id: oldVal['id'], data: newVal)); result: newVal, id: oldVal['id'], data: newVal));
} else if (type == 'remove') { } else if (type == 'remove') {
// Remove // Remove
hookedService.fireEvent( hookedService.fireEvent(
hookedService.afterCreated, hookedService.afterCreated,
HookedServiceEvent( RethinkDbHookedServiceEvent(
true, null, null, this, HookedServiceEvent.removed, true, null, null, this, HookedServiceEvent.removed,
result: oldVal, id: oldVal['id'])); result: oldVal, id: oldVal['id']));
} }
@ -161,48 +162,17 @@ class RethinkService extends Service {
} }
feed.listen(onData); feed.listen(onData);
/*
feed.listen((Map event) {
var type = event['type']?.toString();
var newVal = event['new_val'], oldVal = event['old_val'];
if (type == 'add') {
// Create
hookedService.fireEvent(
hookedService.afterCreated,
HookedServiceEvent(
true, null, null, this, HookedServiceEvent.created,
result: newVal));
} else if (type == 'change') {
// Update
hookedService.fireEvent(
hookedService.afterCreated,
HookedServiceEvent(
true, null, null, this, HookedServiceEvent.updated,
result: newVal, id: oldVal['id'], data: newVal));
} else if (type == 'remove') {
// Remove
hookedService.fireEvent(
hookedService.afterCreated,
HookedServiceEvent(
true, null, null, this, HookedServiceEvent.removed,
result: oldVal, id: oldVal['id']));
}
});
*/
} }
// TODO: Invalid override method
/*
@override @override
Future index([Map params]) async { Future<List<Map<String, dynamic>>> index([Map? params]) async {
var query = buildQuery(table, params); var query = buildQuery(table, params ?? {});
return await _sendQuery(query); return await _sendQuery(query);
} }
*/
@override @override
Future read(id, [Map? params]) async { Future<Map<String, dynamic>> read(String id, [Map? params]) async {
var query = buildQuery(table.get(id?.toString()), params ?? {}); var query = buildQuery(table.get(id.toString()), params ?? {});
var found = await _sendQuery(query); var found = await _sendQuery(query);
//print('Found for $id: $found'); //print('Found for $id: $found');
@ -214,7 +184,7 @@ class RethinkService extends Service {
} }
@override @override
Future create(data, [Map? params]) async { Future<Map<String, dynamic>> create(Map data, [Map? params]) async {
if (table is! Table) throw AngelHttpException.methodNotAllowed(); if (table is! Table) throw AngelHttpException.methodNotAllowed();
var d = _serialize(data); var d = _serialize(data);
@ -224,7 +194,8 @@ class RethinkService extends Service {
} }
@override @override
Future modify(id, data, [Map? params]) async { Future<Map<String, dynamic>> modify(String id, Map data,
[Map? params]) async {
var d = _serialize(data); var d = _serialize(data);
if (d is Map && d.containsKey('id')) { if (d is Map && d.containsKey('id')) {
@ -239,13 +210,14 @@ class RethinkService extends Service {
} }
} }
var query = buildQuery(table.get(id?.toString()), params ?? {}).update(d); var query = buildQuery(table.get(id.toString()), params ?? {}).update(d);
await _sendQuery(query); await _sendQuery(query);
return await read(id, params); return await read(id, params);
} }
@override @override
Future update(id, data, [Map? params]) async { Future<Map<String, dynamic>> update(String id, Map data,
[Map? params]) async {
var d = _serialize(data); var d = _serialize(data);
if (d is Map && d.containsKey('id')) { if (d is Map && d.containsKey('id')) {
@ -261,13 +233,13 @@ class RethinkService extends Service {
} }
if (d is Map && !d.containsKey('id')) d['id'] = id.toString(); if (d is Map && !d.containsKey('id')) d['id'] = id.toString();
var query = buildQuery(table.get(id?.toString()), params ?? {}).replace(d); var query = buildQuery(table.get(id.toString()), params ?? {}).replace(d);
await _sendQuery(query); await _sendQuery(query);
return await read(id, params); return await read(id, params);
} }
@override @override
Future remove(id, [Map? params]) async { Future<Map<String, dynamic>> remove(String? id, [Map? params]) async {
if (id == null || if (id == null ||
id == 'null' && id == 'null' &&
(allowRemoveAll == true || (allowRemoveAll == true ||
@ -281,3 +253,10 @@ class RethinkService extends Service {
} }
} }
} }
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});
}

View file

@ -8,12 +8,13 @@ homepage: https://angel3-framework.web.app/
repository: https://github.com/dart-backend/angel/tree/master/packages/rethinkdb repository: https://github.com/dart-backend/angel/tree/master/packages/rethinkdb
dependencies: dependencies:
angel3_framework: ^8.4.0 angel3_framework: ^8.4.0
angel3_container: ^8.2.0
belatuk_json_serializer: ^7.0.0 belatuk_json_serializer: ^7.0.0
belatuk_rethinkdb: ^1.0.0 belatuk_rethinkdb: ^1.0.0
dev_dependencies: dev_dependencies:
angel3_client: ^8.0.0 angel3_client: ^8.2.0
angel3_test: ^8.0.0 angel3_test: ^8.2.0
logging: ^1.2.0 logging: ^1.2.0
test: ^1.25.0 test: ^1.25.0
lints: ^4.0.0 lints: ^4.0.0

View file

@ -1,6 +1,7 @@
# Tests # Tests
The tests expect you to have installed RethinkDB. You must have a `test` database 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:
available, and a server ready at the default port.
Also, the tests expect a table named `todos`. ```bash
dart test/bootstrap.dart
```

View file

@ -3,7 +3,14 @@ import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart';
void main() async { void main() async {
var r = RethinkDb(); var r = RethinkDb();
await r.connect().then((conn) { await r
.connect(
db: 'testDB',
host: "localhost",
port: 28015,
user: "admin",
password: "")
.then((conn) {
r.tableCreate('todos').run(conn); r.tableCreate('todos').run(conn);
print('Done'); print('Done');
exit(0); exit(0);

View file

@ -1,4 +1,5 @@
import 'package:angel3_client/angel3_client.dart' as c; 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_framework/angel3_framework.dart';
import 'package:angel3_rethinkdb/angel3_rethinkdb.dart'; import 'package:angel3_rethinkdb/angel3_rethinkdb.dart';
import 'package:angel3_test/angel3_test.dart'; import 'package:angel3_test/angel3_test.dart';
@ -15,9 +16,14 @@ void main() {
setUp(() async { setUp(() async {
r = RethinkDb(); r = RethinkDb();
var conn = await r.connect(); var conn = await r.connect(
db: 'testDB',
host: "localhost",
port: 28015,
user: "admin",
password: "");
app = Angel(); app = Angel(reflector: MirrorsReflector());
app.use('/todos', RethinkService(conn, r.table('todos'))); app.use('/todos', RethinkService(conn, r.table('todos')));
app.errorHandler = (e, req, res) async { app.errorHandler = (e, req, res) async {