Skip to content

Commit

Permalink
Reordered datastore docs to make more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored and amercader committed Oct 12, 2012
1 parent b4863d0 commit f5d07eb
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 285 deletions.
1 change: 1 addition & 0 deletions ckanext/datastore/logic/action.py
Expand Up @@ -227,6 +227,7 @@ def datastore_search_sql(context, data_dict):
`PostgreSQL engine <http://www.postgresql.org/docs/9.1/interactive/sql/.html>`_
.. note:: This action is only available when using PostgreSQL 9.X and using a read-only user on the database.
It is not available in :ref:`legacy mode<legacy_mode>`.
:param sql: a single sql select statement
:type sql: string
Expand Down
153 changes: 147 additions & 6 deletions doc/datastore-api.rst
@@ -1,9 +1,12 @@
==================
Using the Data API
==================
======================
The DataStore Data API
======================

The following provides an introduction to using the CKAN :doc:`DataStore<datastore>` Data API.

The following provides an introduction to using the CKAN :doc:`DataStore
<datastore>` Data API.
The DataStore's Data API, which derives from the underlying data table, is JSON-based with extensive query capabilities.

Each resource in a CKAN instance can have an associated DataStore 'table'. The basic API for accessing the DataStore is outlined below.

Introduction
============
Expand All @@ -24,11 +27,149 @@ There are several endpoints into the DataStore API, they are:
at ``http://{YOUR-CKAN-INSTALLATION}/api/3/action/datastore_upsert``
:meth:`~ckanext.datastore.logic.action.datastore_search`
at ``http://{YOUR-CKAN-INSTALLATION}/api/3/action/datastore_search``
:meth:`~ckanext.datastore.logic.action.datastore_search_sql`
:meth:`~ckanext.datastore.logic.action.datastore_search_sql`, not available in :ref:`legacy_mode`
at ``http://{YOUR-CKAN-INSTALLATION}/api/3/action/datastore_search_sql``
``datastore_search_htsql()``, see :ref:`datastore_search_htsql`
at ``http://{YOUR-CKAN-INSTALLATION}/api/3/action/datastore_search_htsql``


API Reference
=============

The datastore related API actions are accessed via CKAN's :ref:`action-api`. When POSTing
requests, parameters should be provided as JSON objects.

.. note:: Lists can always be expressed in different ways. It is possible to use lists, comma separated strings or single items. These are valid lists: ``['foo', 'bar']``, ``'foo, bar'``, ``"foo", "bar"`` and ``'foo'``.

.. automodule:: ckanext.datastore.logic.action
:members:



.. _fields:

Fields
------

Fields define the column names and the type of the data in a column. A field is defined as follows::

{
"id": # a string which defines the column name
"type": # the data type for the column
}

Field **types are optional** and will be guessed by the DataStore from the provided data. However, setting the types ensures that future inserts will not fail because of wrong types. See :ref:`valid-types` for details on which types are valid.

Example::

[
{
"id": "foo",
"type": "int4"
},
{
"id": "bar"
# type is optional
}
]

.. _records:

Records
-------

A record is the data to be inserted in a table and is defined as follows::

{
"<id>": # data to be set
# .. more data
}

Example::

[
{
"foo": 100,
"bar": "Here's some text"
},
{
"foo": 42
}
]

.. _valid-types:

Field types
-----------

The DataStore supports all types supported by PostgreSQL as well as a few additions. A list of the PostgreSQL types can be found in the `type section of the documentation`_. Below you can find a list of the most common data types. The ``json`` type has been added as a storage for nested data.

In addition to the listed types below, you can also use array types. They are defines by prepending a ``_`` or appending ``[]`` or ``[n]`` where n denotes the length of the array. An arbitrarily long array of integers would be defined as ``int[]``.

.. _type section of the documentation: http://www.postgresql.org/docs/9.1/static/datatype.html


text
Arbitrary text data, e.g. ``Here's some text``.
json
Arbitrary nested json data, e.g ``{"foo": 42, "bar": [1, 2, 3]}``.
Please note that this type is a custom type that is wrapped by the DataStore.
date
Date without time, e.g ``2012-5-25``.
time
Time without date, e.g ``12:42``.
timestamp
Date and time, e.g ``2012-10-01T02:43Z``.
int
Integer numbers, e.g ``42``, ``7``.
float
Floats, e.g. ``1.61803``.
bool
Boolean values, e.g. ``true``, ``0``


You can find more information about the formatting of dates in the `date/time types section of the PostgreSQL documentation`_.

.. _date/time types section of the PostgreSQL documentation: http://www.postgresql.org/docs/9.1/static/datatype-datetime.html


Table aliases
-------------

A resource in the DataStore can have multiple aliases that are easier to remember than the resource id. Aliases can be created and edited with the datastore_create API endpoint. All aliases can be found in a special view called ``_table_metadata``.


.. _datastore_search_htsql:

HTSQL Support
-------------


The `ckanext-htsql <https://github.com/okfn/ckanext-htsql>`_ extension adds an API action that allows a user to search data in a resource using the `HTSQL <http://htsql.org/doc/>`_ query expression language. Please refer to the extension documentation to know more.



Comparison of different querying methods
----------------------------------------

The DataStore supports querying with multiple API endpoints. They are similar but support different features. The following list gives an overview of the different methods.

============================== ======================================================== ============================================================ =============================
.. :meth:`~ckanext.datastore.logic.action.datastore_search` :meth:`~ckanext.datastore.logic.action.datastore_search_sql` :ref:`datastore_search_htsql`
.. SQL HTSQL
============================== ======================================================== ============================================================ =============================
**Status** Stable Stable Available as extension
**Ease of use** Easy Complex Medium
**Flexibility** Low High Medium
**Query language** Custom (JSON) SQL HTSQL
**Connect multiple resources** No Yes Not yet
**Use aliases** Yes Yes Yes
============================== ======================================================== ============================================================ =============================


Using the Data API
==================

Before going into detail about the API and the examples, it is useful to create a resource first so that you can store data against it in the Datastore. This can be done either through the CKAN Graphical User Interface or the API.

Using the data API, we need to send JSON with this structure::
Expand Down

0 comments on commit f5d07eb

Please sign in to comment.