Skip to main content

Datasette plugin providing an API for inserting and updating data

Project description

datasette-insert-api

PyPI Changelog License

Datasette plugin providing an API for inserting and updating data

Installation

Install this plugin in the same environment as Datasette.

$ pip install datasette-insert-api

Usage

Having installed the plugin, data can be inserted or updated and tables can be created by POSTing JSON data to the following URL:

/-/insert/name-of-database/name-of-table

The JSON should look like this:

[
    {
        "id": 1,
        "name": "Cleopaws",
        "age": 5
    },
    {
        "id": 2,
        "name": "Pancakes",
        "age": 5
    }
]

The first time data is posted to the URL a table of that name will be created if it does not aready exist, with the desired columns.

You can specify which column should be used as the primary key using the ?pk= URL argument.

Here's how to POST to a database and create a new table using the Python requests library:

import requests

requests.post("http://localhost:8001/-/insert/unsafe/dogs?pk=id", json=[
    {
        "id": 1,
        "name": "Cleopaws",
        "age": 5
    },
    {
        "id": 2,
        "name": "Pancakes",
        "age": 4
    }
])

And here's how to do the same thing using curl:

curl --request POST \
  --data '[
      {
        "id": 1,
        "name": "Cleopaws",
        "age": 5
      },
      {
        "id": 2,
        "name": "Pancakes",
        "age": 4
      }
    ]' \
    'http://localhost:8001/-/insert/unsafe/dogs?pk=id'

Automatically adding new columns

If you send data to an existing table with keys that are not reflected by the existing columns, you will get an HTTP 400 error with a JSON response like this:

{
    "status": 400,
    "error": "Unknown keys: 'foo'",
    "error_code": "unknown_keys"
}

If you add ?alter=1 to the URL you are posting to any missing columns will be automatically added:

curl --request POST \
  --data '[
      {
        "id": 3,
        "name": "Boris",
        "age": 1,
        "breed": "Husky"
      }
    ]' \
    'http://localhost:8001/-/insert/unsafe/dogs?alter=1'

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-insert-api
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and tests:

pip install -e '.[test]'

To run the tests:

pytest

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

datasette-insert-api-0.2a0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

datasette_insert_api-0.2a0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file datasette-insert-api-0.2a0.tar.gz.

File metadata

  • Download URL: datasette-insert-api-0.2a0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for datasette-insert-api-0.2a0.tar.gz
Algorithm Hash digest
SHA256 83a483c412a21c3823db0dee345b4edf7313376f0066437c17b227d8548db07a
MD5 59402af118bcb332b17335ef0b7e4813
BLAKE2b-256 fb53dd0016d86ca5b2e4c61b3e8825557eabe997a4d7508e7f97d9243e776e40

See more details on using hashes here.

File details

Details for the file datasette_insert_api-0.2a0-py3-none-any.whl.

File metadata

  • Download URL: datasette_insert_api-0.2a0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for datasette_insert_api-0.2a0-py3-none-any.whl
Algorithm Hash digest
SHA256 cda01dcf86e59c0a8c3fb949a1ea9c2742163c2bc5e46d86e73bd10ffbb211c9
MD5 91351059c67350982f3f58dac4a499f0
BLAKE2b-256 cdd19489a9b47e32da303f213053228b71af21feb20523130efb82b99fa4d95c

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