Skip to main content

NoSQL Abstraction Library

Project description

NoSQL Abstraction Library

Basic CRUD and query support for NoSQL databases

  • AWS DynamoDB
  • Azure Cosmos NoSQL

This library is not intended to create databases/tables, use Terraform/ARM/CloudFormation etc for that

Why not just 'nosql' or 'pynosql'? because they already exist on pypi :-)

testscodecov

Installation

pip install abnosql[dynamodb]
pip install abnosql[cosmos]

By default, abnosql does not include database depedendencies. This is to facilitate packaging abnosql into AWS Lambda or Azure Functions (for example), without over-bloating the packages

Example Usage

from abnosql import table
import os

os.environ['ABNOSQL_DB'] = 'dynamodb'

item = {
    'hk': '1',
    'rk': 'a',
    'num': 5,
    'obj': {
        'foo': 'bar',
        'num': 5,
        'list': [1, 2, 3],
    },
    'list': [1, 2, 3],
    'str': 'str'
}

tb = table('mytable')

tb.put_item(item)
tb.put_items([item])

# note partition/hash key should be first kwarg
assert tb.get_item(hk='1', rk='a') == item

assert tb.query({'hk': '1'}) == [item]

# be careful not to use cloud specific statements!
assert tb.query_sql(
    'SELECT * FROM mytable WHERE hk = @hk',
    {'@hk': '1'}
) == [item]

tb.delete_item({'hk': '1', 'rk': 'a'})

Configuration

AWS DynamoDB

Set the following environment variable and use the usual AWS environment variables that boto3 uses

  • ABNOSQL_DB = "dynamodb"

Or set the boto3 session in the config

from abnosql import table
import boto3

tb = table(
    'mytable',
    config={'session': boto3.Session()},
    database='dynamodb'
)

Azure Cosmos NoSQL

Set the following environment variables

  • ABNOSQL_DB = "cosmos"
  • ABNOSQL_COSMOS_ACCOUNT = your database account
  • ABNOSQL_COSMOS_ENDPOINT = drived from ABNOSQL_COSMOS_ACCOUNT if not set
  • ABNOSQL_COSMOS_CREDENTIAL = your cosmos credential
  • ABNOSQL_COSMOS_DATABASE = cosmos database

Or define in config

from abnosq import table

tb = table(
    'mytable',
    config={'account': 'foo', 'credential': 'someb64key', 'database': 'bar'},
    database='cosmos'
)

Plugins and Hooks

abnosql uses pluggy and registers in the abnosq.table namespace

The following hooks are available

  • set_config - set config
  • get_item_post - called after get_item(), can return modified data
  • put_item_post
  • put_items_post
  • delete_item_post

See the hookimpl and example test_hooks() in the tests

Testing

AWS DynamoDB

Use moto package and abnosql.mocks.mock_dynamodbx

Example:

from abnosql.mocks import mock_dynamodbx 
from moto import mock_dynamodb2

@mock_dynamodb2
@mock_dynamodbx
def test_something():
    ...

More examples in tests/test_dynamodb.py

Azure Cosmos NoSQL

Use requests package and abnosql.mocks.mock_cosmos

Example:

from abnosql.mocks import mock_cosmos
import requests

@mock_cosmos
@responses.activate
def test_something():
    ...

More examples in tests/test_cosmos.py

CLI

Small abnosql CLI installed with few of the commands above

Usage: abnosql [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  delete-item
  get-item
  put-item
  put-items
  query
  query-sql

Example querying table in Azure Cosmos, with cosmos.json config file containing endpoint, credential and database

$ abnosql query-sql mytable 'SELECT * FROM myable' -d cosmos -c cosmos.json
partkey      id      num  obj                                          list       str
-----------  ----  -----  -------------------------------------------  ---------  -----
p1           p1.1      5  {'foo': 'bar', 'num': 5, 'list': [1, 2, 3]}  [1, 2, 3]  str
p2           p2.1      5  {'foo': 'bar', 'num': 5, 'list': [1, 2, 3]}  [1, 2, 3]  str
p2           p2.2      5  {'foo': 'bar', 'num': 5, 'list': [1, 2, 3]}  [1, 2, 3]  str

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

abnosql-0.0.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

abnosql-0.0.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file abnosql-0.0.1.tar.gz.

File metadata

  • Download URL: abnosql-0.0.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for abnosql-0.0.1.tar.gz
Algorithm Hash digest
SHA256 00d928ebd4c0e215e9c547cd80ee788f4dd6c17f76110498101f9947db4f504e
MD5 5fb09bafe7921e621045d801eba14d7c
BLAKE2b-256 2974751a5f41172bc95e59df089929200c37ab5dc2a202402eeb7f21977b5647

See more details on using hashes here.

File details

Details for the file abnosql-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: abnosql-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for abnosql-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8a9c0a193d9a33f0cf8c03dd9d97d17ae3b7838e0223ebf15666a921a2f1fa9
MD5 de4d56165e2acfe94fa2d6dec28f3b83
BLAKE2b-256 076152836598b60179d51b1cb299f90ca78d293bb282373d4b0cb288d627788a

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