Skip to main content

Storage based off of sqlalchemy_marshmallow

Project description

Quickstart

alchemical-storage is a library intended to bridge CRUD operations with SQLAlchemy query constructs

Install

pip install alchemical-storage

Basic Usage

alchemical-storage assumes that you have set up a session (or scoped_session) for your database. This is assumed to have been imported as session in the following example. The table for the defined model is also assumed to be in the database.

  1. Set up the model.

    """package/models.py"""
    from sqlalchemy import orm
    
    class Model(orm.DeclarativeBase):
        """Model class"""
        __tablename__ = 'models'
        attr: orm.Mapped[int] = orm.mapped_column(primary_key=True)
        attr2: orm.Mapped[int]
        attr3: orm.Mapped[str]
    
  2. Set up the storage schema. The Meta class in the schema should set load_instance to True.

    """package/schema.py"""
    from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
    from package import Model
    
    class ModelSchema(SQLAlchemyAutoSchema):
        """Model storage schema"""
        class Meta:
            model = Model
            load_instance = True
    
  3. Create a DatabaseStorage instance. Set the primary_key keyword argument (defaults to 'slug') to the primary key of the model.

    """___main___.py"""
    from package import models
    from package.schema import ModelSchema
    storage = DatabaseStorage(
        session, models.Model, ModelSchema, primary_key="attr",)
    
  4. Use the DatabaseStorage instance.

    # __main__.py continued...
    
    storage.get(1)  # Gets a record from the database
    storage.put(2, {'attr2': 1, 'attr3': 'test'})  # Puts a record to the database
    storage.patch(1, {'attr2': 42})  # Update a record in the database
    storage.delete(1)  # Delete a record from the database
    storage.index()  # Get an index of records from the database
    
  5. Commit changes to the database.

    session.commit()
    

License

MIT License. See LICENSE file for more details.

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

alchemical_storage-1.0.0.dev0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

alchemical_storage-1.0.0.dev0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file alchemical_storage-1.0.0.dev0.tar.gz.

File metadata

File hashes

Hashes for alchemical_storage-1.0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 dc225fbed8c4064bd90ebd989a832309eeb252585912f0a8229d9d443a8e998c
MD5 c729a2ed64d8d3d1913cbc4113f217be
BLAKE2b-256 f5394a6997cfc25e95629bd987e48f2a3f79bd6e1953239b74411df948f8f40e

See more details on using hashes here.

File details

Details for the file alchemical_storage-1.0.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for alchemical_storage-1.0.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 be4c4cfe28c0885f1afc5d11aaed8bbd06614d7199e6d92c76565d2ee626bfe1
MD5 4145d39c4d06004afc09a5f2bb9f5a11
BLAKE2b-256 91c08e22e079e2e9e3c584729fbb0fbc063e24f784cc6c5f7208817cb9daae3c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page