Skip to main content

A minimal ORM for PostgreSQL

Project description

PostgreSQL Python ORM

A minimal Python ORM for interacting with a PostgreSQL database (using psycopg2)

Installation

pip install post-orm

Example

from post_orm import Database, Column, Table, ForeignKey

db = Database(
    database="test_database",
    user="postgres",
    password="1234",
    host="localhost",
    port="5432",
)

# Create tables
class School(Table):
    country = Column(str)
    name = Column(str)


class Student(Table):
    name = Column(str)
    school = ForeignKey(School)


db.create([School, Student])

# Save school
school = School(name="Hogwarts", country="England")
db.save(school)

# Save students
harry = Student(name="Harry Potter", school=school)
ron = Student(name="Ron Weasley", school=school)
db.save([harry, ron])

# Make queries
all_students = db.all(Student)
harrys_school = db.query(Student, name="Harry Potter", limit=1).school # use limit=1 to return a single object.
hogwarts = db.query(School, country="Eng%", limit=1)  # use % for wildcard search.

assert harrys_school.country == hogwarts.country

Run tests in Docker

Prepare a test-database

docker run --rm -P -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234" --name pg postgres:alpine
psql postgresql://postgres:1234@localhost:5432/postgres
CREATE DATABASE test_database;

Run tests

python -m pytest .

This project was heavily inspiered by the SQLite ORM in this course on testdriven.io

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

post_orm-0.0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

post_orm-0.0.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file post_orm-0.0.4.tar.gz.

File metadata

  • Download URL: post_orm-0.0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for post_orm-0.0.4.tar.gz
Algorithm Hash digest
SHA256 12afb235caba20b63afe7a349320a832e36ccafd6555533b7c0b9be05aa952c1
MD5 99b246a6e5b2ca3208804ebd51fcb869
BLAKE2b-256 3f2c54d684fb92015cde48a2813752d745d22147d10f5232fbf06a798bd46774

See more details on using hashes here.

File details

Details for the file post_orm-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: post_orm-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for post_orm-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f8ddf784ee780475d78979b08ea55e69a89a020d34589f7c96af6c24f619dc96
MD5 a5f832a5dc12e7a69b073f73236ca9d6
BLAKE2b-256 99ed29c78d2665a837dcd26cb2d533e9ac12ead7bec393a9f152bd4c93ba7590

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