Expose Mapping of Python Built-in Types to BSON Type Strings

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Major - P3
    • 4.18.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None

      Hi,

      I'm building an application in which application logic is defined in python, and I'd like to generate a MongoDB schemata based on the schemata of my application objects. I've manually written out my BSON schemata in python, but I'd like to reduce code redundancy and automatically generate such schemata based on the definitions of my objects.

      At present, here are some example schemata and indices I have defined:

      lexeme_index = {
          'keys': [("lemma", pymongo.ASCENDING), ("pos", pymongo.ASCENDING)],
          'name': "lemma index",
          'unique': True
      }
      
      lexeme_schema = {"$jsonSchema": {
          "bsonType": "object",
          "required": ["_id", "lemma", "pos", "definitions"],
          "properties": {
              "_id": {
                  "bsonType": "objectId",
              },
              "lemma": {
                  "bsonType": "string",
              },
              "pos": {
                  "bsonType": "string",
              },
              "definitions": {
                  "bsonType": "array"
              }
          }
      }}
      

      Here is an example of the data class being reflected:

      from dataclasses import dataclass
      from enforce_typing import enforce_types
      # ...
      
      @enforce_types
      @dataclass
      class Lexeme(JSONSerializable):
          """
          A representation of a basic word of a language from which ideas are derived.
      
          Attributes:
              lemma (str): The most basic form of the word.
              pos (PartOfSpeech): The part of speech of the word.
              definitions (list): the definitions of the trm.
          """
          lemma: str
          pos: Union[PartOfSpeech, str]
          definitions: list
          
          def __post_init__(self):
          # ...
      

      I'd like to pass my python dataclass into some function which will generate a schemata for me, which I can then send to MongoDB for enforcement:

      lexeme_bson_properties = get_bson_properties(Lexeme)
      

       

      Ideally, such a function would be provided to me, but I don't know how extensively python data classes are used, so I'm not sure how much demand there would be for that. With that, I plan to make a function that would generate such schemata. At this moment, I plan to generate such a mapping using this python type to bson type mapping, but I would hope such a mapping would be provided through the bson library out of the box. Here's what I would expect the mapping to look like:

      from bson.SOME_PACKAGE import BUILT_IN_TYPE_MAPPING
      
      for type in (int, str, list, dict):
          print(BUILT_IN_TYPE_MAPPING[type])
      
      # int32
      # string
      # array
      # object
      

            Assignee:
            Unassigned
            Reporter:
            Alexander Bieniek
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: