Skip to main content

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.

Read this in Russian

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


Download files

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

Source Distribution

dbrequest-0.2.1.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

dbrequest-0.2.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

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

Hashes for dbrequest-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2a3438da6fae90b3a2b0728ea4e412ff06634d88ade640bf4c3ab3e38956d0e1
MD5 29669ab61455feb7bed0333c31dded36
BLAKE2b-256 a0f6569e67e610e90d9fdbac4b2190fd7c23431f2d6e8710ca9c036691a3f817

See more details on using hashes here.

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

Hashes for dbrequest-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ada7405143ee9358e9f82895d3257aeb6586c26639b7b2da31c39bbc60c387f6
MD5 624abffd3a05c3e369a4e6e26de83c1c
BLAKE2b-256 3b01cd0773faef773793d3f0b19c4a052c05abca474b31abdc444a80336064b1

See more details on using hashes here.

Supported by

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