Plugin for management of dynamic references
MIT License
Mongoose "plugin" for management of dynamic references.
npm install --save mongoose-reference
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Reference = require("mongoose-reference");
let YourSchema = new Schema({
title: String,
description: String,
author: String,
...Reference({ models: ["Company", "Group"], multiple: true })
});
After implementing mongoose-reference
extends your Schema with reference
or references
.
E. g.
// Example above after beeing extended
let YourSchema = new Schema({
title: String,
description: String,
author: String,
references: [
{
ref: {
type: Schema.Types.ObjectId,
required: true,
refPath: "references.onModel"
},
onModel: {
type: String,
required: true,
enum: ["Company", "Group"]
}
}
]
});
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
Reference({
models: [...],
key: 'CustomKey',
multiple: true
})
The MIT License Copyright (c) Carsten Jacobsen