1.0.15 • Published 7 years ago

mongoose-populate-helper v1.0.15

Weekly downloads
17
License
ISC
Repository
github
Last release
7 years ago

mongoose-populate-helper

A mongoose plugin to facilitate population and sorting among others when referencing is used.


How to use

Note: targetModel changed to targetSchema in v1.0.9.

    articleSchema.plugin(mongoosePopulateHelper, configurations: Array<Object> || configuration: Object);

    configuration: {
        'sourceField': 
            '<Field that will be used in targetSchema to set targetField>',

        'targetField': 
            '<New field added to schema>',

        'map': 
            "<Function to map sourceField's value before assigning to targetField>",

        'targetSchema'?: 
            //default = schema using the plugin
            `<
            Schema from which value is taken (for type = 'local') 
            or 
            Schema to which value is assigned (for type = 'foreign')
            >`,

        'referenceField'?: 
            '<Used to get the document whose targetField will be updated>'
    }

    //'referenceField' and 'targetSchema' used => type = 'foreign'; ELSE, type = 'local'

Type = 'local'

  • Assignment is done in Schema using the plugin.

  • Retrieval is done from targetSchema.

E.g.:

Alt text

Type = 'foreign'

  • Assignment is done in targetSchema using referenceField.

  • Retrieval is done from Schema using the plugin.

E.g.:

Alt text

Example (local):

Alt text

Example (foreign):

Alt text )


Result

Alt text


Limitations

  • Path implemented but not yet tested (except for configuration.targetField)

EDIT: Using path works (tested).


Notes

  • Tested on MongoDB 3.2, Mongoose 4.5.9, NodeJS 8.11.1, NPM 5.6.0
1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago