Skip to main content

A sensible orm for PostgreSQL or SQLite

Project description

Prom

An opinionated asynchronous lightweight orm for PostgreSQL or SQLite.

1 Minute Getting Started with SQLite

First, install prom:

$ pip install prom[sqlite]

Set an environment variable:

$ export PROM_DSN=sqlite://:memory:

Start python:

$ python

Create a prom Orm:

>>> import prom
>>>
>>> class Foo(prom.Orm):
...     table_name = "foo_table_name"
...     bar = prom.Field(int)
...
>>>

Now go wild and create some Foo objects:

>>> for x in range(10):
...     f = await Foo.create(bar=x)
...
>>>

Now query them:

>>> f = await Foo.query.one()
>>> f.bar
0
>>> f.pk
1
>>>
>>> async for f in await Foo.query.in_bar([3, 4, 5]):
...     f.pk
...
3
4
5
>>>

Update them:

>>> async for f in await Foo.query:
...     f.bar += 100
...     await f.save()
...
>>>

and get rid of them:

>>> async for f in await Foo.query:
...     await f.delete()
...
>>>

Congratulations, you have now created, retrieved, updated, and deleted from your database.


Configuration

Prom can be automatically configured on import by setting the environment variable PROM_DSN.

The PROM_DSN should define a dsn url:

<full.python.path.InterfaceClass>://<username>:<password>@<host>:<port>/<database>?<options=val&query=string>#<name>

The built-in interface classes don't need their full python paths, you can just use sqlite and postgres.

So to use the builtin Postgres interface on testdb database on host localhost with username testuser and password testpw:

postgres://testuser:testpw@localhost/testdb

And to set it in your environment:

export PROM_DSN=postgres://testuser:testpw@localhost/testdb

After you've set the environment variable, then you just need to import Prom in your code:

import prom

and Prom will take care of parsing the dsn url(s) and creating the connection(s) automatically.

Multiple db interfaces or connections

If you have multiple connections, you can actually set multiple environment variables:

export PROM_DSN_1=postgres://testuser:testpw@localhost/testdb1#conn_1
export PROM_DSN_2=sqlite://testuser:testpw@localhost/testdb2#conn_2

It's easy to have one set of prom.Orm children use one connection and another set use a different connection, since the fragment part of a Prom dsn url sets the name:

import prom

class Orm1(prom.Orm):
    connection_name = "conn_1"
  
class Orm2(prom.Orm):
    connection_name = "conn_2"

Now, any child class that extends Orm1 will use conn_1 and any child class that extends Orm2 will use conn_2.

Creating Models

Checkout the README to see how to define the db schema and create models your python code can use.

Querying Rows

Checkout the README to see how to perform queries on the db.

Versions

While Prom will most likely work on other versions, Prom is tested to work on 3.10.

Installation

Postgres

If you want to use Prom with Postgres:

$ apt-get install libpq-dev python-dev
$ pip install prom[postgres]

Prom

Prom installs using pip:

$ pip install prom[sqlite]
$ pip install prom[postgres]

and to install the latest and greatest:

$ pip install --upgrade "git+https://github.com/Jaymon/prom#egg=prom"

Using for the first time

Prom takes the approach that you don't want to be hassled with table installation while developing, so when it tries to do something and sees that the table doesn't yet exist, it will use your defined fields for your prom.model.Orm child and create a table for you, that way you don't have to remember to run a script or craft some custom db query to add your tables. Prom takes care of that for you automatically.

Likewise, if you add a field (and the field is not required) then prom will go ahead and add that field to your table so you don't have to bother with crafting ALTER queries while developing.

If you want to install the tables manually, you can create a script or something and use the Orm's install() method:

await SomeOrm.install()

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prom-5.4.1.tar.gz (80.3 kB view details)

Uploaded Source

Built Distribution

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

prom-5.4.1-py3-none-any.whl (79.8 kB view details)

Uploaded Python 3

File details

Details for the file prom-5.4.1.tar.gz.

File metadata

  • Download URL: prom-5.4.1.tar.gz
  • Upload date:
  • Size: 80.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for prom-5.4.1.tar.gz
Algorithm Hash digest
SHA256 4a20565da4b65fa0e91cdcb4e2e09c021bcf959311c5b14d454affde569d4043
MD5 276b5021ef44904a5a32f1daef53566d
BLAKE2b-256 f1e92a6404adc4f66b7b0b0a637824147749d02cac4caace547e29310a77c0d9

See more details on using hashes here.

File details

Details for the file prom-5.4.1-py3-none-any.whl.

File metadata

  • Download URL: prom-5.4.1-py3-none-any.whl
  • Upload date:
  • Size: 79.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for prom-5.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8fbe71f3ae51a7c12f4c0c56390cf1865623f8d2a340b721b3c2a1f356e7f9ef
MD5 7c18e6313f898b0841bf2bc8726539b7
BLAKE2b-256 1ee5f320a6addeb184c01e7a5555661da20dbf81772008e8846d3e9a7d9efcc4

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