# SQLAlchemy-Catalyst
SQLAlchemy-Catalyst is a collection of helper methods for base SQLAlchemy classes.
## Installation
You can install the package using pip:
```
pip install SQLAlchemy-Catalyst
```
## Usage
### Importing
`database.py`:
```
from SQLAlchemy_Catalyst import Catalyst
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///example.db')
Session = sessionmaker(bind=engine)
class ExampleModel(Catalyst):
__tablename__ = 'example_table'
id = Column(Integer, primary_key=True)
name = Column(String(50))
age = Column(Integer)
```
### Implementing
`implementation.py`:
```
from database import ExampleModel, Session
example = ExampleModel(name='John', age=30)
session = Session()
session.add(example)
session.commit()
# Call the methods on the example instance
example.add_if_new(session, ['name'], {'name': 'Mary'})
example.add_or_update(session, {'name': 'Mary'}, {'age': 40})
example.update_if_empty(session, {'name': 'Mary'}, {'age': 40})
example.merge_records_attributes(session, {'name': 'Mary'}, {'age': 40})
example.bulk_upsert(session, {'name': 'Mary'}, {'age': 40})
example.increment_column(session, {'name': 'Mary'}, 'age', 10)
example.decrement_column(session, {'name': 'Mary'}, 'age', 5)
```
## Methods available
### add_if_new(session, column_names, values)
Adds a new record to the database if no record exists with the provided values.
* `session`: An active database session object.
* `column_names`: A list of column names that need to be checked.
* `values`: A dictionary of column names and values to be added.
### add_or_update(session, filters, values)
Adds a new record to the database or updates an existing record.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for an existing record.
* `values`: A dictionary of column names and values to be added/updated.
### update_if_empty(session, filters, values)
Updates an existing record in the database if the specified columns are empty.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for an existing record.
* `values`: A dictionary of column names and values to be updated.
### merge_records_attributes(session, filters, values)
Merges the attributes of two records into one.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for an existing record.
* `values`: A dictionary of column names and values to be merged.
### bulk_upsert(session, filters, values)
Updates existing records in the database or inserts new records.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for existing records.
* `values`: A dictionary of column names and values to be updated/added.
### increment_column(session, filters, column_name, value)
Increments a numeric column in an existing record.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for an existing record.
* `column_name`: The name of the numeric column to be incremented.
* `value`: The value to increment the column by (default is 1).
### decrement_column(session, filters, column_name, value)
Decrements a numeric column in an existing record.
* `session`: An active database session object.
* `filters`: A dictionary of column names and values to be checked for an existing record.
* `column_name`: The name of the numeric column to be decremented.
* `value`: The value to decrement the column by (default is 1).
## License
This project is licensed under the **MIT License** - see the [LICENSE](https://mit-license.org/) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/dcavalcante/SQLAlchemy-Catalyst",
"name": "SQLAlchemy-Catalyst",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "sqlalchemy,sql,mysql,sqlite,postgresql,helpers",
"author": "Daniel Cavalcante",
"author_email": "dcavalcante@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c8/85/dc0b8ad85c8964c8daa287b2456adc9175d94261cb6dd33293066d5d91ea/SQLAlchemy-Catalyst-0.1.tar.gz",
"platform": null,
"description": "# SQLAlchemy-Catalyst\nSQLAlchemy-Catalyst is a collection of helper methods for base SQLAlchemy classes.\n\n## Installation\nYou can install the package using pip:\n```\npip install SQLAlchemy-Catalyst\n```\n\n## Usage\n### Importing\n`database.py`:\n```\nfrom SQLAlchemy_Catalyst import Catalyst\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\n\nengine = create_engine('sqlite:///example.db')\nSession = sessionmaker(bind=engine)\n\nclass ExampleModel(Catalyst):\n __tablename__ = 'example_table'\n\n id = Column(Integer, primary_key=True)\n name = Column(String(50))\n age = Column(Integer)\n\n``` \n### Implementing\n`implementation.py`:\n```\nfrom database import ExampleModel, Session\nexample = ExampleModel(name='John', age=30)\nsession = Session()\nsession.add(example)\nsession.commit()\n\n# Call the methods on the example instance\nexample.add_if_new(session, ['name'], {'name': 'Mary'})\nexample.add_or_update(session, {'name': 'Mary'}, {'age': 40})\nexample.update_if_empty(session, {'name': 'Mary'}, {'age': 40})\nexample.merge_records_attributes(session, {'name': 'Mary'}, {'age': 40})\nexample.bulk_upsert(session, {'name': 'Mary'}, {'age': 40})\nexample.increment_column(session, {'name': 'Mary'}, 'age', 10)\nexample.decrement_column(session, {'name': 'Mary'}, 'age', 5)\n\n```\n\n## Methods available\n### add_if_new(session, column_names, values)\nAdds a new record to the database if no record exists with the provided values.\n\n* `session`: An active database session object.\n* `column_names`: A list of column names that need to be checked.\n* `values`: A dictionary of column names and values to be added.\n\n### add_or_update(session, filters, values)\nAdds a new record to the database or updates an existing record.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for an existing record.\n* `values`: A dictionary of column names and values to be added/updated.\n\n### update_if_empty(session, filters, values)\nUpdates an existing record in the database if the specified columns are empty.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for an existing record.\n* `values`: A dictionary of column names and values to be updated.\n\n### merge_records_attributes(session, filters, values)\nMerges the attributes of two records into one.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for an existing record.\n* `values`: A dictionary of column names and values to be merged.\n\n### bulk_upsert(session, filters, values)\nUpdates existing records in the database or inserts new records.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for existing records.\n* `values`: A dictionary of column names and values to be updated/added.\n\n### increment_column(session, filters, column_name, value)\nIncrements a numeric column in an existing record.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for an existing record.\n* `column_name`: The name of the numeric column to be incremented.\n* `value`: The value to increment the column by (default is 1).\n\n### decrement_column(session, filters, column_name, value)\nDecrements a numeric column in an existing record.\n\n* `session`: An active database session object.\n* `filters`: A dictionary of column names and values to be checked for an existing record.\n* `column_name`: The name of the numeric column to be decremented.\n* `value`: The value to decrement the column by (default is 1).\n\n\n## License\nThis project is licensed under the **MIT License** - see the [LICENSE](https://mit-license.org/) file for details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Collection of methods for base SQLAlchemy classes",
"version": "0.1",
"split_keywords": [
"sqlalchemy",
"sql",
"mysql",
"sqlite",
"postgresql",
"helpers"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "db594c2f3e37d95d2838640de13df0f7f17082a141483d450aba392654bbc013",
"md5": "ebf0e5a780fa2f605ce3e964d340e045",
"sha256": "174ba5ec609934bc0523ec70b5dcafaf89858f2891fba237b09f89dbc5d2b4ec"
},
"downloads": -1,
"filename": "SQLAlchemy_Catalyst-0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ebf0e5a780fa2f605ce3e964d340e045",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4383,
"upload_time": "2023-04-17T21:03:02",
"upload_time_iso_8601": "2023-04-17T21:03:02.941915Z",
"url": "https://files.pythonhosted.org/packages/db/59/4c2f3e37d95d2838640de13df0f7f17082a141483d450aba392654bbc013/SQLAlchemy_Catalyst-0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c885dc0b8ad85c8964c8daa287b2456adc9175d94261cb6dd33293066d5d91ea",
"md5": "ad2094598c048dc538dc63d06cf4952e",
"sha256": "d5ece6925b9ab401a8083f2d495b25d70fb8c0c826515663f81cce0f91c96288"
},
"downloads": -1,
"filename": "SQLAlchemy-Catalyst-0.1.tar.gz",
"has_sig": false,
"md5_digest": "ad2094598c048dc538dc63d06cf4952e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3814,
"upload_time": "2023-04-17T21:03:06",
"upload_time_iso_8601": "2023-04-17T21:03:06.841418Z",
"url": "https://files.pythonhosted.org/packages/c8/85/dc0b8ad85c8964c8daa287b2456adc9175d94261cb6dd33293066d5d91ea/SQLAlchemy-Catalyst-0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-17 21:03:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "dcavalcante",
"github_project": "SQLAlchemy-Catalyst",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "sqlalchemy-catalyst"
}