Skip to main content

Collection of methods for base SQLAlchemy classes

Project description

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 file for details.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

SQLAlchemy-Catalyst-0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

SQLAlchemy_Catalyst-0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file SQLAlchemy-Catalyst-0.1.tar.gz.

File metadata

  • Download URL: SQLAlchemy-Catalyst-0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for SQLAlchemy-Catalyst-0.1.tar.gz
Algorithm Hash digest
SHA256 d5ece6925b9ab401a8083f2d495b25d70fb8c0c826515663f81cce0f91c96288
MD5 ad2094598c048dc538dc63d06cf4952e
BLAKE2b-256 c885dc0b8ad85c8964c8daa287b2456adc9175d94261cb6dd33293066d5d91ea

See more details on using hashes here.

File details

Details for the file SQLAlchemy_Catalyst-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for SQLAlchemy_Catalyst-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 174ba5ec609934bc0523ec70b5dcafaf89858f2891fba237b09f89dbc5d2b4ec
MD5 ebf0e5a780fa2f605ce3e964d340e045
BLAKE2b-256 db594c2f3e37d95d2838640de13df0f7f17082a141483d450aba392654bbc013

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