Skip to main content

A middleware module which solves common problems when building rest-apis with nosql databases.

Project description

NoSQL Rest Preprocessor
=======================
[![Build Status](https://travis-ci.org/felixbr/nosql-rest-preprocessor.svg?branch=master)](https://travis-ci.org/felixbr/nosql-rest-preprocessor)
[![Coverage Status](https://img.shields.io/coveralls/felixbr/nosql-rest-preprocessor.svg)](https://coveralls.io/r/felixbr/nosql-rest-preprocessor)

A middleware module which solves common problems when building rest-apis with nosql databases.

### Installation

```
pip install nosql-rest-preprocessor
```

### Examples

```python
from nosql-rest-preprocessor.models import BaseModel

class AddressModel(BaseModel):
required_attributes = {'street', 'city', 'plz'}

# if you specify optional attributes explicitely, it will
# not allow anything but these and the required ones
optional_attributes = {'location'}

class UserModel(BaseModel):
required_attributes = {'firstName', 'lastName', 'email'}
immutable_attributes = {'id'}
private_attributes = {'password', 'salt'}

# will use AddressModel to validate, merge and prepare the content of the 'address' attribute
sub_models: {
'address': AddressModel
}
```

```python
new_user_from_request = {
"firstName": "Sepp",
"lastName": "Huber",
"email": "sepp.huber@fancepants.com",
"address": {
"street": "Bakerstreet 1",
"city": "London",
"plz": "12345"
}
}

# checks required_attributes and raises ValidationError if something's amiss
UserModel.validate(new_user_from_request)

```

```python
user_obj_from_db = db.fetch_user_by_email("sepp.huber@fancepants.com")

# strips out any non-public attributes
response_obj = UserModel.prepare_response(user_obj_from_db)

return Response(response_obj)
```

```python
from nosql-rest-preprocessor.models import BaseModel
from nosql-rest-preprocessor.resolvers import resolve, ResolveWith

class AddressModel(BaseModel):
...

class UserModel(BaseModel):
...

resolved_attributes = {
'address' = ResolveWith(lookup_func=SomeDB.find_address_by_key, model=AddressModel)
}

...

user = {
"name": "cookie_m0nster",
"address": "foreign_key_for_address"
}

resolved_obj = resolve(UserModel, user)
# resolved_obj['address'] is now replaced by the dict fetched by SomeDB.find_address_by_key('foreign_key_for_address')
```

### Running tests
```
pip install detox

detox
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nosql-rest-preprocessor-0.3.zip (8.4 kB view details)

Uploaded Source

File details

Details for the file nosql-rest-preprocessor-0.3.zip.

File metadata

File hashes

Hashes for nosql-rest-preprocessor-0.3.zip
Algorithm Hash digest
SHA256 f6c5d11df70d0dee96a156c68559e4c6e7c07714ca4fefae9267cffb1cdc5417
MD5 a037621fd0b6e854eafdc4cfee7a36c1
BLAKE2b-256 2fd0814d1e61464b32c3cb0c8b34c87baedfd0e83c3083050cc521507b72c0cd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page