DataStores¶
-
class
rest_api_framework.datastore.base.
DataStore
(ressource_config, model, **options)[source]¶ define a source of data. Can be anything fron database to other api, files and so one
-
get
(identifier)[source]¶ Should return a dictionnary representing the ressource matching the identifier or raise a NotFound exception.
Note
Not implemented by base DataStore class
-
create
(data)[source]¶ data is a dict containing the representation of the ressource. This method should call
validate()
, create the data in the datastore and return the ressource identifierNot implemented by base DataStore class
-
update
(obj, data)[source]¶ should be able to call
get()
to retreive the object to be updated,validate_fields()
and return the updated objectNote
Not implemented by base DataStore class
-
delete
(identifier)[source]¶ should be able to validate the existence of the object in the ressource and remove it from the datastore
Note
Not implemented by base DataStore class
-
get_list
(offset=None, count=None, **kwargs)[source]¶ This method is called each time you want a set of data. Data could be paginated and filtered. Should call
filter()
and returnpaginate()
Note
Not implemented by base DataStore class
-
filter
(**kwargs)[source]¶ should return a way to filter the ressource according to kwargs. It is not mandatory to actualy retreive the ressources as they will be paginated just after the filter call. If you retreive the wole filtered ressources you loose the pagination advantage. The point here is to prepare the filtering. Look at SQLiteDataStore.filter for an example.
Note
Not implemented by base DataStore class
-
paginate
(data, offset, count)[source]¶ Paginate sould return all the object if no pagination options have been set or only a subset of the ressources if pagination options exists.
-
-
class
rest_api_framework.datastore.simple.
PythonListDataStore
(ressource_config, model, **options)[source]¶ Bases:
rest_api_framework.datastore.base.DataStore
a datastore made of list of dicts
-
class
rest_api_framework.datastore.sql.
SQLiteDataStore
(ressource_config, model, **options)[source]¶ Bases:
rest_api_framework.datastore.base.DataStore
Define a sqlite datastore for your ressource. you have to give __init__ a data parameter containing the information to connect to the database and to the table.
example:
data={"table": "tweets", "name": "test.db"} model = ApiModel datastore = SQLiteDataStore(data, **options)
SQLiteDataStore implement a naive wrapper to convert Field types into database type.
- int will be saved in the database as INTEGER
- float will be saved in the database as REAL
- basestring will be saved in the database as TEXT
- if the Field type is PKField, is a will be saved as PRIMARY KEY AUTOINCREMENT
As soon as the datastore is instanciated, the database is create if it does not exists and table is created too
Note
- It is not possible to use :memory database either. The connection is closed after each operations
-
get_connector
()[source]¶ return a sqlite3 connection to communicate with the table define in self.db
-
filter
(**kwargs)[source]¶ Change kwargs[“query”] with “WHERE X=Y statements”. The filtering will be done with the actual evaluation of the query in
paginate()
the sql can then be lazy
-
paginate
(data, **kwargs)[source]¶ paginate the result of filter using ids limits. Obviously, to work properly, you have to set the start to the last ids you receive from the last call on this method. The max number of row this method can give back depend on the paginate_by option.
-
get_list
(**kwargs)[source]¶ return all the objects, paginated if needed, fitered if filters have been set.
-
create
(data)[source]¶ Validate the data with
base.DataStore.validate()
And, if data is valid, create the row in database and return it.