Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3013

Deserializing to custom targets (classes)

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      https://github.com/mongodb/js-bson/pull/253

      This pull request tries to cover this feature request https://github.com/mongodb/js-bson/issues/211

      I thought of two options for nested targeting:

      1- one (which current code is based on) is using static props of the top target, for example consider following document in mongodb:

      {
          _id: 1,
          comments: [
                {_id: 1, content: ""},
                {_id: 2, content: ""}
          ]
      }
      

      Assigning the top document to an Article object:

      class Article {}
      articles.find({}, {target: Article});
      

      Assigning articles to an extended Array:

      class Article {
           static $comments = class Comments extends Array {}
      } 
      articles.find({}, {target: Article});
      

      assigning articles items to a Comment class:

      class Article {
           static $comments = class Comments extends Array {
                  static $ = class Comment {}
           }
      } 
      articles.find({}, {target: Article});
      

      I used $ at the begging of field names to prevent confusion with Class self properties, and used $ for array items.

      2- Using a custom data structure like this:
      {{articles.find({}, {targets: {root: Article, Comments:

      {root: Comments, item: Comment}

      }});}}

      I believe first one is nicer but it's more confusing and the whole hierarchy of data must be defines.
      If the maintainers accepts the general idea then it can be discussed.

      For now I used firstBatch and nextBatch to detect documents parsing, until someone suggest a better approach.

      if you want to test this code please use my modified mongodb core driver (I've added necessary code to commands.js).

            Assignee:
            Unassigned Unassigned
            Reporter:
            alexander.golin@mongodb.com Alexander Golin (Inactive)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: