Skip to main content

A Darned Simple ORM

Project description

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Darned Simple ORM

A single file ORM for SQLite in Python

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact

About The Project

Darned Simple ORM (dsORM) is a little bit different from other ORMs. The typical ORM approach is to have developers "map" their classes to a database and then do "magic" to make that database work. dsORM aims instead to map database components into easy to use Python objects which can be leveraged by your classes for database interactions.

If SQLAlchemy's expression language comes to mind, yes, this is a bit like that. But dsORM is simple, the entire functional code is in a single file which is currently under 1,000 lines. For comparison, PeeWee, a fairly small ORM is 7,723 lines long in it's main file and that doesn't contain all of it's functional code. SQLAlchemy as of this writing contains 343,975 lines of Python code (though admittedly it dwarfs dsORM's feature set.)

Designed for easy integration / modification

  • 100% Python
  • No external dependencies
  • 100% test coverage
  • Functional code in a single file

Should I use this?

You should not use dsORM if:

  • You need a fully featured and robust ORM supporting multiple back ends
  • You don't have any idea how SQL works and need maximal hand holding
  • You want something that enforces best practices

You should use dsORM if:

  • You know SQL enough to get around and want to avoid some boilerplate
  • You are prototyping and want something minimal to stand in for another ORM
  • You want to make your own project tailored ORM and can use dsORM as a starting point
  • You cannot pip install in your environment and need a single file solution that can be bundled

Getting Started

To get a local copy up and running follow these simple steps.

Installing with pip

pip install dsorm

For information about cloning and dev setup see: Contributing

Usage

Here is an example showing basic usage.

from dsorm import ID_COLUMN, Column, Database, Table, Where

person = Table(
    name="person",
    column=[
        Column.id(),  # This is shorthand for Column("id", int, pkey=True)
        Column("first_name", nullable=False),
        Column("last_name", nullable=False),
    ],
)

print(person.sql())


person2 = Table.from_dict(
    "person",
    {
        "id": ID_COLUMN,
        "first_name": {"nullable": False},
        "last_name": {"nullable": False},
    },
)

print(person2.sql())

# See Database example for more details about the Database object
Database(db_path=":memory:", is_default=True).init_db()  # This creates all tables


# Tables have insert, select, and delete methods.
# These return a Statement
stmt = person.insert(
    data=[
        {"first_name": "Jane", "last_name": "Doe"},
        {"first_name": "John", "last_name": "Doe"},
    ],
)

# Statements can be examined with .sql method
print(stmt.sql())

# INSERT INTO Main.person (first_name, last_name)
# VALUES ('Jane', 'Doe'), ('John', 'Doe')

# or executed with .execute()
stmt.execute()

# select returns a list of dicts of rows matching the where
does = person.select(
    where={"first_name": Where.like(target="J%n%")},
    column=[
        "id",
        "first_name || ' ' || last_name AS full_name",  # Note that the columns can be sql
    ],
).execute()

print(does)
# [{"id": 1, "full_name": "John Doe"}, {"id": 2, "full_name": "Jane Doe"}]

# And Delete
person.delete(where={"id": does[0]["id"]}).execute()
print([r["id"] for r in person.select().execute()])
# [2]

It's darned simple.

Further Examples

Roadmap

Needed features:

  • JOIN between objects
  • Grouping/Aggregates
  • Order/Limit/Offset

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Add tests, we aim for 100% test coverage Using Coverage
  4. Commit your Changes (git commit -m 'Add some AmazingFeature')
  5. Push to the Branch (git push origin feature/AmazingFeature)
  6. Open a Pull Request

Cloning / Development setup

  1. Clone the repo and install
    git clone https://github.com/kajuberdut/dsorm.git
    cd dsorm
    pipenv install --dev
    
  2. Run tests
    pipenv shell
    py.test
    

For more about pipenv see: Pipenv Github

License

Distributed under the BSD Two-clause License. See LICENSE for more information.

Contact

Patrick Shechet - patrick.shechet@gmail.com

Project Link: https://github.com/kajuberdut/dsorm

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

dsorm-0.0.7.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

dsorm-0.0.7-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file dsorm-0.0.7.tar.gz.

File metadata

  • Download URL: dsorm-0.0.7.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for dsorm-0.0.7.tar.gz
Algorithm Hash digest
SHA256 06c92433ca114fdb2ff5a6059f0cf4a0ba064e2514e70189358117ec117f8430
MD5 552655d66fa5bb6ebc3b172c5dc9555d
BLAKE2b-256 f615bca9c868b7e0fadda1b9a396a1a76a0327b03dc51a49404b2c6f8cf7de16

See more details on using hashes here.

File details

Details for the file dsorm-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: dsorm-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for dsorm-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4a52c618af76003b8544e8d1442b38d6ec028b4bd2d2e806c13b81828fa11487
MD5 ac851f9f6ae8c72318700fe3534d9da4
BLAKE2b-256 26d00777a2e970fba60152038039840fe0026e5956f1621bc221b796988a6a2e

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