Ability to generate related models as well, by seeing the ref?
Closed this issue · 1 comments
shahidcodes commented
How to generate the related models along with main models together or separately?
Example:
/* eslint-disable prefer-destructuring */
const mongoose = require('mongoose');
const activitySchema = new mongoose.Schema({
name: String,
value: String,
});
mongoose.model('activity', activitySchema)
const schema = new mongoose.Schema(
{
mobile: String,
email: String,
name: {
type: String,
},
password: String,
status: {
type: String,
enum: ['active', 'inactive'],
default: 'active',
},
role: {
type: String,
enum: [
'staff',
'admin'
],
},
activities: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'activity'
}],
pushNotificationTokens: [String],
},
);
module.exports = mongoose.model('users', schema);
faboulaws commented
You can use the 2 options to achieve this
-
options.\<propertyName\>.populateWithSchema
: Uses a schema to generate mocks of related models. -
options.\<propertyName\>.populateWithFactory
: Uses another factory to generate mocks of related models. This options provides more flexibility thanpopulateWithSchema
, taking advantage of custom behaviors of factory via Factory options.
Example: options.populateWithFactory
const activityFactory = mocker(activitySchema)
const personFactory = mocker(personSchema, {
activities: {
populateWithFactory: activityFactory
}
})
const mock = personFactory.generate()
Example: options.populateWithSchema
const myFactory = mocker(schema, {
activities: {
populateWithSchema: activitySchema
}
})
const mock = myFactory.generate()