dbsession
The python postgreSQL ORM
introduction
DBSession gives simplicity, flexibility and order to the abstraction of database tables into python objects. Right now it's only supporting postgreSQL with psycopg2
Usage
Assuming we have a table named A with the following estructure
id | name
----+-------
1 | Paul
2 | George
Create an archive A.py and create a class for the database abstraction A that inherits from DatabaseObject. The attribute _TABLE_NAME is the database table actual name
from DBSession import DatabaseObject
TABLE_NAME = 'A'
class _A(DatabaseObject):
_TABLE_NAME = TABLE_NAME
def __init__(self, row):
super().__init__(row)
wrapper = _A
Now implement every function associated with this table in the same file. Every function receives the params that goes in the query, and returns a tuple of four items:
- query (str): The actual query
- values (Tuple): The params to be replaced into the query
- wrapper (Object): The class that represent the table
- many (bool): Whether the query will return one or many instances
def find_by_id(a_id):
query = f"""
SELECT *
FROM {TABLE_NAME}
WHERE id = %s
"""
values = (a_id, )
return query, values, wrapper, False
def get_all():
query = f"""
SELECT *
FROM {TABLE_NAME}
"""
values = ()
return query, values, wrapper, True
Create a connection to the database using the DBConnection class. It receives the pool instance from psycopg2.
connection_pool = psycopg2.pool.ThreadedConnectionPool(
MINIMUM_CONNECTION,
MAXIMUM_CONNECTION,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host=POSTGRES_HOST,
port=POSTGRES_PORT,
database=POSTGRES_DB)
db_connection = DBConnection(connection_pool)
Finally, in the main code. To execute the query just create a DBSession instance, and call the A module functions by using de query function
import A
db_session = db_connection.get_session()
a_1 = db_session.query(A.find_by_id, 1)
print(a_1)
# (1, 'Paul')
all_a = db_session.query(A.get_all)
print(all_a)
# [(1, 'Paul'), (2, 'George')]
Metadata
Release files for dbsession 0.6.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dbsession-0.6.7.tar.gz | 4.9 kB | Without provenance | Details for dbsession-0.6.7.tar.gz |
Release files / dbsession-0.6.7.tar.gz
| Download URL | dbsession-0.6.7.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c3dd8ce8ecc8bee1f3fe6a83ac411feaee5333407a590195aacca9143301c03
|
|
BLAKE2b-256 checksum How to use checksums |
6fa346a43f3a1038096c40bc0cf61668fc392d5449f810626b12014d2b498d3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/18.5 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/2.7.16
|
Security for dbsession 0.6.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.