The flask-neon-kit is a Flask extension which generates CRUD endpoints out of the box from defined models of the Neon Postgres database. It empowers developers with resources' CRUD endpoints by just defining database models and instantiating the kit. This solution is built upon the foundation of the Flask-SQLAlchemy. This initiative was driven by the tedious nature of manually writing CRUD logic for every Flask application entity. It was also motivated by flask-mongo-crud, the package I am working on that automatically generates CRUD endpoints for MongoDB models.
Features of the solution are:
Automatically generates CRUD endpoints from defined model.
Allows to customize app base URL as well as each model’s url prefix.
Allows to paginate when getting many entities from database table.
Prerequisites for using the kit:
Familiarity with implementing Flask Applications.
Familiarity with integrating Flask Applications with Postgres database using Flask-SQLAlchemy.
Models directory is required in the project root directory:
If custom name Models directory is not defined, as:
app.config[MODELS_DIRECTORY]="<CUSTOM_NAME>"
then, default “models” directory will be used.
This is where models files are defined.
Inside these files declare models classes and their configurations such as:
model_url_prefix [OPTIONAL]
and those supported by Flask SQLAlchemy
model_url_prefix allows to have unique URLs for each and every model.
If this configuration is not defined, default configuration will be used.
Model Class Code Snippet (professor_subject.py):
classProfessorSubject:model_url_prefix="/professor-subject-test"id=db.Column(db.Integer,primary_key=True,autoincrement=True)professor_first_name=db.Column(db.String())professor_last_name=db.Column(db.String())subject_name=db.Column(db.String())# These are entity fields
def__init__(self,professor_first_name,professor_last_name,subject_name):self.professor_first_name=professor_first_nameself.professor_last_name=professor_last_nameself.subject_name=subject_name
Basic Application
Code Snippet (app.py):
fromflaskimportFlask,requestfromflask_sqlalchemyimportSQLAlchemyfrom.configimportDbConfigfromflask_neon_kitimportFlaskNeonKitapp=Flask(__name__)# If models directory is not defined, default "models" directory will be used
app.config["MODELS_DIRECTORY"]="<CUSTOM_NAME>"# If root URL is not defined, generated endpoints will not have a root URL
app.config["ROOT_URL"]="/flask-neon-kit/v1"app.config.from_object(DbConfig)# Database related part
db=SQLAlchemy()db.init_app(app)flask_neon_kit=FlaskNeonKit()flask_neon.init_app(app,request,db)if__name__=="__main__":app.run(debug=True)
Generated Endpoints Examples and HTTP Methods:
HTTP Methods supported are POST, GET, PUT and DELETE.
The following generated endpoints will be using model snippet defined earlier in Configuration as well Basic Application sections.
These endpoints are after application base URL:
<IP_ADDRESS>:<PORT_NUMBER>
Main objective of this solution is to minimize the tedious nature of manually writing common CRUD logic for every Flask application entity. There is also a plan to integrate some cool Neon features like data branches management in the future. This will enable developers to switch among their data branches to work with different versions of their data whilst in their application development environment. This should have been integrated in this version but seems Neon API currently has some limitations to support such features. I learned a lot during the course, and the concept of data branches is one of the features I am looking forward into exploring in the future.