node.js - 猫鼬同时创建孩子和关联的父母
问题描述
所以基本上,在我的应用程序中,我有一个员工和一个公司模型。这只是关于这些模型的基本信息,实际上还有更多信息,所以使用嵌套对象而不是 2 个模式似乎不是一个好的选择(我认为)
var EmployeeSchema = new Schema(
{
name: { type: String, required: true, max: 100 },
company: { type: Schema.Types.ObjectId, ref: 'Company', required: true },
}
);
var CompanySchema = new Schema(
{
name: { type: String, required: true },
},
{
toJSON: { virtuals: true },
},
);
CompanySchema.virtual('employees', {
ref: 'Employee',
localField: '_id',
foreignField: 'company',
justOne: false,
});
在创建新员工的表单上,我想要选择公司或创建新公司。
所以我的 API 将发送如下信息:
employee: {
name: 'John Bastien',
company: 5d44635fa5993c0424da8e07
}
或者:
employee: {
name: 'Dan Smith',
company: {
name: 'ACME'
}
}
这当然可以改变,这正是我的想法。
因此,在我的快递应用程序中,当我这样做时,我var employee = await new Employee(req.body.employee).save();
怎样才能使公司与员工一起创建。发送对象 ID 时它工作正常,但我怎样才能只使用关联文档的 JSON 对象呢?
解决方案
我最终在我的模型上编写了一些中间件来处理这个问题。可以提取此逻辑以使其更通用,但对于我的用例,它还不需要。
EmployeeSchema.virtual('company', {
ref: 'Company',
localField: 'companyId',
foreignField: '_id',
justOne: true,
}).set(function(company) {
this.companyId= company._id;
this.$company = company;
return this.$company;
});
EmployeeSchema.pre('validate', function(next) {
if (this.company && this.company instanceof Company) {
var err = this.company.validateSync();
if (err) {
// mergeErrors is a helper function that will merge the two exceptions into a nice format
err = mergeErrors(this.validateSync(), { company: err });
}
next(err);
}
next();
});
EmployeeSchema.pre('save', async function(next, saveOpts) {
if (this.company && this.company instanceof Company && this.company.isModified()) {
await this.company.save(saveOpts);
}
next();
});
推荐阅读
- http - 从 Jmeter 中的 xml 文件获取链接以发出 http 请求
- jquery - 更改 jQuery Mobile 页脚按钮的高度
- ios - 在 IB 中给 VC 分配一个类会导致黑屏
- c# - 我们如何知道 struct 是在 CPU 缓存上还是丢失到内存缓存中?
- angularjs - 如何将 ngForce 添加到我的 Angular 应用程序
- c# - 如何在 SQLite 中存储布尔值
- speech-recognition - Sphinxtrain 返回除 pocketsphinx 以外的其他结果
- airflow - 气流图视图未更新
- selenium - 可以根据本地主机地址运行 docker 映像吗?
- python - wxpython CallAfter 不改变 GUI