An Object Relation Mapper for Autodesk Flow Production Tracker (previously Shotgrid and Shotgun) inspired by SQLAlchemy.
You can declare all the entities and fields using a dataclass like structure:
from __future__ import annotations
from sgchemist.orm import SgBaseEntity
from sgchemist.orm import TextField
from sgchemist.orm import EntityField
from sgchemist.orm import MultiEntityField
class SgEntity(SgBaseEntity):
"""Base class for all the entities."""
class Project(SgEntity):
__sg_type__ = "Project"
name: TextField = TextField(name="code")
title: TextField
assets: MultiEntityField[Asset]
class Asset(SgEntity):
__sg_type__ = "Asset"
name: TextField = TextField(name="code")
description: TextField
project: EntityField[Project]To make a query using sgchemist, you need to use two elements:
- an engine: responsible for communicating with your Shotgrid instance.
sgchemistprovides an engine implementation using theshotgun-api3. - and a session: responsible for converting raw data from the engine back to objects. In case of creation and update querying it also implements the unit of work pattern.
from shotgun_api3 import Shotgun
from sgchemist.orm import ShotgunAPIEngine
from sgchemist.orm import select
from sgchemist.orm import Session
from myentities import Asset, Project
# Create the engine
shotgun = Shotgun("https://mysite.shotgunstudio.com", script_name="xyz", api_key="abc")
engine = ShotgunAPIEngine(shotgun)
# Create the session
session = Session(engine)
# Create the query
query = select(Asset).where(Asset.project.f(Project.name).eq("myproject"))
# Perform the query using the session
assets = list(session.exec(query))
# Update the description of the assets
with session:
for asset in assets:
asset.description = "This is an awesome asset"
session.add(asset)
# Assets are now updated