Updated mongoDB
This commit is contained in:
parent
32a7e88d2d
commit
2139ae4422
12 changed files with 204 additions and 65 deletions
|
@ -1,11 +1,24 @@
|
||||||
# Working with Docker
|
# Docker setup
|
||||||
|
|
||||||
## Postgresql
|
## PostreSQL
|
||||||
|
|
||||||
### Starting the container
|
### Starting the PostreSQL container
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker-compose -f docker-compose-pg.yml up
|
docker compose -f docker-compose-pg.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the PostreSQL container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-pg.yml stop
|
||||||
|
docker compose -f docker-compose-pg.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the PostreSQL container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-pg-1 -f
|
||||||
```
|
```
|
||||||
|
|
||||||
### Running psql
|
### Running psql
|
||||||
|
@ -25,6 +38,84 @@
|
||||||
|
|
||||||
## MariaDB
|
## MariaDB
|
||||||
|
|
||||||
|
### Starting the MariaDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mariadb.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the MariaDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mariadb.yml stop
|
||||||
|
docker compose -f docker-compose-mariadb.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the MariaDB container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-mariadb-1 -f
|
||||||
|
```
|
||||||
|
|
||||||
## MySQL
|
## MySQL
|
||||||
|
|
||||||
|
### Starting the MySQL container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mysql.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the MySQL container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mysql.yml stop
|
||||||
|
docker compose -f docker-compose-mysql.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the MySQL container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-mysql-1 -f
|
||||||
|
```
|
||||||
|
|
||||||
|
## MongoDB
|
||||||
|
|
||||||
|
### Starting the MongoDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mongo.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the MongoDB container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-mongo.yml stop
|
||||||
|
docker compose -f docker-compose-mongo.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the MongoDB container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-mongo-1 -f
|
||||||
|
```
|
||||||
|
|
||||||
## Redis
|
## Redis
|
||||||
|
|
||||||
|
### Starting the Redis container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-redis.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stopping the Redis container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose-redis.yml stop
|
||||||
|
docker compose -f docker-compose-redis.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking the Redis container log
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs docker-redis-1 -f
|
||||||
|
```
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
version: "3.8"
|
|
||||||
services:
|
services:
|
||||||
mariadb:
|
mariadb:
|
||||||
image: mariadb:latest
|
image: mariadb:latest
|
||||||
|
|
38
docker/docker-compose-mongo.yml
Normal file
38
docker/docker-compose-mongo.yml
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
services:
|
||||||
|
|
||||||
|
mongo:
|
||||||
|
image: mongo
|
||||||
|
restart: no
|
||||||
|
ports:
|
||||||
|
- 27017:27017
|
||||||
|
environment:
|
||||||
|
#MONGO_INITDB_ROOT_USERNAME: root
|
||||||
|
#MONGO_INITDB_ROOT_PASSWORD: example
|
||||||
|
MONGO_INITDB_DATABASE: local
|
||||||
|
volumes:
|
||||||
|
- "mongo:/data/db"
|
||||||
|
networks:
|
||||||
|
- webnet
|
||||||
|
|
||||||
|
mongo-express:
|
||||||
|
image: mongo-express
|
||||||
|
restart: no
|
||||||
|
depends_on:
|
||||||
|
- mongo
|
||||||
|
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_BASICAUTH: false
|
||||||
|
networks:
|
||||||
|
- webnet
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mongo:
|
||||||
|
driver: local
|
||||||
|
|
||||||
|
networks:
|
||||||
|
webnet:
|
|
@ -1,4 +1,3 @@
|
||||||
version: "3.8"
|
|
||||||
services:
|
services:
|
||||||
mysql:
|
mysql:
|
||||||
image: mysql:latest
|
image: mysql:latest
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
version: "3.8"
|
|
||||||
services:
|
services:
|
||||||
pgdb:
|
pgdb:
|
||||||
image: postgres:latest
|
image: postgres:latest
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
version: "3.8"
|
|
||||||
services:
|
services:
|
||||||
redis:
|
redis:
|
||||||
image: redis:latest
|
image: redis:latest
|
||||||
|
|
|
@ -3,9 +3,10 @@
|
||||||
## 8.2.0
|
## 8.2.0
|
||||||
|
|
||||||
* Require Dart >= 3.3
|
* Require Dart >= 3.3
|
||||||
* Updated `mongo_dart` to 0.10.2
|
* Updated `mongo_dart` to 0.10.3
|
||||||
* Updated `lints` to 3.0.0
|
* Updated `lints` to 4.0.0
|
||||||
* Fixed deprecated methods
|
* Refactored `mongo_service` class
|
||||||
|
* Refactored test cases
|
||||||
|
|
||||||
## 8.1.1
|
## 8.1.1
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ void main() async {
|
||||||
|
|
||||||
## MongoService
|
## MongoService
|
||||||
|
|
||||||
This class interacts with a `DbCollection` (from mongo_dart) and serializing data to and from Maps.
|
This class interacts with `DbCollection` (from mongo_dart) and serializing data to and from Maps.
|
||||||
|
|
||||||
## Querying
|
## Querying
|
||||||
|
|
||||||
|
@ -57,9 +57,20 @@ You can query these services as follows:
|
||||||
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.
|
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.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
List queried = await MyService.index({r"$query": where.id(new ObjectId.fromHexString("some hex string"})));
|
List queried = await MyService.index({r"$query": where.id(ObjectId.fromHexString("some hex string"})));
|
||||||
```
|
```
|
||||||
|
|
||||||
And, of course, you can use mongo_dart queries. Just pass it as `query` within `params`.
|
And, you can use mongo_dart queries. Just pass it as `query` within `params`.
|
||||||
|
|
||||||
See the tests for more usage examples.
|
See the tests for more usage examples.
|
||||||
|
|
||||||
|
## **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.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
var db = Db('mongodb://<username>:<password>@localhost:27017/local');
|
||||||
|
```
|
||||||
|
|
||||||
|
* `<username>` is MongoDB username
|
||||||
|
* `<password>` is MongoDB password
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
|
import 'package:angel3_container/mirrors.dart';
|
||||||
import 'package:angel3_framework/angel3_framework.dart';
|
import 'package:angel3_framework/angel3_framework.dart';
|
||||||
import 'package:angel3_mongo/angel3_mongo.dart';
|
import 'package:angel3_mongo/angel3_mongo.dart';
|
||||||
import 'package:mongo_dart/mongo_dart.dart';
|
import 'package:mongo_dart/mongo_dart.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
var app = Angel();
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
var db = Db('mongodb://localhost:27017/local');
|
var db = Db('mongodb://root:example@localhost:27017/local');
|
||||||
await db.open();
|
await db.open();
|
||||||
|
|
||||||
var service = app.use('/api/users', MongoService(db.collection('users')));
|
var service = app.use('/api/users', MongoService(db.collection('users')));
|
||||||
|
|
|
@ -90,7 +90,8 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const String _nonceKey = '__angel__mongo__nonce__key__';
|
// Deprecated:
|
||||||
|
//static const String _nonceKey = '__angel__mongo__nonce__key__';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Map<String, dynamic>> create(Map<String, dynamic> data,
|
Future<Map<String, dynamic>> create(Map<String, dynamic> data,
|
||||||
|
@ -98,13 +99,19 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
var item = _removeSensitive(data);
|
var item = _removeSensitive(data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var nonce = (await collection.db.getNonce())['nonce'] as String?;
|
if (params == null || params.isEmpty) {
|
||||||
var result = await (collection.findAndModify(
|
var result = await collection.insertOne(data);
|
||||||
query: where.eq(_nonceKey, nonce),
|
return _jsonify(result.document ?? {});
|
||||||
|
} else {
|
||||||
|
// Deprecated:
|
||||||
|
// var nonce = (await collection.db.getNonce())['nonce'] as String?;
|
||||||
|
var result = await collection.findAndModify(
|
||||||
|
query: _makeQuery(params),
|
||||||
update: item,
|
update: item,
|
||||||
returnNew: true,
|
returnNew: true,
|
||||||
upsert: true) as FutureOr<Map<String, dynamic>>);
|
upsert: true);
|
||||||
return _jsonify(result);
|
return _jsonify(result ?? {});
|
||||||
|
}
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
throw AngelHttpException(stackTrace: st);
|
throw AngelHttpException(stackTrace: st);
|
||||||
}
|
}
|
||||||
|
@ -150,10 +157,10 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
@override
|
@override
|
||||||
Future<Map<String, dynamic>> modify(String id, data,
|
Future<Map<String, dynamic>> modify(String id, data,
|
||||||
[Map<String, dynamic>? params]) async {
|
[Map<String, dynamic>? params]) async {
|
||||||
Map<String, dynamic> target;
|
Map<String, dynamic> currentDoc;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
target = await read(id, params);
|
currentDoc = await read(id, params);
|
||||||
} on AngelHttpException catch (e) {
|
} on AngelHttpException catch (e) {
|
||||||
if (e.statusCode == 404) {
|
if (e.statusCode == 404) {
|
||||||
return await create(data, params);
|
return await create(data, params);
|
||||||
|
@ -162,15 +169,13 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = mergeMap([target, _removeSensitive(data)]);
|
var updatedDoc = mergeMap([currentDoc, _removeSensitive(data)]);
|
||||||
//result['updatedAt'] = new DateTime.now().toIso8601String();
|
updatedDoc['updatedAt'] = DateTime.now().toIso8601String();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var modified = await (collection.findAndModify(
|
var modified = await collection.findAndModify(
|
||||||
query: where.id(_makeId(id)),
|
query: where.id(_makeId(id)), update: updatedDoc, returnNew: true);
|
||||||
update: result,
|
var result = _jsonify(modified ?? {}, params);
|
||||||
returnNew: true) as FutureOr<Map<String, dynamic>>);
|
|
||||||
result = _jsonify(modified, params);
|
|
||||||
result['id'] = _makeId(id).oid;
|
result['id'] = _makeId(id).oid;
|
||||||
return result;
|
return result;
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
|
@ -182,23 +187,17 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
@override
|
@override
|
||||||
Future<Map<String, dynamic>> update(String id, Map<String, dynamic> data,
|
Future<Map<String, dynamic>> update(String id, Map<String, dynamic> data,
|
||||||
[Map<String, dynamic>? params]) async {
|
[Map<String, dynamic>? params]) async {
|
||||||
var result = _removeSensitive(data);
|
var updatedDoc = _removeSensitive(data);
|
||||||
result['_id'] = _makeId(id);
|
|
||||||
/*result['createdAt'] =
|
|
||||||
target is Map ? target['createdAt'] : target.createdAt;
|
|
||||||
|
|
||||||
if (result['createdAt'] is DateTime)
|
updatedDoc['updatedAt'] = DateTime.now().toIso8601String();
|
||||||
result['createdAt'] = result['createdAt'].toIso8601String();
|
|
||||||
|
|
||||||
result['updatedAt'] = new DateTime.now().toIso8601String();*/
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var updated = await (collection.findAndModify(
|
var updated = await collection.findAndModify(
|
||||||
query: where.id(_makeId(id)),
|
query: where.id(_makeId(id)),
|
||||||
update: result,
|
update: updatedDoc,
|
||||||
returnNew: true,
|
returnNew: true,
|
||||||
upsert: true) as FutureOr<Map<String, dynamic>>);
|
upsert: true);
|
||||||
result = _jsonify(updated, params);
|
var result = _jsonify(updated ?? {}, params);
|
||||||
result['id'] = _makeId(id).oid;
|
result['id'] = _makeId(id).oid;
|
||||||
return result;
|
return result;
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
|
@ -225,10 +224,9 @@ class MongoService extends Service<String, Map<String, dynamic>> {
|
||||||
// var result = await read(id, params);
|
// var result = await read(id, params);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var result = await (collection.findAndModify(
|
var result = await collection.findAndModify(
|
||||||
query: where.id(_makeId(id)),
|
query: where.id(_makeId(id)), remove: true);
|
||||||
remove: true) as FutureOr<Map<String, dynamic>>);
|
return _jsonify(result ?? {});
|
||||||
return _jsonify(result);
|
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
//printDebug(e, st, 'REMOVE');
|
//printDebug(e, st, 'REMOVE');
|
||||||
throw AngelHttpException(stackTrace: st);
|
throw AngelHttpException(stackTrace: st);
|
||||||
|
|
|
@ -7,9 +7,10 @@ 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.0.0
|
||||||
|
angel3_container: ^8.0.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.2
|
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
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:angel3_container/mirrors.dart';
|
||||||
import 'package:angel3_framework/angel3_framework.dart';
|
import 'package:angel3_framework/angel3_framework.dart';
|
||||||
import 'package:angel3_framework/http.dart';
|
import 'package:angel3_framework/http.dart';
|
||||||
import 'package:angel3_mongo/angel3_mongo.dart';
|
import 'package:angel3_mongo/angel3_mongo.dart';
|
||||||
|
@ -25,18 +26,19 @@ 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/angel_mongo');
|
var db = Db('mongodb://localhost:27017/testingDB');
|
||||||
|
|
||||||
late DbCollection testData;
|
late DbCollection testData;
|
||||||
String? url;
|
String? url;
|
||||||
late HookedService<String, Map<String, dynamic>, MongoService>
|
late HookedService<String, Map<String, dynamic>, MongoService>
|
||||||
greetingService;
|
greetingService;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
app = Angel();
|
app = Angel(reflector: MirrorsReflector());
|
||||||
transport = AngelHttp(app);
|
transport = AngelHttp(app);
|
||||||
client = http.Client();
|
client = http.Client();
|
||||||
await db.open();
|
await db.open();
|
||||||
testData = db.collection('test_data');
|
testData = db.collection('testData');
|
||||||
// Delete anything before we start
|
// Delete anything before we start
|
||||||
await testData.remove(<String, dynamic>{});
|
await testData.remove(<String, dynamic>{});
|
||||||
|
|
||||||
|
@ -126,30 +128,30 @@ void main() {
|
||||||
var response = await client.post(Uri.parse('$url/api'),
|
var response = await client.post(Uri.parse('$url/api'),
|
||||||
body: god.serialize(testGreeting), headers: headers);
|
body: god.serialize(testGreeting), headers: headers);
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
var created = god.deserialize(response.body) as Map;
|
var createdDoc = god.deserialize(response.body) as Map;
|
||||||
|
|
||||||
response = await client.patch(Uri.parse("$url/api/${created['id']}"),
|
response = await client.patch(Uri.parse("$url/api/${createdDoc['id']}"),
|
||||||
body: god.serialize({'to': 'Mom'}), headers: headers);
|
body: god.serialize({'to': 'Mom'}), headers: headers);
|
||||||
var modified = god.deserialize(response.body) as Map;
|
var modifiedDoc = god.deserialize(response.body) as Map;
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
expect(modified['id'], equals(created['id']));
|
expect(modifiedDoc['id'], equals(createdDoc['id']));
|
||||||
expect(modified['to'], equals('Mom'));
|
expect(modifiedDoc['to'], equals('Mom'));
|
||||||
//expect(modified['updatedAt'], isNot(null));
|
expect(modifiedDoc['updatedAt'], isNot(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('update item', () async {
|
test('update item', () async {
|
||||||
var response = await client.post(Uri.parse('$url/api'),
|
var response = await client.post(Uri.parse('$url/api'),
|
||||||
body: god.serialize(testGreeting), headers: headers);
|
body: god.serialize(testGreeting), headers: headers);
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
var created = god.deserialize(response.body) as Map;
|
var createdDoc = god.deserialize(response.body) as Map;
|
||||||
|
|
||||||
response = await client.post(Uri.parse("$url/api/${created['id']}"),
|
response = await client.post(Uri.parse("$url/api/${createdDoc['id']}"),
|
||||||
body: god.serialize({'to': 'Updated'}), headers: headers);
|
body: god.serialize({'to': 'Updated'}), headers: headers);
|
||||||
var modified = god.deserialize(response.body) as Map;
|
var modifiedDoc = god.deserialize(response.body) as Map;
|
||||||
expect(response.statusCode, isIn([200, 201]));
|
expect(response.statusCode, isIn([200, 201]));
|
||||||
expect(modified['id'], equals(created['id']));
|
expect(modifiedDoc['id'], equals(createdDoc['id']));
|
||||||
expect(modified['to'], equals('Updated'));
|
expect(modifiedDoc['to'], equals('Updated'));
|
||||||
//expect(modified['updatedAt'], isNot(null));
|
expect(modifiedDoc['updatedAt'], isNot(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('remove item', () async {
|
test('remove item', () async {
|
||||||
|
|
Loading…
Reference in a new issue