项目作者: 1011-1-000

项目描述 :
Flask-Records is an extension that manuiplate the DB with the raw sql for the flask application using records.
高级语言: Python
项目地址: git://github.com/1011-1-000/flask-records.git
创建时间: 2019-10-04T13:11:41Z
项目社区:https://github.com/1011-1-000/flask-records

开源协议:MIT License

下载


Flask Records

PyPI - Python Version
Build Status

Flask-Records is an extension that manuiplate the DB with the raw sql for the flask application using records.

Installation

pip install flask-records

Usage

Setup

Add the flask-records to your flask application.

  1. from flask import Flask
  2. from flask_records import FlaskRecords
  3. raw_db = FlaskRecords(app)

or initialize the app in the way below:

  1. from flask import Flask
  2. from flask_records import FlaskRecords
  3. raw_db = FlaskRecords()
  4. raw_db.init_app(app)

Access DB With Flask Records

We have provided two decorators for easy using in the development:

query

The basic query decorator you can use in the flask records.

  1. from flask_records.decorators import query
  2. @query("INSERT INTO users VALUES(:id, :name, :age)")
  3. def hello_flask_records(id, name, age):
  4. pass
  5. # call the function: hello_flask_records(1, 'Leo', 27)

also, you can wrap all the parameters in a dictionary and pass it to the function.

  1. from flask_records.decorators import query
  2. @query( "INSERT INTO users VALUES(:id, :name, :age)")
  3. def hello_flask_records(parameters):
  4. pass
  5. # define a dict which contains the query parameters
  6. # parameters = {
  7. # 'id': 1,
  8. # 'name': 'Leo',
  9. # 'age': 27
  10. # }
  11. # call the function: hello_flask_records(parameters)
query_by_page

This is the decorator which for the pagination.

  1. from flask_records.decorators import query_by_page
  2. @query_by_page("SELECT * FROM users", 2)
  3. def hello_flask_records(page):
  4. pass

Extra Features

Flask-Records also provide the basic crud function, all you need to do is inherit the RecordsDao when you write the DAO layer classes.

  1. from flask_records import RecordsDao
  2. class UserDao(RecordsDao):
  3. def __init__(self):
  4. super(UserDao, self).__init__()

For detailed instructions on the features, please refer to the flask records documentation.

ChangeLog

Release 0.0.15

  • add a param converters in as_dict function, which means u can do the convertion according to your requirements. the format for this param is {‘key’: func}
  • add bulk_query decorator, so you can insert multiple rows to the db
  • add as_df function to the RecordCollection

Release 0.0.14

  • Fix the initialize issue in the way like FlaskRecords()
  • Modified the error in the Doc

Release 0.0.9

  • Support the default params in the function when do the query
  • Compatible with the Python2.7+ and 3.5+

Release 0.0.8

  • Support the decorators on the function or method in the class
  • Simplify the usage of the decorators
  • Provide the base crud operations
  • Add unit tests for the decorators, basic dao etc