Skip to main content

Automatically generate a RESTful API service for CRUD operation on database

Project description

Based on sandman2 and sqlalchemy-filters.

Automatically generate a RESTful API service for CRUD operation on database. If a list of tables or a list of sqlalchemy model is not provided, all tables are affected.

Quickstart

Install flask_autocrud using pip:

$ pip install Flask-AutoCRUD

Example usage

from flask import Flask

from flask_autocrud import AutoCrud
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite+pysqlite:///db.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['AUTOCRUD_METADATA_ENABLED'] = True

db = SQLAlchemy(app)
AutoCrud(app, db)

app.run(debug=True)

Go to http://127.0.0.1:5000/resources and see all available resources with its endpoint. NOTE: you must set SQLALCHEMY_DATABASE_URI with your database.

If you want to see an example use with Flask-Admin see in example folder.

Filtering and Sorting

Add filters as query string parameters, they are used in AND. NOTE: At this time OR operator are not implemented.

You can use entity fields as parameter with the following placeholders:

  • null value: null

  • in operator: list separated by ;

  • not operator: ! means: not equal, not null, not in

  • like operator: % for example: %%test%, %test% or %%test. NOTE first % are not used in expression, it only indicated that value must be used with like operator.

Other parameters:

  • Use fields parameter to get only the fields listes as value, separated by ;.

  • Use limit and page parameters for pagination.

  • Sorting is implemented with sort parameter. The value is a list of field separated by ; You can prepend - to reverse order.

  • Use export parameter to export data into csv format.

  • Use extended in order to fetch data of related resources.

Example requests:

http://127.0.0.1:5000/invoice?fields=BillingCountry;Total;InvoiceId&InvoiceId=!355;344&sort=-InvoiceId

http://127.0.0.1:5000/invoice?fields=Total;InvoiceId&BillingPostalCode=!null&BillingCountry=%%ermany

http://127.0.0.1:5000/invoice?fields=Total;InvoiceDate;InvoiceId;CustomerId&page=2&limit=10

Example FETCH:

curl --request FETCH \
    --url http://127.0.0.1:5000/customer \
    --header 'content-type: application/json' \
    --data '{
        "fields": [
            "Address",
            "City"
        ],
        "joins": {
            "Employee": [
                "FirstName",
                "LastName"
            ],
            "Invoice": [
                "*"
            ]
        },
        "filters": [
            {
                "model": "Customer",
                "field": "SupportRepId",
                "op": "==",
                "value": 5
            },
            {
                "model": "Invoice",
                "field": "Total",
                "op": ">",
                "value": 6
            }
        ],
        "sortBy": [
            {
                "model": "Invoice",
                "field": "Total",
                "direction": "asc"
            },
            {
                "model": "Customer",
                "field": "Address",
                "direction": "desc"
            }
        ],
        "pagination": {
            "page": 1,
            "limit": 5
        }
    }'

Configuration

  1. AUTOCRUD_METADATA_ENABLED: (default: True) enable metadata endpoint for a resource

  2. AUTOCRUD_METADATA_URL: (default: ‘/meta) added at the end of url resource

  3. AUTOCRUD_READ_ONLY: (default: False) enable only http GET method

  4. AUTOCRUD_BASE_URL: (default: ‘’) prefix url for resources

  5. AUTOCRUD_RESOURCES_URL: (default: ‘/resources’) url for all available resources

  6. AUTOCRUD_RESOURCES_URL_ENABLED: (default: True) enable route for resources list

  7. AUTOCRUD_SUBDOMAIN: (default: None) bind autocrud endpoints to a subdomain

  8. AUTOCRUD_QUERY_LIMIT_ENABLED: (default True) enable max query limit

  9. AUTOCRUD_MAX_QUERY_LIMIT: (default 1000) max query limit

  10. AUTOCRUD_FETCH_ENABLED: (default True) enable or disable FETCH method

License MIT

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

Flask-AutoCRUD-1.2.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

Flask_AutoCRUD-1.2.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask-AutoCRUD-1.2.1.tar.gz.

File metadata

  • Download URL: Flask-AutoCRUD-1.2.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for Flask-AutoCRUD-1.2.1.tar.gz
Algorithm Hash digest
SHA256 3980ba46f5a9335d4998581be402159781a139c2a16f636775eee4965c8d5241
MD5 7c27fd63db22d44ab4187f61707e0fc3
BLAKE2b-256 28d937b4985df47760f0dbfd1e7fcbf4aa2bbc2004bda2f209cde8964ee09ad5

See more details on using hashes here.

File details

Details for the file Flask_AutoCRUD-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: Flask_AutoCRUD-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for Flask_AutoCRUD-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9107b505c71bf0c9c3aea108da40842579875399b2f924b0c0c058c5ca1d393c
MD5 22b05582f5c09eeef9ce35a87328e99b
BLAKE2b-256 8bb88123bd0f5d41590d108a88553ed0f3446db8d491a8491f07b5834f3ef439

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