From 094fe740c0655c55adb0d2bf44aa0d2d1381a813 Mon Sep 17 00:00:00 2001 From: Thomas Hii Date: Wed, 3 Jul 2024 00:13:02 +0800 Subject: [PATCH] Updated RethinkDB --- .devcontainer/devcontainer.json | 2 +- docker/docker-compose-mongo.yml | 11 ++- packages/client/CHANGELOG.md | 4 + packages/client/lib/base_angel_client.dart | 10 ++- packages/mongo/CHANGELOG.md | 4 + packages/mongo/example/example.dart | 4 +- packages/mongo/pubspec.yaml | 4 +- packages/mongo/test/generic_test.dart | 5 +- packages/rethinkdb/.gitignore | 89 +++++++++++++++++++ packages/rethinkdb/example/example.dart | 7 +- .../rethinkdb/lib/src/rethink_service.dart | 71 ++++++--------- packages/rethinkdb/pubspec.yaml | 5 +- packages/rethinkdb/test/README.md | 7 +- packages/rethinkdb/test/bootstrap.dart | 9 +- packages/rethinkdb/test/generic_test.dart | 10 ++- 15 files changed, 175 insertions(+), 67 deletions(-) create mode 100644 packages/rethinkdb/.gitignore diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 1e19909c..4f93a5a3 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,5 +1,5 @@ { - "image": "dart:3.0", + "image": "dart:3.4", "forwardPorts": [3000,5000], "features": { } diff --git a/docker/docker-compose-mongo.yml b/docker/docker-compose-mongo.yml index e27d0cb7..00034fcd 100644 --- a/docker/docker-compose-mongo.yml +++ b/docker/docker-compose-mongo.yml @@ -6,8 +6,8 @@ services: ports: - 27017:27017 environment: - #MONGO_INITDB_ROOT_USERNAME: root - #MONGO_INITDB_ROOT_PASSWORD: example + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: Qwerty MONGO_INITDB_DATABASE: local volumes: - "mongo:/data/db" @@ -22,10 +22,9 @@ services: ports: - 8081:8081 environment: - #ME_CONFIG_MONGODB_ADMINUSERNAME: root - #ME_CONFIG_MONGODB_ADMINPASSWORD: example - #ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/ - ME_CONFIG_MONGODB_URL: mongodb://mongo:27017/ + ME_CONFIG_MONGODB_ADMINUSERNAME: root + ME_CONFIG_MONGODB_ADMINPASSWORD: Qwerty + ME_CONFIG_MONGODB_URL: mongodb://root:Qwerty@mongo:27017/ ME_CONFIG_BASICAUTH: false networks: - webnet diff --git a/packages/client/CHANGELOG.md b/packages/client/CHANGELOG.md index bf254169..11a7b474 100644 --- a/packages/client/CHANGELOG.md +++ b/packages/client/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 8.2.1 + +* Updated error handling + ## 8.2.0 * Require Dart >= 3.3 diff --git a/packages/client/lib/base_angel_client.dart b/packages/client/lib/base_angel_client.dart index 4d2aeda1..5f790758 100644 --- a/packages/client/lib/base_angel_client.dart +++ b/packages/client/lib/base_angel_client.dart @@ -97,7 +97,7 @@ abstract class BaseAngelClient extends Angel { } on AngelHttpException { rethrow; } catch (e, st) { - _log.severe('Authentication failed'); + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -202,6 +202,8 @@ abstract class BaseAngelClient extends Angel { } class BaseAngelService extends Service { + final _log = Logger('BaseAngelService'); + @override final BaseAngelClient app; final Uri baseUrl; @@ -294,6 +296,7 @@ class BaseAngelService extends Service { if (_onIndexed.hasListener) { _onIndexed.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -327,6 +330,7 @@ class BaseAngelService extends Service { if (_onRead.hasListener) { _onRead.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -356,6 +360,7 @@ class BaseAngelService extends Service { if (_onCreated.hasListener) { _onCreated.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -388,6 +393,7 @@ class BaseAngelService extends Service { if (_onModified.hasListener) { _onModified.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -420,6 +426,7 @@ class BaseAngelService extends Service { if (_onUpdated.hasListener) { _onUpdated.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } @@ -451,6 +458,7 @@ class BaseAngelService extends Service { if (_onRemoved.hasListener) { _onRemoved.addError(e, st); } else { + _log.severe(st); throw failure(response, error: e, stack: st); } } diff --git a/packages/mongo/CHANGELOG.md b/packages/mongo/CHANGELOG.md index 93f4f4ee..a806783c 100644 --- a/packages/mongo/CHANGELOG.md +++ b/packages/mongo/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 8.2.1 + +* Updated README + ## 8.2.0 * Require Dart >= 3.3 diff --git a/packages/mongo/example/example.dart b/packages/mongo/example/example.dart index 9618969b..b108ddb2 100644 --- a/packages/mongo/example/example.dart +++ b/packages/mongo/example/example.dart @@ -5,8 +5,10 @@ import 'package:mongo_dart/mongo_dart.dart'; void main() async { 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.authenticate("root", "Qwerty"); var service = app.use('/api/users', MongoService(db.collection('users'))); diff --git a/packages/mongo/pubspec.yaml b/packages/mongo/pubspec.yaml index 5b3a066a..ec745b34 100644 --- a/packages/mongo/pubspec.yaml +++ b/packages/mongo/pubspec.yaml @@ -6,8 +6,8 @@ repository: https://github.com/dart-backend/angel/tree/master/packages/mongo environment: sdk: '>=3.3.0 <4.0.0' dependencies: - angel3_framework: ^8.0.0 - angel3_container: ^8.0.0 + angel3_framework: ^8.4.0 + angel3_container: ^8.2.0 belatuk_json_serializer: ^7.1.0 belatuk_merge_map: ^5.1.0 mongo_dart: ^0.10.3 diff --git a/packages/mongo/test/generic_test.dart b/packages/mongo/test/generic_test.dart index 961acc2b..36ddaa8f 100644 --- a/packages/mongo/test/generic_test.dart +++ b/packages/mongo/test/generic_test.dart @@ -26,7 +26,8 @@ void main() { Angel app; late AngelHttp transport; 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; String? url; @@ -38,6 +39,8 @@ void main() { transport = AngelHttp(app); client = http.Client(); await db.open(); + //await db.authenticate("root", "Qwerty"); + testData = db.collection('testData'); // Delete anything before we start await testData.remove({}); diff --git a/packages/rethinkdb/.gitignore b/packages/rethinkdb/.gitignore new file mode 100644 index 00000000..467d808c --- /dev/null +++ b/packages/rethinkdb/.gitignore @@ -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 + diff --git a/packages/rethinkdb/example/example.dart b/packages/rethinkdb/example/example.dart index 50cee9e0..d43d100c 100644 --- a/packages/rethinkdb/example/example.dart +++ b/packages/rethinkdb/example/example.dart @@ -5,7 +5,12 @@ import 'package:logging/logging.dart'; void main() async { 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(); app.use('/todos', RethinkService(conn, r.table('todos'))); diff --git a/packages/rethinkdb/lib/src/rethink_service.dart b/packages/rethinkdb/lib/src/rethink_service.dart index aa5059da..a200cb21 100644 --- a/packages/rethinkdb/lib/src/rethink_service.dart +++ b/packages/rethinkdb/lib/src/rethink_service.dart @@ -8,7 +8,7 @@ import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart'; typedef QueryCallback = RqlQuery Function(RqlQuery query); /// Queries a single RethinkDB table or query. -class RethinkService extends Service { +class RethinkService extends Service> { /// If set to `true`, clients can remove all items by passing a `null` `id` to `remove`. /// /// `false` by default. @@ -136,23 +136,24 @@ class RethinkService extends Service { if (type == 'add') { // Create + hookedService.fireEvent( hookedService.afterCreated, - HookedServiceEvent( + RethinkDbHookedServiceEvent( true, null, null, this, HookedServiceEvent.created, result: newVal)); } else if (type == 'change') { // Update hookedService.fireEvent( hookedService.afterCreated, - HookedServiceEvent( + RethinkDbHookedServiceEvent( true, null, null, this, HookedServiceEvent.updated, result: newVal, id: oldVal['id'], data: newVal)); } else if (type == 'remove') { // Remove hookedService.fireEvent( hookedService.afterCreated, - HookedServiceEvent( + RethinkDbHookedServiceEvent( true, null, null, this, HookedServiceEvent.removed, result: oldVal, id: oldVal['id'])); } @@ -161,48 +162,17 @@ class RethinkService extends Service { } 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 - Future index([Map params]) async { - var query = buildQuery(table, params); + Future>> index([Map? params]) async { + var query = buildQuery(table, params ?? {}); return await _sendQuery(query); } -*/ + @override - Future read(id, [Map? params]) async { - var query = buildQuery(table.get(id?.toString()), params ?? {}); + Future> read(String id, [Map? params]) async { + var query = buildQuery(table.get(id.toString()), params ?? {}); var found = await _sendQuery(query); //print('Found for $id: $found'); @@ -214,7 +184,7 @@ class RethinkService extends Service { } @override - Future create(data, [Map? params]) async { + Future> create(Map data, [Map? params]) async { if (table is! Table) throw AngelHttpException.methodNotAllowed(); var d = _serialize(data); @@ -224,7 +194,8 @@ class RethinkService extends Service { } @override - Future modify(id, data, [Map? params]) async { + Future> modify(String id, Map data, + [Map? params]) async { var d = _serialize(data); 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); return await read(id, params); } @override - Future update(id, data, [Map? params]) async { + Future> update(String id, Map data, + [Map? params]) async { var d = _serialize(data); 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(); - var query = buildQuery(table.get(id?.toString()), params ?? {}).replace(d); + var query = buildQuery(table.get(id.toString()), params ?? {}).replace(d); await _sendQuery(query); return await read(id, params); } @override - Future remove(id, [Map? params]) async { + Future> remove(String? id, [Map? params]) async { if (id == null || id == 'null' && (allowRemoveAll == true || @@ -281,3 +253,10 @@ class RethinkService extends Service { } } } + +class RethinkDbHookedServiceEvent + extends HookedServiceEvent { + RethinkDbHookedServiceEvent(super.isAfter, super.request, super.response, + super.service, super.eventName, + {super.id, super.data, super.params, super.result}); +} diff --git a/packages/rethinkdb/pubspec.yaml b/packages/rethinkdb/pubspec.yaml index 74fd8acd..aa72467e 100644 --- a/packages/rethinkdb/pubspec.yaml +++ b/packages/rethinkdb/pubspec.yaml @@ -8,12 +8,13 @@ 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.0.0 - angel3_test: ^8.0.0 + angel3_client: ^8.2.0 + angel3_test: ^8.2.0 logging: ^1.2.0 test: ^1.25.0 lints: ^4.0.0 diff --git a/packages/rethinkdb/test/README.md b/packages/rethinkdb/test/README.md index 59324524..3ca34709 100644 --- a/packages/rethinkdb/test/README.md +++ b/packages/rethinkdb/test/README.md @@ -1,6 +1,7 @@ # Tests -The tests expect you to have installed RethinkDB. You must have a `test` database -available, and a server ready at the default port. +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: -Also, the tests expect a table named `todos`. +```bash + dart test/bootstrap.dart +``` diff --git a/packages/rethinkdb/test/bootstrap.dart b/packages/rethinkdb/test/bootstrap.dart index 39c2fbaf..c09763c1 100644 --- a/packages/rethinkdb/test/bootstrap.dart +++ b/packages/rethinkdb/test/bootstrap.dart @@ -3,7 +3,14 @@ import 'package:belatuk_rethinkdb/belatuk_rethinkdb.dart'; void main() async { 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); print('Done'); exit(0); diff --git a/packages/rethinkdb/test/generic_test.dart b/packages/rethinkdb/test/generic_test.dart index 6eb0063f..a2beaa1d 100644 --- a/packages/rethinkdb/test/generic_test.dart +++ b/packages/rethinkdb/test/generic_test.dart @@ -1,4 +1,5 @@ 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'; @@ -15,9 +16,14 @@ void main() { setUp(() async { 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.errorHandler = (e, req, res) async {