Simple ORM library for easy database handling.
Project description
dbrequest
dbrequest is a simple ORM library for easy database handling. The library is primarily designed for simple projects where complex SQL queries are not needed and SQLite can be used as the DBMS (although the library supports other DBMS as well).
The library provides an abstraction from the DBMS and allows working with storing, loading, modifying, and deleting model objects without explicit use of SQL.
Contents
Installation
Installation from the PyPI repository:
$ pip install dbrequest
Installation from a GitHub repository (requires pip version 20 and above).
$ pip install git+https://github.com/korandr/dbrequest.git
Library import:
import dbrequest
Quick Start
For example, let's create a table users. Let's describe it in the tables.sql file:
create table IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
last_message TEXT DEFAULT NULL
);
Let's initialize the library:
import dbrequest
dbrequest.init(init_script='tables.sql')
Next, define the model as User class:
class User:
def __init__(self, id: int | None = None, username: str | None = None) -> None:
self.id = id
self.username = username
self.last_message: str | None = None
Now let's create IField objects for all saved fields of the class:
from dbrequest import AutoField
id_field = AutoField('id', int, allowed_none=True)
username_field = AutoField('username', str)
last_message_field = AutoField('last_message', str, allowed_none=True)
Here the dbrequest.AutoField class is using, becouse the names of the fields and the corresponding columns in the table are the same.
In more complex cases, you can use the dbrequest.BaseField class and explicitly specify the name matches.
All that remains is to create an object for queries to the database:
from dbrequest import BaseDBRequest
user_db_request = BaseDBRequest[User](
model_type = User,
table_name = 'users',
fields = (
id_field,
username_field,
last_message_field,
),
key_fields = (
id_field,
username_field,
),
)
In the fields parameter we specify a tuple of all fields in the order in which the columns were created in the table.
key_fields - fields with unique values that can be used to find a specific object in the database.
The Generic parameter of the BaseDBRequest class is not required, but will help in typing the return of the load_all method.
Now you can conveniently perform operations with the User class and the database:
user = User(username='simple_user')
user_db_request.save(user)
user = user_db_request.load_all(User(), limit=1, reverse=True)[0]
print(user.id)
same_user = User(id=user.id)
user_db_request.load(same_user)
print(same_user.username)
user.last_message = 'Hello world!'
user_db_request.update(user)
admin = User(username='admin')
admin.last_message = 'Do you want to be banned?'
user_db_request.save(admin)
users = user_db_request.load_all(User())
for user in users:
print(f'The user who said "{user.last_message}" has been deleted')
user_db_request.delete(user)
See the code for this and other examples
Documentation
Basic documentation is contained in the library (docstring).
Full documentation for the library is only available in Russian.
(Use Google Translate as a browser extension, as it works really well)
Feedback
Developer: Andrey Korovyanskiy | andrey.korovyansky@gmail.com
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dbrequest-0.2.1.tar.gz.
File metadata
- Download URL: dbrequest-0.2.1.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a3438da6fae90b3a2b0728ea4e412ff06634d88ade640bf4c3ab3e38956d0e1
|
|
| MD5 |
29669ab61455feb7bed0333c31dded36
|
|
| BLAKE2b-256 |
a0f6569e67e610e90d9fdbac4b2190fd7c23431f2d6e8710ca9c036691a3f817
|
File details
Details for the file dbrequest-0.2.1-py3-none-any.whl.
File metadata
- Download URL: dbrequest-0.2.1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ada7405143ee9358e9f82895d3257aeb6586c26639b7b2da31c39bbc60c387f6
|
|
| MD5 |
624abffd3a05c3e369a4e6e26de83c1c
|
|
| BLAKE2b-256 |
3b01cd0773faef773793d3f0b19c4a052c05abca474b31abdc444a80336064b1
|