Skip to main content

abarorm is a lightweight and easy-to-use Object-Relational Mapping (ORM) library for SQLite & PostgreSQL and MySQL databases in Python. It aims to provide a simple and intuitive interface for managing database models and interactions.

Project description

abarorm

abarorm Logo abarorm is a lightweight and easy-to-use Object-Relational Mapping (ORM) library for SQLite, MySQL, and PostgreSQL databases in Python. It provides a simple and intuitive interface for managing database models and interactions.

Features

  • Define models using Python classes.
  • Automatically handle database schema creation and management.
  • Support for basic CRUD (Create, Read, Update, Delete) operations.
  • Manage foreign key relationships effortlessly.
  • Custom field types with validation and constraints.
  • New in v1.0.0: Automatic table creation and updates without needing explicit create_table() calls.
  • New in v2.0.0: Added support for PostgreSQL databases.
  • New in v2.0.0: Ordering by fields in the all() method.
  • New in v3.0.0: Fixed table naming bugs to ensure consistent naming conventions.
  • New in v3.0.0: Updated return values for methods to improve clarity and usability.
  • New in v3.0.0: Enhanced filter method now supports order_by functionality for result ordering.
  • New in v3.2.0: Added __gte and __lte functionality in the filter section.
  • New in v4.0.0: Added __repr__, count, and to_dict methods for easier data manipulation and debugging.

Installation

You can install abarorm from PyPI using pip:

pip install abarorm

For MySQL support, you also need to install mysql-connector-python:

pip install mysql-connector-python

For PostgreSQL support, you need to install psycopg2-binary:

pip install psycopg2-binary

Documentation

For detailed documentation, examples, and advanced usage, please visit the official abarorm documentation website.

Database Configuration

Before defining models, you need to set up your database configuration. This involves specifying connection parameters for the database you are using (SQLite, MySQL, or PostgreSQL). Here’s an example of how to configure the database:

# Database configuration
DATABASE_CONFIG = {
    'sqlite': {
        'db_name': 'example.db',  # Name of the SQLite database file
    },
    'mysql': {
        'host': 'localhost',
        'user': 'your_mysql_user',
        'password': 'your_mysql_password',
        'database': 'example_db',
    },
    'postgresql': {
        'host': 'localhost',
        'user': 'your_pg_user',
        'password': 'your_pg_password',
        'database': 'example_db',
    }
}

Model Definition

After setting up the database configuration, you can define your models. A model is a representation of a database table. Here’s how to create a model using abarorm:

from abarorm import SQLiteModel, MySQLModel, PostgreSQLModel
from abarorm.fields import CharField, DateTimeField, ForeignKey

# Define the Category model for SQLite
class Category(SQLiteModel):
    class Meta:
        db_config = DATABASE_CONFIG['sqlite']
        table_name = 'categories'  # Name of the table for storing the Category model data

    title = CharField(max_length=200, null=False)  # Title of the category, must be unique and not null
    create_time = DateTimeField(auto_now=True, auto_now_add=True)  # Automatically set to current datetime
    update_time = DateTimeField(auto_now=True)  # Automatically set to current datetime


# Define the Post model for MySQL
class Post(MySQLModel):
    class Meta:
        db_config = DATABASE_CONFIG['mysql']

    title = CharField(max_length=100, null=False)  # Title of the post, must be unique and not null
    create_time = DateTimeField(auto_now=True)  # Automatically set to current datetime
    category = ForeignKey(to=Category)  # Foreign key referring to the Category model

CRUD Operations

Now that you have defined your models, you can perform CRUD operations. Here’s a breakdown of each operation:

Create

To create new records in the database, use the create() method. For example:

# Create a new category
Category.create(title='Movies')

# Create a new post
category = Category.get(id=1)  # Fetch the category with ID 1
if category:
    Post.create(title='Godfather', category=category.id)  # Create a new post associated with the fetched category

Read

To read records from the database, use the all() or get() methods:

# Retrieve all posts
all_posts = Post.all()

# Retrieve a specific post by ID
post_data = Post.get(id=1)

Filtering Records

The filter() method allows you to retrieve records based on specified criteria. You can use keyword arguments to filter by field values and sort the results using order_by.

# Filter posts by category ID and order by creation time
filtered_posts = Post.filter(category=category.id, order_by='-create_time')

Advanced Filtering

You can also use special lookup expressions like __gte (greater than or equal to) and __lte (less than or equal to) for more complex queries:

# Retrieve posts created after a specific date
filtered_posts = Post.filter(create_time__gte='2024-01-01 00:00:00')

Update

To update existing records, fetch the record, modify its attributes, and then save it:

if post_data:
    post_data.title = "The Godfather"
    post_data.save()  # Save the updated post data

Or:

Post.update(1, title='Updated Godfather')  # Update the title of the post with ID 1 to 'Updated Godfather'

Delete

To delete a record from the database, use the delete() method:

Post.delete(1)  # Delete the post with ID 1

Converting to Dictionary and Counting Records

After performing operations on the model, you can convert records to dictionaries using the to_dict() method and count the number of records using the count() method.

to_dict Method

The to_dict() method converts a model instance into a dictionary, making it easier to manipulate and serialize the data.

Example:

# Retrieve a post by ID
post = Post.get(id=1)

# Convert the post to a dictionary
post_dict = post.all().to_dict()
print(post_dict)
# Output: [{'id': 1, 'title': 'Godfather', 'create_time': '2024-01-01 12:00:00', ...}]

count Method

The count() method allows you to get the number of records in a model's table.

Example:

# Count the number of posts in the database
num_posts = Post.count()
print(num_posts)  # Output: 10 (if there are 10 posts in the database)

These methods are particularly useful for data manipulation and debugging, as they provide a simple way to view and interact with your database records.

Version 4.0.0

  • __repr__ Method: Added to provide a string representation of model instances for better debugging.
  • count Method: Added to count the number of records in a model's table.
  • to_dict Method: Converts model instances into dictionaries for easier manipulation and serialization.

Important for Developers: When adding new fields to models, they will default to NULL. It’s recommended to recreate the database schema after development is complete to ensure fields have appropriate constraints and default values.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.

License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

Acknowledgements

  • Python: The language used for this project.
  • SQLite & MySQL: The databases supported by this project.
  • setuptools: The tool used for packaging and distributing the library.
  • psycopg2-binary: The PostgreSQL adapter used for connecting to PostgreSQL databases.

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

abarorm-4.0.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

abarorm-4.0.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file abarorm-4.0.0.tar.gz.

File metadata

  • Download URL: abarorm-4.0.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for abarorm-4.0.0.tar.gz
Algorithm Hash digest
SHA256 de18e88ba2c1895f0c047afcfd493202d9bb0a1e1d89709a50e7d7e360434687
MD5 9a8946fe2450bb535115a610b02acf1c
BLAKE2b-256 7b9288c2af2ac9a684602a4c851ed7c421397183c66bc2adc95c68997004915b

See more details on using hashes here.

File details

Details for the file abarorm-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: abarorm-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for abarorm-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff013de4a5ec9a9f4497f6678218a4b5c764879b1aa6ccec1aa3ced357d5421f
MD5 57d82a23eb8c6dd0335233ee7acdbbe6
BLAKE2b-256 c62aad7d53b5433d4a6a867976a01d9fb0f6206fb9666175c647016286cbd6f9

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