Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/ flask-pymongo Public archive

Python (Flask) and Mongo DB Restful Micro Service Architecture

License

Notifications You must be signed in to change notification settings

akmamun/flask-pymongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Model Based Restful Flask and Mongo DB

Note: Front-end React API Integration Here react-api-integration

Model based restful flask (python) and mongo db. Integrate with any API development.

Create virtual environment and install requirements

pip install -r requirements.txt

Configure Database

From db_config.json configure datbase url, name, user and password

 {
   "db": {
            "url" : "mongodb://localhost:27017/",
            "name" :"db_name",  
            "user" :"",
            "password" :""
    }
 }

In model update collection name and desire fields name and fields type. For example see todo model file

From model folder write your individual model and configure db collection name, fields name and fields type

Example

In todo model update collection name, fields name and fields type
collection_name = 'todos'   # collection name
fields = {   
    "title"     : "string",
    "body"      : "string",
    "created"   : "datatime"
} 
Update required fields, optional fields from todo model
create_required_fields = []  # example create_required_fields = ["title", "body"]
create_optional_fields = []  # example create_required_fields = ["created"]
update_required_fields = []
update_optional_fields = []

Example

create_required_fields = ["title", "body"] 
create_optional_fields = []  
update_required_fields = ["title", "body"]
update_optional_fields = []

In Database insert, find , find_by_id, update and delete methods are generalize methods.

Those methods are call from model

  • insert method store data to database after confirm validation from model
  • find method retries all data from mongo database
  • find_by_id method retries back a single search data
  • update method store updated data to database with corresponding id
  • and delete method delete data from database with corresponding id

List of Todo Routes

Request Endpoint Details
GET http://127.0.0.1:5000/api/v1/todos Get All
GET http://127.0.0.1:5000/api/v1/todos/todo_id Get Single Id
POST http://127.0.0.1:5000/api/v1/todos Insert One
PUT http://127.0.0.1:5000/api/v1/todos/todo_id Update One
DELETE http://127.0.0.1:5000/api/v1/todos/todo_id Delete One
  • To see route list type cli flask routes

Lets run the App

python app.py