1.0.15 • Published 6 years ago

mongoose-populate-helper v1.0.15

Weekly downloads
17
License
ISC
Repository
github
Last release
6 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

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago