Gathering detailed insights and metrics for mongoose-polymer
Gathering detailed insights and metrics for mongoose-polymer
Gathering detailed insights and metrics for mongoose-polymer
Gathering detailed insights and metrics for mongoose-polymer
npm install mongoose-polymer
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
4 Stars
22 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Jan 20, 2025
Latest Version
0.1.0
Package Id
mongoose-polymer@0.1.0
Size
5.10 kB
NPM Version
2.7.4
Node Version
0.12.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Polymorphic associations for mongoose inspired by laravel polymorphic relations and others.
mongoose-polymer
and thus polymorphic associations, allow a model to belong to more than one other model on a single association. For example, you might have a photo model that belongs to either a user model or an product model.
1$ npm install --save mongoose-polymer
Require mongoose-polymer
after mongoose
prior to your schema definition. This allows mongoose-polymer
to patch Schema
and add all required polymorphism boilerplates.
1var mongoose = require('mongoose'); 2var polymer = require('mongoose-polymer');
To define polymorphic one-to-one
with mongoose-polymer
, use morphBy schema method on the owned model side and morphOne schema method on the owning model side. Consider a case where a user schema
and product schema
each having a single photo
. Here user schema
and product schema
form the owning side(or parent) and photo schema
is the owned side(or child).
Example
1//photo schema defenition 2var PhotoSchema = new Schema({ 3 ... 4}); 5PhotoSchema.morphBy('User','photoable'); 6PhotoSchema.morphBy('Product','photoable'); 7var Photo = mongoose.model('Photo',PhotoSchema); 8 9//user schema definition 10var UserSchema = new Schema({ 11 ... 12}); 13UserSchema.morphOne('Photo','photoable'); 14var User = mongoose.model('User',UserSchema); 15 16//product schema definition 17var ProductSchema = new Schema({ 18 ... 19}); 20PhotoSchema.morphOne('Photo','photoable'); 21var Product = mongoose.model('Product',ProductSchema); 22 23//samples 24//adding user photo 25user 26 .setPhoto({name:'user photo'}) 27 .exec(function(error,photo){ 28 ... 29 }); 30 31//get user photo 32user 33 .getPhoto(function(error,photo){ 34 ... 35 }); 36 37//removing user photo 38user 39 .removePhoto(function(error,photo){ 40 ... 41 }); 42 43//get photo user 44photo 45 .photoable(function(error,user){ 46 ... 47 });
To define polymorphic one-to-many
with mongoose-polymer
, use morphBy schema method on the owned model side and morphMany schema method method in the owning model side. Consider a case where a user schema
and product schema
each having multiple photo
. Here user schema
and product schema
form the owning side(or parent) and photo schema
is the owned side(or child).
Example
1//photo schema definition 2var PhotoSchema = new Schema({ 3 ... 4}); 5PhotoSchema.morphBy('User','photoable'); 6PhotoSchema.morphBy('Product','photoable'); 7var Photo = mongoose.model('Photo',PhotoSchema); 8 9//user schema definition 10var UserSchema = new Schema({ 11 ... 12}); 13UserSchema.morphMany('Photo','photoable'); 14var User = mongoose.model('User',UserSchema); 15 16//product schema definition 17var ProductSchema = new Schema({ 18 ... 19}); 20PhotoSchema.morphMany('Photo','photoable'); 21var Product = mongoose.model('Product',ProductSchema); 22 23//samples 24//add user photo 25user 26 .addPhoto({name:'user photo'}) 27 .exec(function(error,photo){ 28 ... 29 }); 30 31//add user photos 32user 33 .addPhoto([{name:'first photo'},{name:'second phot'}]) 34 .exec(function(error,photos){ 35 ... 36 }); 37 38//get all user photos 39user 40 .getPhotos() 41 .exec(function(error,photos){ 42 ... 43 }); 44 45//get one user photo 46user 47 .getPhoto(photoId) 48 .exec(function(error,photo){ 49 ... 50 }); 51 52//remove all user photos 53user 54 .removePhotos() 55 .exec(function(error,photos){ 56 ... 57 }); 58 59//remove one user photo 60user 61 .removePhoto(photoId) 62 exec(function(error,photo){ 63 ... 64 });
morphBy(modelName,morphName)
Specifies the owning model of polymorphism. In case of Product
and Photo
the owning model is Product
. modelName
is valid model name of the owning side and morpName
is the name of polymorphic association formed. morpName
controls the name of fields used to store the formed association.
Example
1PhotoSchema.morphBy('Product','photoable'); 2...
Following model instance methods will be added to the owned model instance
morphName(callback)
An instance method whose name is determines by morphName
will be added to owned side model instance to enable retrieving owning side model instance. For the above cases Photo
model instance will gain photoable
instance method to enable it to retrieve either Product
or User
instance.
Example
1//when want to retrieve user from photo instance 2photo 3 .photoable(function(error,user){ 4 ... 5 }); 6 7//when want to retrieve product from photo instance 8photo 9 .photoable(function(error,product){ 10 ... 11 });
morpOne(modelName,morphName)
Specifies the owned model in one-to-one polymorphism. In case of Product
and Photo
the owned model is Photo
. modelName
is valid model name of the owned side and morpName
is the name of polymorphic association formed. morpName
controls mongoose criteria building in the owning side.
Example
1//one-to-one 2PhotoSchema.morphOne('Photo','photoable'); 3...
The following model instance methods will be added on owning model instance
getModelName(callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to retrieve its polymer. For the case of the above examples User
and Product
model instance will have instance method with name getPhoto
to enable them to get their photo. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when retrieving photo instance from the user instance 2user 3 .getPhoto() 4 //more conditions 5 .exec(function(error,photo){ 6 ... 7 });
setModelName(morphOne,callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to set its polymer. For the case of the above examples User
and Product
model instance will have instance method with name setPhoto
to enable them to get their photo. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when set photo instance for the user instance 2user 3 .setPhoto({name:'mongoose-polymer'}) 4 .exec(function(error,photo){ 5 ... 6 });
removeModelName(callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to remove its polymer. For the case of the above examples User
and Product
model instance will have instance method with name removePhoto
to enable them to remove their photo. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when removing photo instance from the user instance 2user 3 .removePhoto() 4 //more conditions 5 .exec(function(error,photo){ 6 ... 7 });
morphMany(modelName,morphName)
Specifies the owned model in one-to-many polymorphism. In case of Product
and Photo
the owned model is Photo
. modelName
is valid model name of the owned side and morpName
is the name of polymorphic association formed. morpName
controls mongoose criteria building in the owning side.
Example
1//one-to-many
2PhotoSchema.morphMany('Photo','photoable');
3...
The following model instance methods will be added to the owning model instance
getModelName(id,callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to get one of its polymer. For the case of the above examples User
and Product
model instance will have instance method with name getPhoto
to enable them to retrieve one of their photo. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when retrieving one photo instance from the user instance 2user 3 .getPhoto(_id) 4 //more conditions 5 .exec(function(error,photo){ 6 ... 7 });
getModelNames(callback)
Additional instance method whose name is determined by pluralizing the name of the owned model, will be added to the owning model to enable it to get all of its polymer. For the case of the above examples User
and Product
model instance will have instance method with name getPhotos
to enable them to retrieve all their photos. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when retrieving all photos instance from the user instance 2user 3 .getPhotos() 4 //more conditions 5 .exec(function(error,photos){ 6 ... 7 });
addModelName(morpOne,callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to add one or more polymer. For the case of the above examples User
and Product
model instance will have instance method with name addPhoto
to enable them to add one or more photo. If callback
is not supplied mongoose promise
will be returned for evaluation.
Example
1//when adding one photo instance from the user instance 2user 3 .addPhoto({name:'photo name'}) 4 .then(function(error,photo){ 5 ... 6 }); 7 8//when adding multiple photos instance from the user instance 9user 10 .addPhoto([{name:'photo name'},{name:'another photo'}]) 11 .then(function(error,photo){ 12 ... 13 });
removeModelName(id,callback)
Additional instance method whose name is determined by the name of the owned model, will be added to the owning model to enable it to remove one of its polymer. For the case of the above examples User
and Product
model instance will have instance method with name removePhoto
to enable them to remove one of their photo. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when removing one of the photo instance from the user instance 2user 3 .removePhoto(_id) 4 //more conditions 5 .exec(function(error,photo){ 6 ... 7 });
removeModelNames(callback)
Additional instance method whose name is determined by pluralizing the name of the owned model, will be added to the owning model to enable it to remove its polymers. For the case of the above examples User
and Product
model instance will have instance method with name removePhotos
to enable them to remove their photos. If callback
is not supplied valid mongoose query
will be returned to allow additional chaining.
Example
1//when removing photos instance from the user instance 2user 3 .removePhotos() 4 //more conditions 5 .exec(function(error,photo){ 6 ... 7 });
Clone this repository
Install all development dependencies
1$ npm install
1$ npm test
It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
The MIT License (MIT)
Copyright (c) 2015 lykmapipo & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 0/22 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More