6. Web API - Flask-DStore

6.1. Introduction

Flask-DStore is a Web API and Javascript Client.

The API routes, logic and client code is automatically generated for you.

6.2. Installing

You have two choices to install dstore-mysql, using pip or from source.

6.2.1. From PyPi

DStore is available from the PyPi repository at https://pypi.python.org/pypi/flask-dstore.

This means that all you have to do to install PyMan is run the following in a console:

$ pip install flask-dstore

6.2.2. From Source

DStore can also be installed from source by downloading from GitHub and running setup.py.

$ wget https://github.com/MarkLark/flask-dstore/archive/master.tar.gz
$ tar xvf master.tar.gz
$ cd flask-dstore-master
$ python setup.py install

6.3. Minimal Example

from flask import Flask
from dstore import MemoryStore, Model, var, mod
from flask_dstore import API

class Car( Model ):
    _namespace = "cars.make"
    _vars = [
        var.RowID,
        var.String( "manufacturer", 32, mods = [ mod.NotNull() ] ),
        var.String( "make", 32, mods = [ mod.NotNull() ] ),
        var.Number( "year", mods = [ mod.NotNull(), mod.Min( 1950 ), mod.Max( 2017 ) ] ),
    ]

# Create the app instances
app = Flask( __name__ )
store = MemoryStore( [ Car ] )
api = API( store, app )

# While inside the Flask app context, create all storage and add a car
with app.app_context():
    store.create_all()
    Car( manufacturer = "Holden", make = "Commodore", year = 2005 ).add()

# Run the Flask dev. server
app.run()

# Now destroy all data
with app.app_context():
    store.destroy_all()

store.destroy_app()

6.4. Javascript Client

Flask-DStore provides a javascript library that allows you to easily create, read, update and delete model instances within Javascript, for every Model type registered, a Factory class is created.

To include this library, add the following to your base html template

<script src="/dstore-client.js"></script>
<script src="/dstore-models.js"></script>
<script src="/dstore-view.js"></script>

6.4.1. DS.Factory

This Factory contains the following methods:

class DS.Factory()
load_all()

Load all Model instances from the server into browser memory

load(id)

Load a specific Model Instance from the server into the browser memory

Arguments:
  • id (int) – The instance ID to retrieve
get(id)

Get a Model Instance from browser memory, null if it doesn’t exist

Arguments:
  • id (int) – The instance ID to retrieve
add(args)

Add a new Model Instance into browser memory.

You need to execute save on the returned object to save the instance to the server.

Arguments:
  • args – A dictionary of values to store in the Model Instance

6.4.2. DS.Model

The Model class is as follows:

class DS.Model()
save()

Save this Model Instance to the Server.

delete()

Delete the Model Instance from the server, and local browser memory.

6.4.3. Example Usage

You don’t directly use the DS.Factory and DS.Model class’ directly. Instead Flask-DStore generates instances of these class’ for every Model type registered.

These are then stored under the ‘ds’ namespace, proceeded by the namespace of the Model types.

The following are examples of how to use this library with the following Model:

from dstore import MemoryStore, Model, var, mod

class Car( Model ):
    _namespace = "cars.make"
    _vars = [
        var.RowID,
        var.String( "manufacturer", 32, mods = [ mod.NotNull() ] ),
        var.String( "make", 32, mods = [ mod.NotNull() ] ),
        var.Number( "year", mods = [ mod.NotNull(), mod.Min( 1950 ), mod.Max( 2017 ) ] ),
    ]

6.4.3.1. Load All

ds.cars.make.load_all();
car = ds.cars.make.add({ manufacturer: "Holden", make: "Commodore", year: 2010 });
car.save();

6.4.3.2. Create

ds.cars.make.load_all();
car = ds.cars.make.add({ manufacturer: "Holden", make: "Commodore", year: 2010 });
car.save();

6.4.3.3. Update

ds.cars.make.load_all();
car = ds.cars.make.get(1);
car.year = 2011;
car.save();

6.4.3.4. Delete

ds.cars.make.load_all();
car = ds.cars.make.get(1);
car.delete();