Introduction
PyPQL is a lightweight Python library that simplifies working with PostgreSQL. It is designed for projects that need easy CRUD operations with strong consistency but do not require the complexity of large-scale ORM frameworks.
Installation
pip install pypql
Quick Start
import asyncio
from pypql import tableobj
users = Table('user', 'password', 'database', 'host', 'table_name')
await users.connect()
await users.insert_single(['name'], ['Bobby']) # Inserts new row with value "Bobby" into name column of users table
await users.close()
API Overview
-
Database class A class to manage an asynchronous connection to a PostgreSQL database using `asyncpg`.
-
Attributes:
- _db_config (dict): Configuration for database connection containing user, password, database, & host.
- connection (asyncpg.Connection): The active database connection or `None` if not connected.
-
Methods:
- __init__(user, password, database, host): Initializes the database configuration with the given parameters.
- user (str):The username for the database.
- password (str): The password for the database.
- database (str): The name of the database to connect to.
- host (str): The hostname or IP address of the database server.
- connect(): Establishes an asynchronous connection to the PostgreSQL database. Returns asyncpg.Connection.
- close(): Closes the active database connection.
- connection_info(): Returns information about the current connection state. Retrieves information about the current connection state (dict).
- __init__(user, password, database, host): Initializes the database configuration with the given parameters.
-
Attributes:
-
Table class The Table class extends the `Database` clas to manage table-specific operations in a PostgreSQL database.
-
Attributes:
- table_name (str): The name of the table the class interacts with.
-
Methods:
- insert_single(columnNames, values): Inserts a single row into the table.
- insert_multiple(columnNames, values): Inserts multiple rows into the table.
- del_single(column, value): Deletes a single row based on a column-value pair.
- del_multiple(column, values): Deletes multiple rows based on a column and a list of values.
- del_custom(condition): Deletes rows based on a custom condition.
- update(setColumn, newVal, conditionColumn, conditionVal): Updates rows based on a condition.
- exc(func, *args): Executes a function with database connectivity management.
- select(selection, returnMethod): Selects and returns data from the table with different return formats.
- select_conditional(selection, condition, returnMethod): Selects and returns data from the table based on a condition.
- customQuery(queryType, query): Executes a custom SQL query based on the type.
- tableInfo(): Retrieves metadata about the table, including column names and row count.
-
Attributes:
- select_all() – Returns all rows.
- select_where(**kwargs) – Returns rows matching a condition.
- update(where: dict, values: dict) – Updates rows matching a condition.
- delete(**kwargs) – Deletes rows matching a condition.
Best Practices
- Always close connections when your app shuts down.
- Use transactions for bulk operations.
- Store your database URI in environment variables for production.