Skip to main content

Starting adaptor for JSON/Pandas to SQL db

Project description

sqladaptor

Transferring data, stored as JSON or Pandas, into an SQL database and back again.

Why?

Building webserver prototypes, one often saves initial data as JSON or Pandas files. Later, you will want to transition to an SQL database where updating/inserting data to disk is more efficient.

SqlAdaptor allows an easy transition for this transition, using a familiar API consisting of dict lists and pandas dataframes. Table schema are treated as Json-Schema.

This is possible because there is an equivalence between JSON dict lists, Pandas DataFrames and SQL tables - they are all tabular arrangements of columnar data.

Installation

pip install sqladaptor

Basic Usage

Inserting dict lists into an SQLite db

from sqladaptor.sqlite import SqliteAdaptor
import pandas

db = SqliteAdaptor('db.sqlite')
entries = [
    {"description": "this", "value": 1},
    {"description": "that", "value": 2}
]
db.set_from_df('data1', pandas.DataFrame(entries))

Read as dict lists or dataframes:

entries = db.read_dicts('data1')
# [
#   {'description': 'this', 'value': 1}, 
#   {'description': 'that', 'value': 2}
# ]
df = db.read_df("data1", {"value": 2})
#   description  value
# 0        this      1
# 1        that      2

Get the table schema as Json-schema:

schema = db.read_table_schema("data1")
# {
# │   'type': 'object',
# │   'properties': {
# │   │   'description': {'type': 'string'},
# │   │   'value': {'type': 'integer'}
# │   },
# │   'additionalProperties': False
# }

Make an SQL table with Json-schema:

db.create_table(
    "data1",
    {
        "type": "object",
        "properties": {
            "row_id": {"type": "string"},
            "description": {"type": "string"},
            "value": {"type": "integer"},
        },
        "required": ["row_id"],
    },
)

Find using dict search:

return_entries = db.read_dicts('data1', {"description": "this"})
# [{'description': 'this', 'value': 1}]

CRUD your database:

db.insert("data1", {"value": 3, "description": "inserted"})
entries = db.read_dicts('data1')
# [
# │   {'description': 'this', 'value': 1},
# │   {'description': 'that', 'value': 2},
# │   {'description': 'inserted', 'value': 3}
# ]

db.update(
    "data1",
    {"description": "inserted"},
    {"description": "inserted-then-updated", "value": 77},
)
entries = db.read_dicts('data1')
# [
# │   {'description': 'this', 'value': 1},
# │   {'description': 'that', 'value': 2},
# │   {'description': 'inserted-then-updated', 'value': 77}
# ]

db.delete("data1", {"value": 77})
entries = db.read_dicts('data1')
# [{'description': 'this', 'value': 1}, {'description': 'that', 'value': 2}]

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

sqladaptor-0.5.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqladaptor-0.5.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file sqladaptor-0.5.0.tar.gz.

File metadata

  • Download URL: sqladaptor-0.5.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.3

File hashes

Hashes for sqladaptor-0.5.0.tar.gz
Algorithm Hash digest
SHA256 35ca7cd7e6b2043b60c83aaf57118086aefa4a1ccea482e3c53cdcb03ce03e5c
MD5 174c8392999edba3314de2cda3353190
BLAKE2b-256 8b096fda003d3805b59a760956a8294899fc6d81f2fb8a02dc55964278c03a5f

See more details on using hashes here.

File details

Details for the file sqladaptor-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sqladaptor-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.3

File hashes

Hashes for sqladaptor-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa2c56ac7a4e52a99484cff659ce8274857a49696fd30154a7dbd82a37572b1c
MD5 a5d6d6faf1614b04f80efa5c0cc11971
BLAKE2b-256 abfa011ba9d82bf1fbafc4a42929f42ce665a4940aa396810ad8f7c61e85162b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page