Skip to main content

SQLtills

Simple CRUD utilities for sqlalchemy

Finding myself having to duplicate code for my projects for CRUD even when using SQLAlchemy was very annoying. Thus I created some utilities which I then found annoying to have to copy paste and maintain between different projects. So in all my selfish glory I present to you SQLtills, a simple CRUD tool for sqlalchemy. Why is it called SQLtills because I am extremly horrible at naming things.

SQLtills is available on PyPi so to install simply:

pip install -U SQLtills

How to use sqltills

For this tutorial we will be using an in-memory sqlite database as is typical for an sqlalchemy tutorial however, and this probably doesn't need to be said, we're good with whatever sqlalchemy is good with.

Imagine you have a database with two tables: authors and blogs

your sqlalchemy models would then be

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref, sessionmaker

Base = declarative_base()

class Blog(Base):
    __tablename__ = "blogs"
    id = Column(Integer, primary_key = True)
    author_id = Column(Integer, ForeignKey("authors.id"))
    title = Column(String)
    blog_text = Column(String)

class Author(Base):
    __tablename__ = "authors"
    id = Column(Integer, primary_key = True)
    name = Column(String)
    blogs = relationship(Blog, backref = backref("author", uselist = False))

engine = create_engine('sqlite:///:memory:', echo=True)

Base.metadata.bind = engine

Base.metadata.create_all()

Awesome we now have the two tables in the database and the models defined. In the same script lets write some basic CRUD you would do in an app using sqltills utilities

from sqltills import create_rows

session_maker = sessionmaker(bind = engine)
session = sessionmaker()

new_author = Author()
new_author.name = "Omar"

new_blog = Blog()
new_blog.title = "How SQLAlchemy is Totally Awesome"
new_blog.blog_text = "too much stuff to put into one blog so the short answer is because"

new_author.blogs.append(new_blog)

create_rows(sesion, new_author, new_blog)

Easy right ! Okay imagine my application explodes and becomes medium.com now I have thousands of blogs and thousands of authors. I want to get all blogs which have the author being either Omar or Nancy

from sqltills import read_rows

results = read_rows(session, Author, filters = [
    {
        'name': {
            'comparitor': '==',
            'data': 'Omar'
        },
    'join': 'or'
    },
    {
        'name': {
            'comparitor':'==',
            'data': 'Nancy'
        }
    }
])

results = results.all()

for author in results:
    for blog in author.blogs:
        print(blog.title)

The filters argument is optional, if excluded the whole table will be included in the SQLAlchemy Query object. A parse-(like)-tree is used to build the filter expressions used in the session.query().filter() method

This filter parser can be used as well.

from sqltills import ParseTree
filters = [
    {
        'name': {
            'comparitor': '==',
            'data': 'Omar'
        },
    'join': 'or'
    },
    {
        'name': {
            'comparitor':'==',
            'data': 'Nancy'
        }
    }
]
parser = ParseTree(Author, filters)
results = parser.query(session)

This is a really simple and almost useless package but it allows me to have a centralized set of tools to do all my CRUD and makes my code clean(er) and easier to maintain.

Release files for SQLtills 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for SQLtills 0.0.3
File Size Uploaded
SQLtills-0.0.3.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for SQLtills 0.0.3
File Interpreter ABI Platform
SQLtills-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / SQLtills-0.0.3.tar.gz

Download URL SQLtills-0.0.3.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
488404bf049f9a0ad9803184b5739b4c18bbb247783c5775aeeb7fa6887ddd42
BLAKE2b-256 checksum
How to use checksums
da81eaee7388d2fa6c683968036967d8f8e9e30c90a0b06850a998a0ec1105c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release files / SQLtills-0.0.3-py3-none-any.whl

Download URL SQLtills-0.0.3-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea7c7c44c2c75f93003f7b8b71d1e7122c41b6b0a82b429e52a78f37195b92a0
BLAKE2b-256 checksum
How to use checksums
8f8c503c55901009e82fc637dd1441dd25bd69f076575b712bc94fb6e26c9dfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page