2021-09-25 06:32:32 +00:00
# Angel3 Mongo
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/angel3_mongo?include_prereleases )
2021-06-18 11:10:38 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
[![Gitter ](https://img.shields.io/gitter/room/angel_dart/discussion )](https://gitter.im/angel_dart/discussion)
2023-12-25 03:45:10 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/angel )](https://github.com/dart-backend/angel/tree/master/packages/mongo/LICENSE)
2020-02-15 23:43:58 +00:00
2021-06-18 11:10:38 +00:00
MongoDB-enabled services for the Angel3 framework.
2020-02-15 23:43:58 +00:00
2021-09-25 06:32:32 +00:00
## Installation
2020-02-15 23:43:58 +00:00
Add the following to your `pubspec.yaml` :
```yaml
dependencies:
2023-12-24 16:10:10 +00:00
angel3_mongo: ^8.0.0
2020-02-15 23:43:58 +00:00
```
2021-09-25 06:32:32 +00:00
## Usage
2020-02-15 23:43:58 +00:00
This library exposes one main class: `MongoService` .
## Model
2021-09-25 06:32:32 +00:00
2023-12-24 16:10:10 +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.
2020-02-15 23:43:58 +00:00
```dart
class User extends Model {
String username;
String password;
}
2021-06-18 11:10:38 +00:00
void main() async {
var db = Db('mongodb://localhost:27017/local');
2020-02-15 23:43:58 +00:00
await db.open();
2021-06-18 11:10:38 +00:00
var service = app.use('/api/users', MongoService(db.collection("users")));
2020-02-15 23:43:58 +00:00
service.afterCreated.listen((event) {
print("New user: ${event.result}");
});
}
```
## MongoService
2021-09-25 06:32:32 +00:00
2024-06-23 03:15:41 +00:00
This class interacts with `DbCollection` (from mongo_dart) and serializing data to and from Maps.
2020-02-15 23:43:58 +00:00
## Querying
2021-09-25 06:32:32 +00:00
2020-02-15 23:43:58 +00:00
You can query these services as follows:
2023-12-24 16:10:10 +00:00
```curl
2020-02-15 23:43:58 +00:00
/path/to/service?foo=bar
2021-09-25 06:32:32 +00:00
```
2020-02-15 23:43:58 +00:00
2023-12-24 16:10:10 +00:00
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.
2020-02-15 23:43:58 +00:00
2021-09-25 06:32:32 +00:00
```dart
2024-06-23 03:15:41 +00:00
List queried = await MyService.index({r"$query": where.id(ObjectId.fromHexString("some hex string"})));
2021-09-25 06:32:32 +00:00
```
2020-02-15 23:43:58 +00:00
2024-06-23 03:15:41 +00:00
And, you can use mongo_dart queries. Just pass it as `query` within `params` .
2020-02-15 23:43:58 +00:00
See the tests for more usage examples.
2024-06-23 03:15:41 +00:00
## **Important Notes**
2024-07-06 10:15:18 +00:00
When connecting to a locally installed instance of MongoDB or docker based MongoDB with authentication enabled, the following connection string is not supported yet.
2024-06-23 03:15:41 +00:00
```dart
2024-07-06 10:15:18 +00:00
var db = Db('mongodb://< username > :< password > @localhost:27017/testDB');
2024-07-04 16:04:58 +00:00
await db.open();
```
2024-07-06 10:15:18 +00:00
Use the following instead. By default the user access information is stored in `admin` database.
2024-07-04 16:04:58 +00:00
```dart
var db = Db('mongodb://localhost:27017/testDB');
await db.open();
await db.authenticate("< username > ", "< password > ", authDb: "admin");
2024-06-23 03:15:41 +00:00
```
2024-07-06 10:15:18 +00:00
Where
2024-06-23 03:15:41 +00:00
* `<username>` is MongoDB username
* `<password>` is MongoDB password