{"id":21449292,"url":"https://github.com/prodbygodfather/abarorm","last_synced_at":"2025-06-15T06:38:54.926Z","repository":{"id":253556897,"uuid":"843790698","full_name":"ProdByGodfather/abarorm","owner":"ProdByGodfather","description":"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.","archived":false,"fork":false,"pushed_at":"2025-04-08T09:25:18.000Z","size":1739,"stargazers_count":3,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T10:28:55.155Z","etag":null,"topics":["mysql-orm","orm","postgresql-orm","python-orm","sqlite-orm"],"latest_commit_sha":null,"homepage":"https://prodbygodfather.github.io/abarorm/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ProdByGodfather.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-17T12:33:10.000Z","updated_at":"2025-04-08T09:25:19.000Z","dependencies_parsed_at":"2024-12-30T08:21:51.755Z","dependency_job_id":"83894907-3212-4dbf-9a3c-772589b28521","html_url":"https://github.com/ProdByGodfather/abarorm","commit_stats":null,"previous_names":["prodbygodfather/abarorm"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/ProdByGodfather/abarorm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProdByGodfather%2Fabarorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProdByGodfather%2Fabarorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProdByGodfather%2Fabarorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProdByGodfather%2Fabarorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProdByGodfather","download_url":"https://codeload.github.com/ProdByGodfather/abarorm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProdByGodfather%2Fabarorm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259935037,"owners_count":22934338,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql-orm","orm","postgresql-orm","python-orm","sqlite-orm"],"created_at":"2024-11-23T03:19:40.036Z","updated_at":"2025-06-15T06:38:54.912Z","avatar_url":"https://github.com/ProdByGodfather.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# abarorm\n\n| ![abarorm Logo](https://prodbygodfather.github.io/abarorm/images/logo.png) | **abarorm** is a lightweight and easy-to-use Object-Relational Mapping (ORM) library for SQLite and PostgreSQL databases in Python. It provides a simple and intuitive interface for managing database models and interactions. |\n|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n## Features\n\n- Define models using Python classes.\n- Automatically handle database schema creation and management.\n- Support for basic CRUD (Create, Read, Update, Delete) operations.\n- Manage foreign key relationships effortlessly.\n- Custom field types with validation and constraints.\n- **New in v1.0.0**: Automatic table creation and updates without needing explicit `create_table()` calls.\n- **New in v2.0.0**: Added support for PostgreSQL databases.\n- **New in v2.0.0**: Ordering by fields in the `all()` method.\n- **New in v3.0.0**: Fixed table naming bugs to ensure consistent naming conventions.\n- **New in v3.0.0**: Updated return values for methods to improve clarity and usability.\n- **New in v3.0.0**: Enhanced `filter` method now supports `order_by` functionality for result ordering.\n- **New in v3.2.0**: Added `__gte` and `__lte` functionality in the filter section.\n- **New in v4.0.0**: Added `__repr__`, `count`, and `to_dict` methods.\n- **New in v4.2.3**: Added `first()`, `last()`, `exists()`, and `paginate()` methods to the QuerySet class.\n- **New in v5.0.0**: Fix `PostgreSQL` Bugs and structure.\n- **New in v5.1.0**: Enhanced functionality for better usability for `delete` and `contains` methods.\n- **New in v5.2.0**: Introduced `bulk_create` for efficient batch insertions.\n\n\n\n\n## Installation\n\nYou can install [**abarorm**](https://pypi.org/project/abarorm/) from PyPI using pip:\n\n```bash\npip install abarorm\n```\n\nFor PostgreSQL support, you need to install `psycopg2-binary`:\n```bash\npip install psycopg2-binary\n```\n\n## Documentation\nFor detailed documentation, examples, and advanced usage, please visit the [official abarorm documentation website](https://prodbygodfather.github.io/abarorm/).\n\n## Database Configuration\nBefore defining models, you need to set up your database configuration. This involves specifying connection parameters for the database you are using (SQLite and PostgreSQL). Here’s an example of how to configure the database:\n```python\n# Database configuration\nDATABASE_CONFIG = {\n    'sqlite': {\n        'db_name': 'example.db',  # Name of the SQLite database file\n    },\n    'postgresql': {\n        'host': 'localhost',\n        'user': 'hoopad',\n        'password': 'db_password',\n        'database': 'example_db',  # Ensure this matches everywhere\n        'port': 5432,\n    }\n}\n```\n## Model Definition\nAfter 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:\n```python\nfrom abarorm import SQLiteModel, PostgreSQLModel\nfrom abarorm.fields.sqlite import CharField, DateTimeField, ForeignKey\nfrom abarorm.fields import psql\n\n# Define the Category model for SQLite\nclass Category(SQLiteModel):\n    class Meta:\n        db_config = DATABASE_CONFIG['sqlite']\n        table_name = 'categories'  # Name of the table for storing the Category model data\n\n    title = CharField(max_length=200, null=False)  # Title of the category, must be unique and not null\n    create_time = DateTimeField(auto_now=True, auto_now_add=True)  # Automatically set to current datetime\n    update_time = DateTimeField(auto_now=True)  # Automatically set to current datetime\n\n\n# Define the Post model for Postgresql\nclass Post(PostgreSQLModel):\n    class Meta:\n        db_config = DATABASE_CONFIG['postgresql']\n\n    title = psql.CharField(max_length=100, null=False)  # Title of the post, must be unique and not null\n    create_time = psql.DateTimeField(auto_now=True)  # Automatically set to current datetime\n    category = psql.ForeignKey(to=Category)  # Foreign key referring to the Category model\n```\n## CRUD Operations\nNow that you have defined your models, you can perform CRUD operations. Here’s a breakdown of each operation:\n### Create\nTo create new records in the database, use the `create()` method or **Bulk Create**: `bulk_create`. For example:\n```python\n# Create a new category\nCategory.create(title='Movies')\n\n# Create a new post\ncategory = Category.get(id=1)  # Fetch the category with ID 1\nif category:\n    Post.create(title='Godfather', category=category.id)  # Create a new post associated with the fetched category\n\n# Bulk Create Post\nrecords = [\n    {\"title\": \"Godfather Part II\", \"category\": 1},\n    {\"title\": \"Godfather Part III\", \"category\": 1},\n]\nPost.bulk_create(records)\n```\n### Read\nTo read records from the database, use the `all()` or `get()` methods:\n```python\n# Retrieve all posts\nall_posts = Post.all()\n\n# Retrieve a specific post by ID\npost_data = Post.get(id=1)\n```\n### Filtering Records\nThe `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`.\n```python\n# Filter posts by category ID and order by creation time\nfiltered_posts = Post.filter(category=category.id, order_by='-create_time')\n```\n#### Advanced Filtering\nYou can also use special lookup expressions like `__gte` (greater than or equal to) and `__lte` (less than or equal to) for more complex queries:\n```python\n# Retrieve posts created after a specific date\nfiltered_posts = Post.filter(create_time__gte='2024-01-01 00:00:00')\n```\n\n### Update\nTo update existing records, fetch the record, modify its attributes, and then save it:\n```python\nif post_data:\n    post_data.title = \"The Godfather\"\n    post_data.save()  # Save the updated post data\n```\nOr:\n```python\nPost.update(1, title='Updated Godfather')  # Update the title of the post with ID 1 to 'Updated Godfather'\n```\n### Delete\nTo delete a record from the database, use the `delete()` method:\n```python\nPost.delete(id=1)  # Delete the post with ID 1\n# or Removal based on a sometimes repeated argument\nPost.delete(title='Godfather')\n```\n## Converting to Dictionary, Counting Records, and Other Query Methods\n\nThis section covers how to convert records to dictionaries, count records, and use various query methods like `first()`, `last()`, `exists()`, `order_by()`, `paginate()`, and `contains()`. These methods are essential for data manipulation, debugging, and optimizing query performance.\n\n### `to_dict()` Method\nThe `to_dict()` method converts a model instance (or a collection of instances) into a dictionary, which is particularly useful for data manipulation and serialization. It makes it easier to work with the data in Python or send it over an API.\n\n#### Example:\n```python\n# Retrieve all posts\nposts = Post.all()\n\n# Convert the collection of posts to a list of dictionaries\nposts_dict = posts.to_dict()\nprint(posts_dict)\n# Output: [{'id': 1, 'title': 'Godfather', 'create_time': '2024-01-01 12:00:00', ...}, {...}]\n```\n\n#### `count` Method\nThe `count()` method returns the number of records that match the query. It’s an efficient way to find the size of a dataset without retrieving the actual data.\n\n**Example:**\n```python\n# Count the number of posts in the database\nnum_posts = Post.count()\nprint(num_posts)  # Output: 10 (if there are 10 posts in the database)\n```\n\nThe `count()` method can also be used after applying filters to count specific records:\n```python\n# Count the number of posts with a specific title\nnum_posts_with_title = Post.filter(title='Godfather').count()\nprint(num_posts_with_title)  # Output: 3 (if there are 3 posts with the title 'Godfather')\n```\n\n\n\n#### `first()`, `last()`, `exists()`, `order_by()`, `paginate()` and `contains()`\n- **`first()`**: Returns the first result or `None` if no results are present.\n- **`last()`**: Returns the last result or `None` if no results are present.\n- **`exists()`**: Checks if any records exist in the `QuerySet`.\n- **`paginate()`**: Handles pagination of results, allowing you to retrieve subsets of data based on page and page size.\n- **`contains()`**: Performs a case-insensitive search to check if a field contains a specific substring.\n\n**Example:**\n```python\n# Check if any posts exist\nexists = Post.all().exists()\n\n# Get the first post\nfirst_post = Post.all().first()\n\n# Get the last post\nlast_post = Post.all().last()\n\n# Paginate the results\npaginated_posts = Post.all().paginate(1, 5)  # Page 1, 5 results per page\n\n# Using multiple querysets in one query\nposts = Post.all().contains(title='god').order_by('create_time').paginate(1, 4).to_dict()\n```\n\nThese methods are particularly useful for data manipulation and debugging, as they provide a simple way to view and interact with your database records.\n\n\n## Contributing\n\nContributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.\n\n## License\n\nThis project is licensed under the [Apache-2.0 License](https://github.com/ProdByGodfather/abarorm/blob/main/LICENSE) - see the LICENSE file for details.\n\n## Acknowledgements\n\n- **Python**: The language used for this project.\n- **SQLite and Postgresql**: The databases supported by this project.\n- **setuptools**: The tool used for packaging and distributing the library.\n- **psycopg2-binary**: The PostgreSQL adapter used for connecting to PostgreSQL databases.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodbygodfather%2Fabarorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprodbygodfather%2Fabarorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodbygodfather%2Fabarorm/lists"}