Skip to main content

(Postgres) Not an ORM

Project description

(Postgres) Not an ORM

This library helps you interact with a Postgres database in Python, by writing raw SQL.

https://www.odbms.org/wp-content/uploads/2013/11/031.01-Neward-The-Vietnam-of-Computer-Science-June-2006.pdf https://blog.codinghorror.com/object-relational-mapping-is-the-vietnam-of-computer-science/ https://news.ycombinator.com/item?id=7310077

Basic Examples

from pydantic import BaseModel

from pnorm import PostgresClient, PostgresCredentials

creds = PostgresCredentials(host="", port=5432, user="", password="", dbname="")

client = PostgresClient(creds)

class User(BaseModel):
    name: str
    age: int

# If we *know* there is exactly one "john"
john: User = client.get(User, "select * from users where name = %(name)s", {"name": "john"})

# Get the first "mike" or return None
mike: User | None = client.find(Users, "select * from users where name = %(name)s, {"name": "mike"})

# Get all results
adults: list[User] = client.select(User, "select * from users where age >= 18")

# delete adults
client.execute("delete from users where age >= 18")

# insert into table
client.execute_values(
    "insert into users (name, age) values (%(name)s, %(age)s),
    [
        User(name="sally", age=20),
        User(name="daniel", age=21)
    ]
)

Keep connection alive

with client.start_session(schema="admin") as session:
    # Connection end
    # > Set the default schema to "admin"

    session.execute("create table users (name varchar, age integer)")
    client.execute_values(
    "insert into users (name, age) values (%(name)s, %(age)s),
        [
            User(name="sally", age=20),
            User(name="daniel", age=21)
        ]
    )
    
    # Connection end

Create a transaction

This example, retrieves a user from the users table, deletes the user, in python increments the user's age, then inserts the user back into the DB. Because this is in a transaction, the user will exist in the database with it's previous age (in case of a failure) or exist in the database with their new age.

person = transaction.get(User, "select * from users where name = %(name)s", {"name": "mike"})

with client.create_transaction() as transaction:
    # Transaction start

    transaction.execute("delete from users where name = %(name)s", {"name": "mike"})
    person.age += 1
    transaction.execute("insert into users (name, age) (%(name)s, %(age)s))", person)
   
    # Transaction end

Of course you could do this all in SQL with an update statement.

Inspired by https://github.com/jmoiron/sqlx

https://github.com/dagster-io/dagster/blob/master/python_modules/libraries/dagster-aws/dagster_aws/redshift/resources.py https://github.com/jmoiron/sqlx https://jmoiron.github.io/sqlx/

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

pnorm-0.0.0.6.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

pnorm-0.0.0.6-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file pnorm-0.0.0.6.tar.gz.

File metadata

  • Download URL: pnorm-0.0.0.6.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pnorm-0.0.0.6.tar.gz
Algorithm Hash digest
SHA256 c551f707e78c779c2d44f9cd64b35e1989e3b31975f808ac5327932a612d24a0
MD5 b75f75b39a9f7697790bd5ff8ceca284
BLAKE2b-256 bb77a2c5d32dbfb54b2ca549f76c152aaa2c1733c817268851a26ba3b4536483

See more details on using hashes here.

File details

Details for the file pnorm-0.0.0.6-py3-none-any.whl.

File metadata

  • Download URL: pnorm-0.0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pnorm-0.0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e0283ef7758165a829f6c2af17c058396aef07d2ec0fd40dbe03ae17bfbad7f6
MD5 a1d474d13398290fd177dd5cf9593385
BLAKE2b-256 150139b705e3e1e5b4d22852a73337e917769594f5350c5b57eb5cb2beb7eb6c

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