Skip to main content

A simple database API

Project description

code-of-conduct build-status cover-status pyver-status pypiv-status coding-style

Introduction

What is DINAO? Well it might be easier to tell you what it’s not. DINAO Is Not An ORM. If you want an ORM, SQLAlchemy is absolutely the best python has to offer.

Target Audience

Do you like writing SQL? Do you hate all the boiler plate involved with setting up connections and cursors then cleaning them up? Would you just like something simple that executes a query and can map the results to simple data classes? Then DINAO is for you!

Influences and Guiding Principles

The APIs implemented mirror libraries I’ve used in other ecosystems. Specifically, you may notice similarities to the JDBI Declarative API or the MyBatis interface mappers. This is because I very much like this approach. You’re the developer, I’m just here to reduce the number of lines of code you have to write to meet your goal. At the end of the day you know your schema and database better than I do, and so you know what kinds of queries you need to write better than I do.

How do you pronounce DINAO?

You pronounce it “Dino” like “Dinosaur”. Going back to plain old SQL probably seems rather archaic after all.

Usage

Install via pip:

$ pip install dinao

You will also need to install your backend driver. Backends + drivers supported are:

  • Sqlite3 via Python’s standard library

  • PostgreSQL via psycopg2

Basic Example

DINAO focuses binding functions to scoped connections / transactions against the database and using function signatures and type hinting to infer mapping and query parameterization.

Below shows a simple example of DINAO usage. For more comprehensive usage and feature showcase see examples.

from typing import List
from dataclasses import dataclass
from dinao.backend import create_connection_pool, Connection
from dinao.binding import FunctionBinder

binder = FunctionBinder()

@dataclass
class MyModel:
    name: str
    value: int

@binder.execute(
    "CREATE TABLE IF NOT EXISTS my_table ( "
    "  name VARCHAR(32) PRIMARY KEY, "
    "  value INTEGER DEFAULT 0"
    ")"
)
def make_table():
    pass


@binder.execute(
    "INSERT INTO my_table (name, value) VALUES(#{model.name}, #{model.value}) "
    "ON CONFLICT (name) DO UPDATE "
    "  SET value = #{model.value} "
    "WHERE my_table.name = #{model.name}"
)
def upsert(model: MyModel) -> int:
    pass


# This is an example of a query where a template variable is directly
# replaced in a template.  This is via a template argument denoted with
# !{column_name}.  The #{search_term} on the other hand uses proper
# escaping and parameterization in the underlying SQL engine.
#
# IMPORTANT: This is a vector for SQL Injection, do not use direct template
#            replacement on untrusted inputs, especially those coming from
#            users.  Ensure that you validate, restrict, or otherwise limit
#            the values that can be used in direct template replacement.
#
@binder.query("SELECT name, value FROM my_table WHERE !{column_name} LIKE #{search_term}")
def search(column_name: str, search_term: str) -> List[MyModel]:
    pass


@binder.transaction()
def populate(cnx: Connection = None):
    make_table()
    cnx.commit()
    upsert(MyModel("testing", 52))
    upsert(MyModel("test", 39))
    upsert(MyModel("other_thing", 20))


if __name__ == '__main__':
    con_url = "sqlite3:///tmp/example.db"
    db_pool = create_connection_pool(con_url)
    binder.pool = db_pool
    populate()
    for model in search("name", "test%"):
        print(f"{model.name}: {model.value}")

Contributing

Check out our code of conduct and contributing documentation.

Release Process

This library adheres too semantic versioning 2.0.0 standards, in general that means, given a version number MAJOR.MINOR.PATCH, increment:

  1. MAJOR version when you make incompatible API changes

  2. MINOR version when you add functionality in a backwards compatible manner

  3. PATCH version when you make backwards compatible bug fixes

Changes for the next version should be accumulated on the main branch until such time that there is enough confidence in the build that it can be released. When this is done, a repository administrator opens a PR to bump the version in __version__.py updates the change logs, merges this PR then tags the merge with the release version. Only tagged commits of main are built and published.

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

dinao-1.4.0.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

dinao-1.4.0-py3-none-any.whl (39.8 kB view details)

Uploaded Python 3

File details

Details for the file dinao-1.4.0.tar.gz.

File metadata

  • Download URL: dinao-1.4.0.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for dinao-1.4.0.tar.gz
Algorithm Hash digest
SHA256 082ef312601fc6a1bf12bde8d023a52943cc87d4e7b9f087bbb22ff0be164139
MD5 f5ef1f017a4a7e4d75e01a9a3893f1c8
BLAKE2b-256 04af6292fc34e17fba5bb63a42c29cf53c61930a4afd0a3c492e3c6051d4b5d6

See more details on using hashes here.

File details

Details for the file dinao-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: dinao-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 39.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for dinao-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7970e8ae1d3cd467306ec24a3a0a30029f61425d3cee888a7c14068409f0544
MD5 49853cb88d1a060025cc9c9034dcba72
BLAKE2b-256 957c8b8bf943f41e7d97df17aca76bfe39b163e14e236c5ef2c3d3c8011f743e

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