@@ -32,6 +32,7 @@ def default_conventions(cls):
32
32
return cls ()
33
33
34
34
__cached_default_type_collection_names : Dict [type , str ] = {}
35
+ __cached_keys_collection_names : Dict [str , str ] = {}
35
36
36
37
def __init__ (self ):
37
38
self ._frozen = False
@@ -65,6 +66,7 @@ def __init__(self):
65
66
self ._find_identity_property = lambda q : q .__name__ == "key"
66
67
self ._find_python_class : Optional [Callable [[str , Dict ], str ]] = None
67
68
self ._find_collection_name : Callable [[Type ], str ] = self .default_get_collection_name
69
+ self ._find_collection_name_for_dict : Callable [[str ], str ] = self .default_get_collection_name_for_dict
68
70
self ._find_python_class_name : Callable [[Type ], str ] = (
69
71
lambda object_type : f"{ object_type .__module__ } .{ object_type .__name__ } "
70
72
)
@@ -281,6 +283,13 @@ def get_collection_name(self, entity_or_type: Union[type, object]) -> str:
281
283
282
284
return self .default_get_collection_name (object_type )
283
285
286
+ def get_collection_name_for_dict (self , key : str ):
287
+ collection = key .split ("/" )[0 ]
288
+ collection_name = self ._find_collection_name_for_dict (collection )
289
+ if collection_name :
290
+ return collection_name
291
+ return self .default_get_collection_name (dict )
292
+
284
293
def generate_document_id (self , database_name : str , entity : object ) -> str :
285
294
object_type = type (entity )
286
295
for list_of_registered_id_convention in self ._list_of_registered_id_conventions :
@@ -305,6 +314,16 @@ def default_get_collection_name(object_type: type) -> str:
305
314
DocumentConventions .__cached_default_type_collection_names [object_type ] = result
306
315
return result
307
316
317
+ @staticmethod
318
+ def default_get_collection_name_for_dict (key : str ) -> str :
319
+ result = DocumentConventions .__cached_keys_collection_names .get (key , None )
320
+ if result :
321
+ return result
322
+ # singular_noun returns False if the word is singular
323
+ result = inflector .plural (key ) if not inflector .singular_noun (key ) else key
324
+ DocumentConventions .__cached_keys_collection_names [key ] = result
325
+ return result
326
+
308
327
@staticmethod
309
328
def try_get_type_from_metadata (metadata ):
310
329
if "Raven-Python-Type" in metadata :
0 commit comments