Skip to main content

Postgres Not an ORM

Project description

Postgres Not an ORM

A simple library that helps you interact with Postgres databases

  • Write raw SQL queries
  • Marshall queries into Pydantic models for type safety
  • Manage sessions and transactions using simple context api
  • Automatically create Open Telemetry spans for monitoring

Basic Examples

from pydantic import BaseModel

from pnorm import AsyncPostgresClient, PostgresCredentials

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

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

# If we expect there to be exactly one "john"
john = await client.get(User, "select * from users where name = %(name)s", {"name": "john"})
# john: User or throw exception

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

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

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

Keep connection alive

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

    await session.execute("create table users (name varchar, age integer)")
    await session.execute(
        "insert into users (name, age) values (%(name)s, %(age)s),
        User(name="sally", age=20),
    )
    
    # 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.

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

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

Inspired by

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.3.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pnorm-0.0.3-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pnorm-0.0.3.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pnorm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 16aa3fecee3edeb370cd8ae51eb3b3efdaa5a2a7937603c6298a8ea0f9a65230
MD5 c0139615cb414a355d09bc9001c9020b
BLAKE2b-256 ea64711b1f45e88fc4b996d2ed59b71540e78b29992a7e01bed24c4cf3ca722c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pnorm-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pnorm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3755802e25bf4fadef94a61f7eb5398286a1a8eee43adf54ef28fc859f2c64f2
MD5 c9b75d54a7b1b23b67dbb7d664314738
BLAKE2b-256 66df32888cff40e885c24dabacdafe0372d825af04daff0e23cbc93b31c05710

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page