mongoose-reference
Mongoose "plugin" for management of dynamic references.
Installation
npm install --save mongoose-reference
Usage
const mongoose = ;const Schema = mongooseSchema;const Reference = ; let YourSchema = title: String description: String author: String ...;
After implementing mongoose-reference
extends your Schema with reference
or references
.
E. g.
// Example above after beeing extendedlet YourSchema = title: String description: String author: String references: ref: type: SchemaTypesObjectId required: true refPath: "references.onModel" onModel: type: String required: true enum: "Company" "Group" ;
Options
Key | Type | Description |
---|---|---|
models | Array | Define all allowed reference types (enum). Must be the same as the name of the registered Schemas |
key | String | Define your custom key. (Default: reference or references ) |
multiple | Boolean | Set to true if you want to have multiple references |
modelKey | String | Define your custom key for model value. (Default: onModel ) |
referenceKey | String | Define your custom key for reference value. (Default: ref ) |
Important! Mongoose has some reserved keys that will throw an error if used. See list here: Reserved keys
Example
License
The MIT License Copyright (c) Carsten Jacobsen