Add all_pks() method to HashModel

This commit is contained in:
Andrew Brookins 2021-11-08 16:28:58 -08:00
parent db7b8d19ad
commit bc441143de
3 changed files with 19 additions and 1 deletions

View file

@ -1014,6 +1014,7 @@ class ModelMeta(ModelMetaclass):
new_class._meta.primary_key_creator_cls = getattr(
base_meta, "primary_key_creator_cls", UlidPrimaryKey
)
# TODO: Configurable key separate, defaults to ":"
if not getattr(new_class._meta, "index_name", None):
new_class._meta.index_name = (
f"{new_class._meta.global_key_prefix}:"
@ -1203,6 +1204,17 @@ class HashModel(RedisModel, abc.ABC):
db.hset(self.key(), mapping=document)
return self
@classmethod
def all_pks(cls):
key_prefix = cls.make_key(cls._meta.primary_key_pattern.format(pk=""))
# TODO: We assume the key ends with the default separator, ":" -- when
# we make the separator configurable, we need to update this as well.
# ... And probably lots of other places ...
return (
key.split(":")[-1]
for key in cls.db().scan_iter(f"{key_prefix}*", _type="HASH")
)
@classmethod
def get(cls, pk: Any) -> "HashModel":
document = cls.db().hgetall(cls.make_primary_key(pk))