PyConES24/src/retailtwin/views.py
2023-09-18 09:43:10 +02:00

37 lines
1.2 KiB
Python

"""
This module contains a set of functions that provide SQLAlchemy table interfaces
to existing views in the database. Make sure these views have been synced after
table creation.
"""
from retailtwin.models import Base
from functools import lru_cache
from sqlalchemy import Table, Column, Date, Integer, String, JSON
# Decorate with a cache to prevent sqlalchemy to create the table definition
# after multiple function calls. This is not an issue in production because
# a whole new isntance will be created by the worker generated by gunicorn,
# but this prevents errors during development. This is a way to create a
# pythonic singleton.
@lru_cache
def get_inventory_view() -> Table:
return Table(
"inventory",
Base.metadata,
Column("upc", Integer),
Column("batch", Integer),
Column("name", String),
Column("package", String),
Column("received", Date),
Column("best_until", Date),
Column("quantity", Integer),
Column("location", Integer),
Column("discount_definition", JSON),
)
@lru_cache
def get_stores_view() -> Table:
return Table("stores", Base.metadata, Column("id", Integer), Column("name", String))