💫 Mongoose Extended Reference and Autopopulate
Extended Reference
The extended reference builder provides a plugin for managing stale data updating as well as a builder for assembling extended references. It auto-syncs the target props on save, so you don't have to worry about matching types, setting projects and more.
const mongoose = require('mongoose');
const { ExtendedReference } = require('mongoose-field-populate');
const ReferenceSchema = new mongoose.Schema({
name: String,
age: Number,
});
ReferenceSchema.plugin(ExtendedReference.plugin, [
'TARGETS',
]);
module.exports = mongoose.model('DEMO_ONLY', ReferenceSchema);
const mongoose = require('mongoose');
const { ExtendedReference } = require('mongoose-field-populate');
const ReferenceModel = require('./reference');
const TargetSchema = new mongoose.Schema({
friend: new ExtendedReference(ReferenceModel)
.on(['name', 'age'])
.set('name', { private: true })
.done(),
});
module.exports = mongoose.model('TARGETS', TargetSchema);
Autopopulate
This package also ships with a very basic autopopulation feature. The most common solution on NPM does not suit my needs, though it might fit nicely into your project. Use this only if you require autopopulation on discriminators or embedded arrays.
const mongoose = require('mongoose');
const { autopopulate } = require('mongoose-field-populate');
const TargetSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
autopopulate: true,
autopopulateSelect: 'projection path',
}
});
TargetSchema.plugin(autopopulate)