Skip to content

no way to prefix query DateTime #150

@numberoverzero

Description

@numberoverzero
from bloop import BaseModel, DateTime, String, Column, Engine

class MyModel(BaseModel):
    id = Column(String, hash_key=True)
    date = Column(DateTime, range_key=True)

e = Engine()
e.bind(MyModel, skip_table_setup=True)

condition_hk = MyModel.id == "foo"
condition_rk = MyModel.date.begins_with("2021-11")
condition = condition_hk & condition_rk

# this line fails:
# AttributeError: 'str' object has no attribute 'in_timezone'
q = e.query(MyModel, condition)

This is because DateTime assumes only a datetime.datetime instance will be passed through dynamo_dump but for the begins_with condition we'll often pass a string prefix of the serialized datetime object. For example, filtering on a datetime range key to a single month.

One possible solution is to pass an additional field(s) through the context. Some rough first drafts:

rendering query condition
{
    "engine": Engine,
    "some key tbd": "engine.query",
    "condition": <BeginsWithCondition object>,
    "query": <Query object>,
}

rendering transactional read key
{
    "engine": Engine,
    "some key tbd": "tx.read",
    "tx": <Transaction object>,
    "tx_item": <TxItem object>,
}

rendering transactional write condition
{
    "engine": Engine,
    "some key tbd": "tx.write",
    "tx": <Transaction object>,
    "tx_item": <TxItem object>,
}

rendering keys for Engine.load
{
    "engine": Engine,
    "some key tbd": "engine.load",
    "obj": <object being loaded into>,
}

Today, only "engine" is required. The additional fields would vary based on what high-level operation is happening, and there's some unpacking we should do for ease of use (eg. point at each item in the AndCondition instead of the AndCondition itself).

To find all call sites, look for Type._dump or Type._load. Specifically look for calls to:

Metadata

Metadata

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions