Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4816

Inheritance: Allow override of discriminator values in _type field used to denote subclass

    • Type: Icon: Epic Epic
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.2.0.rc1
    • Affects Version/s: 7.0.5
    • Component/s: Attributes
    • None
    • Done
    • Discriminator Value Override
    • Hide

      Engineer(s): Neil Shweky

      2020-07-24: Setting initial target date to 2020-08-07 (2 weeks)

      • Neil has picked this up as an additional intern project after completing his first and is slated to have it all wrapped up by the end of the internship on August 7.
      Show
      Engineer(s): Neil Shweky 2020-07-24: Setting initial target date to 2020-08-07 (2 weeks) Neil has picked this up as an additional intern project after completing his first and is slated to have it all wrapped up by the end of the internship on August 7.
    • Major Change

      We're using the Trailblazer framework with a fairly complicated data model that includes fairly deeply nested document structures.  We use inheritance on nested subclasses in some situations.

      We also use both Mongod (RoR) and Mongoose (NodeJS) to access our database.

      So we might have a parent class name such as:

      Customer:PreferredProducts::ProductSpecificDetails

      and a inheriting subclasses such as:

      Customer:PreferredProducts::BookSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails

      and

      Customer:PreferredProducts::CourseSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails

      The issue we have is that the _type field then has to have overly long values that match the subclass names:  "Customer:PreferredProducts::ProductSpecificDetails" and "Customer:PreferredProducts::CourseSpecificDetails".

      This is both cumbersome and undesired, especially since the _type field will be stored for every instance of the subclass in the database.   This makes database contents dependent on the app implementation technology. Having to include development stack specific _type values in our database is ugly (and offensive), especially since we use two different stacks, Mongoid/RoR and Mongoose/NodeJS as noted. If we change the class naming (which we have done) it necessitates a reload of the entire database and changes in the other stack (we use Mongoose primarily for migration of data from a legacy RDBMS).

      We need to be able to do the same in Mongoid.  In the example above, we would like to be able to specify that _type field values wold be "Book" and "Course" respectively for the two subclasses.

      In Mongoose we can easily specify a custom _type value override for each discriminator model (RoR subclass equivalent) to accomplish this.  We would like to see the same functionality available in Mongoid.

      A macro like this would do the trick:

      Customer:PreferredProducts::BookSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails
      override_inheritance_type_value  "Book"
      field :fld1, type: String, default: "N"
      field :fld2, type: Number
      etc....

      Failing that, if you could point me at the appropriate areas in the Mongoid code base that deal with instantiation subclasses based on the _type field value, we'll try and put together a monkey-patch to accomplish this and donate it back.

      Thanks!

       

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            ataramina@shorecg.com Andrzej Taramina
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: