https://github.com/floatingghost/flask-mongoengine-autocrud
Automatically generate a CRUD blueprint for a mongoengine object
https://github.com/floatingghost/flask-mongoengine-autocrud
automated flask mongodb mongoengine python
Last synced: 7 months ago
JSON representation
Automatically generate a CRUD blueprint for a mongoengine object
- Host: GitHub
- URL: https://github.com/floatingghost/flask-mongoengine-autocrud
- Owner: FloatingGhost
- Created: 2018-12-08T16:54:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-08T17:33:10.000Z (over 7 years ago)
- Last Synced: 2025-06-18T12:46:54.195Z (9 months ago)
- Topics: automated, flask, mongodb, mongoengine, python
- Language: Python
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Flask Mongoengine Autocrud
Need more crud in your life?
me too.
The idea behind this is that, given a mongoengine type,
we can autogenerate a standard set of REST endpoints to
facilitate creation, modification, deletion and readification.
Maybe search too. I think.
### Usage
```python3
# In your blueprints folder
from flask_mongoengine_autocrud import create_crud
from models.my_model import SuperGoodModel
my_blueprint = create_crud(
SuperGoodModel,
"super_good_blueprint",
__name__
)
```
and theoretically that's... it
Register the blueprint with flask in the standard way, and
should be good to go.
Now we can use standard REST calls to do stuff
The following endpoints are created
| Method | URL Pattern | Returns | Options |
|--------|-------------|---------|--------|
| GET | / | List of all items | page (int), size (int), for pagination |
| GET | /\ | A single item | |
| POST | / | The created item | All valid mongoengine field names in POST body |
| PATCH | /\ | The modified item | All valid mongoengine field names |
| DELETE | /\ | Success status | |
| POST | /search | List of matching items | See search section |
For example
```bash
$ curl -XPOST /mydatatype/
--header "Content-Type: application/json" \
--data-binary '{"title": "my data", "admin": true}'
http://myserver
{"title": "my data", "admin": true, "_id": "1234"}
$ curl -XPATCH /mydatatype/1234
--header "Content-Type: application/json" \
--data-binary '{"admin": false}'
http://myserver
{"title": "my data", "admin": false, "_id": "1234"}
$ curl -XPOST /mydatatype/search
--header "Content-Type: application/json" \
--data-binary '{"filter": {"admin": false}}'
http://myserver
[{"title": "my data", "admin": false, "_id": "1234"}]
$ curl -XDELETE /mydatatype/1234
--header "Content-Type: application/json" \
--data-binary '{"filter": {"admin": false}}'
http://myserver
{"success": true}
```
### Search
Search options should be posted as a JSON body in the following format
All fields are optional
```json
{
"filter": {
"field_name": "field_value",
...
},
"sort": {
"field_name": "direction (desc or asc)"
},
"size": integer,
"page": integer
}
```
The `page` field *requires* the size field, else it'll be ignored.