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.
-
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]
-
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
-
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",)
-
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
-
Commit changes to the database.
session.commit()
License
MIT License. See LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file alchemical_storage-1.0.0.dev1.tar.gz
.
File metadata
- Download URL: alchemical_storage-1.0.0.dev1.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.8 Linux/6.2.0-32-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e7b3c7a45e7ffb2f0dee9c9ace60e68cfddcfb530151a5c5ca46ee7848ee076 |
|
MD5 | 2d1b5d126932603ef30612f0dc35dd12 |
|
BLAKE2b-256 | 05661ff790ba3673abd95e0a41977119ed829d9814faf72da4ba64e012866743 |
File details
Details for the file alchemical_storage-1.0.0.dev1-py3-none-any.whl
.
File metadata
- Download URL: alchemical_storage-1.0.0.dev1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.8 Linux/6.2.0-32-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f39ddc11825012b64594ef9ab19e39ab7456fe8245bff07e7cc1449476d65b7 |
|
MD5 | 7be28953c8d8c577954ba0f021342b76 |
|
BLAKE2b-256 | b32c94ef2c6408fd49b06c5b2659d47c04626c5b0c34e0f5342c21c4f6522f67 |