Skip to main content

A tiny ORM for DuckDB

Project description

duckdb-tinyorm-py

PyPI version

Overview

duckdb-tinyorm-py is a lightweight ORM (Object-Relational Mapping) library designed to work seamlessly with DuckDB. It provides an elegant, Pythonic interface for defining entities, managing relationships, and performing database operations.

Installation

You can install the library using pip:

pip install duckdb-tinyorm-py

For export functionality, install additional dependencies:

pip install pyarrow  # For Parquet export support

Basic Usage

Define an Entity

from duckdb_tinyorm_py import entity, field, id_field

@entity(table_name="courses")
class Course:
    def __init__(self, id_=None, name="", department=""):
        self._id = id_
        self._name = name
        self._department = department

    @property
    @id_field('INTEGER', auto_increment=True)
    def id(self) -> int:
        return self._id
    
    @id.setter
    def id(self, value: int):
        self._id = value

    @property
    @field('VARCHAR', not_null=True)
    def name(self) -> str:
        return self._name
        
    @name.setter
    def name(self, value: str):
        self._name = value
        
    @property
    @field('VARCHAR', not_null=True)
    def department(self) -> str:
        return self._department
    
    @department.setter
    def department(self, value: str):
        self._department = value

Create a Repository

from duckdb_tinyorm_py import BaseRepository, repository, DuckDbConfig, DuckDbLocation

# Configure your database
config = DuckDbConfig(
    name='mydb',
    location=DuckDbLocation.FILE,
    filename='my_database.db'
)

@repository(Course)
class CourseRepository(BaseRepository[Course, int]):
    # Custom repository methods
    async def find_by_department(self, department):
        """Find courses by department"""
        return await self.find_by({"department": department})

Use the Repository

import asyncio

async def main():
    # Initialize repository and create table
    repo = CourseRepository()
    await repo.init()
    
    # Create and save entities
    course = Course(name="Python Programming", department="CS")
    saved = await repo.save(course)
    print(f"Saved course ID: {saved.id}")
    
    # Find entities
    all_courses = await repo.find_all()
    cs_courses = await repo.find_by_department("CS")
    
    # Update an entity
    course.name = "Advanced Python"
    updated = await repo.save(course)
    
    # Delete an entity
    await repo.remove(course)
    
    # Export to different formats
    df = repo.to_dataframe()
    json_data = await repo.to_json()
    await repo.to_parquet(path="courses.parquet")

# Run the async code
asyncio.run(main())

Features

  • Property-based entity mapping with type hints
  • Auto-incrementing primary keys
  • Advanced querying capabilities
  • Transaction support
  • Data export to DataFrame, JSON, CSV, and Parquet
  • Support for table indices
  • Connection pooling and configuration

Advanced Features

See the advanced_usage.py file for examples of:

  • Complex queries with pagination
  • Filtering and sorting
  • Transaction safety
  • Data imports/exports
  • Custom repository methods

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any enhancements or bugs.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Bye me a Caffee!

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

duckdb_tinyorm_py-1.0.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

duckdb_tinyorm_py-1.0.0-py2.py3-none-any.whl (21.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file duckdb_tinyorm_py-1.0.0.tar.gz.

File metadata

  • Download URL: duckdb_tinyorm_py-1.0.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.14.0-1017-azure

File hashes

Hashes for duckdb_tinyorm_py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6965c099391938c9d0c5e992e88fe9ba33e90a77f535ca170cbced2cf10e233a
MD5 e109ad0a1d1eb30a2b451f3f8703c063
BLAKE2b-256 d7d840034f71ad21efd3e265b666dc913bb44b6d105503b73b77893073c9b525

See more details on using hashes here.

File details

Details for the file duckdb_tinyorm_py-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: duckdb_tinyorm_py-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.14.0-1017-azure

File hashes

Hashes for duckdb_tinyorm_py-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 be3e30e1fb6a43dbbf4d4fc10551bc848b45ac8139cfab01e54d35a928859a00
MD5 713b0f3fda2cacbbc5556b88355b1f9a
BLAKE2b-256 a161d44b6eae708c1c030a8250068e8fcfe6ec63621e4887cb4cb9bcf5e8037b

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