https://github.com/ketgo/nameko-mongoengine
MongoEngine dependency provider for Nameko microservice framework
https://github.com/ketgo/nameko-mongoengine
database microservices mongodb mongoengine nameko nosql
Last synced: 9 days ago
JSON representation
MongoEngine dependency provider for Nameko microservice framework
- Host: GitHub
- URL: https://github.com/ketgo/nameko-mongoengine
- Owner: ketgo
- License: apache-2.0
- Created: 2019-12-03T06:45:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-07T06:41:34.000Z (about 4 years ago)
- Last Synced: 2026-03-27T07:28:03.488Z (about 1 month ago)
- Topics: database, microservices, mongodb, mongoengine, nameko, nosql
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
# nameko-mongoengine
[](https://travis-ci.com/ketgo/nameko-mongoengine)
[](https://codecov.io/gh/ketgo/nameko-mongoengine/coverage.svg?branch=master)
[](LICENSE)
---
MongoEngine dependency provider for [Nameko](https://github.com/nameko/nameko) microservice framework.
## Installation
```bash
pip install nameko-mongoengine
```
## Usage
The basic usage of the dependency provider is shown:
```python
from mongoengine import Document, fields
from nameko_mongoengine import MongoEngine
from nameko.rpc import rpc
class MyModel(Document):
"""
My document model
"""
info = fields.StringField(required=True)
class MockService:
name = "mock_service"
engine = MongoEngine()
@rpc
def write(self, info):
model = MyModel()
model.info = info
model.save()
return model
@rpc
def read(self, _id):
return MyModel.objects.get(id=_id)
```
The dependency `engine` exposes standard `pymongo` interface to database connections. The default connection can be accessed by the `db` property:
```python
class MockService:
name = "mock_service"
engine = MongoEngine()
@rpc
def get(self, _id):
return self.engine.db.your_collection.find_one({'_id': _id})
```
Other database connections defined by `MongoEngine` aliases can be accessed by:
```python
@rpc
def get(self, _id):
db = self.engine.with_alias("your_alias").db
return db.your_collection.find_one({'_id': _id})
```
See full [example](https://github.com/ketgo/nameko-mongoengine/tree/master/example) for more details.
## Configurations
The dependency configurations can be set in nameko `config.yaml` [file](https://docs.nameko.io/en/stable/cli.html), or by environment variables.
### Config File
```yaml
MONGODB_URI: mongodb://localhost:27017/dbname?replicaSet=replset
# or
# ---- with aliases
MONGODB_URI:
default: mongodb://localhost:27017/dbname?replicaSet=replset
"": ""
```
### Environment Variables
```.env
MONGODB_URI='mongodb://localhost:27017/dbname?replicaSet=replset'
# or
# ---- with aliases
MONGODB_URI='{"default": "mongodb://localhost:27017/dbname?replicaSet=replset", "": ""}'
```
## Developers
To perform development tasks and run tests run:
```bash
$ pip install -e .[dev] # to install all dependencies
$ docker run -d --restart=always --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management # Run rabbitmq-management server
$ docker run --rm -d -p 27017:27017 mongo # Run mongodb server on docker
$ pytest --cov=nameko_mongoengine tests/ # to get coverage report
$ pylint nameko_mongoengine # to check code quality with PyLint
```
Optionally you can use `make`.
## Contributions
Pull requests always welcomed. Thanks!