platform/README.md

58 lines
1.6 KiB
Markdown
Raw Normal View History

2016-05-09 21:16:44 +00:00
# angel_mongo
2016-06-23 04:58:21 +00:00
2017-07-09 18:07:02 +00:00
[![Pub](https://img.shields.io/pub/v/angel_mongo.svg)](https://pub.dartlang.org/packages/angel_mongo)
[![build status](https://travis-ci.org/angel-dart/mongo.svg)](https://travis-ci.org/angel-dart/mongo)
2016-06-23 04:58:21 +00:00
2016-05-09 21:16:44 +00:00
MongoDB-enabled services for the Angel framework.
2016-06-23 04:58:21 +00:00
# Installation
Add the following to your `pubspec.yaml`:
```yaml
dependencies:
2018-10-18 23:06:17 +00:00
angel_mongo: ^2.0.0
2016-06-23 04:58:21 +00:00
```
# Usage
2017-02-23 01:00:25 +00:00
This library exposes one main class: `MongoService`.
2016-06-23 04:58:21 +00:00
## Model
2017-02-20 16:00:42 +00:00
`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.
2016-06-23 04:58:21 +00:00
```dart
class User extends Model {
String username;
String password;
}
2016-06-23 19:59:10 +00:00
2016-09-21 04:26:22 +00:00
main() async {
2018-10-18 23:06:17 +00:00
var db = new Db('mongodb://localhost:27017/local');
2016-09-21 04:26:22 +00:00
await db.open();
2018-10-18 23:06:17 +00:00
var service = app.use('/api/users', new MongoService(db.collection("users")));
2016-09-21 04:26:22 +00:00
2018-10-18 23:06:17 +00:00
service.afterCreated.listen((event) {
2016-09-21 04:26:22 +00:00
print("New user: ${event.result}");
});
}
2016-06-23 04:58:21 +00:00
```
## MongoService
This class interacts with a `DbCollection` (from mongo_dart) and serializing data to and from Maps.
2016-06-23 19:59:10 +00:00
## Querying
You can query these services as follows:
/path/to/service?foo=bar
2017-02-13 01:38:24 +00:00
The above will query the database to find records where 'foo' equals 'bar'.
2016-06-23 19:59:10 +00:00
The former will sort result in ascending order of creation, and so will the latter.
List queried = await MyService.index({r"$query": where.id(new ObjectId.fromHexString("some hex string"})));
And, of course, you can use mongo_dart queries. Just pass it as `query` within `params`.
2016-06-23 04:58:21 +00:00
See the tests for more usage examples.