schema嵌入式模型的选项(typegoose)



我在猫鼬中通常做的是:

import { Schema, model } from 'mongoose';
const SubCategorySchema = new Schema({
value: {
type: String
}
})
const CategorySchema = new Schema({
value: {
type: String,
required: true
},
subCategories: [SubCategorySchema]
});
SubCategorySchema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: (doc, ret, options) =>
{
delete ret._id;
return ret;
}
})
CategorySchema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: (doc, ret, options) =>
{
delete ret._id;
return ret;
}
});
export const Category = model('Category', CategorySchema);

当数据通过快递进入我的网络应用程序时。应用程序打印CategorySchemaSubCategorySchemaid而不是_id,这正是我想要的。然而,我似乎无法在typegoose上复制这一点。我只能通过以下操作为Category做到这一点:

import { Typegoose, prop, arrayProp } from 'typegoose';
import { ICategory, ISubCategory } from './category.interface';
export class SubCategory implements ISubCategory
{
readonly id: string;
@prop({ required: true })
public value: string;
}
export class Category extends Typegoose implements ICategory
{
readonly id: string;
@prop({ required: true })
public value: string;
@arrayProp({ items: SubCategory })
public subCategories?: SubCategory[];
}
export const CategoryContext = new Category().getModelForClass(Category, {
schemaOptions: {
toJSON: {
virtuals: true,
versionKey: false,
transform: (doc, ret, options) => {
delete ret._id;
return ret;
}
}
}
});

我甚至试过做:

  • new SubCategory().getModelForClass(SubCategory, {...})
  • new SubCategory().setModelForClass(SubCategory, {...})

但无济于事。


对于第一个例子,我会得到这样的结果:

[
{
id: 'asdjuo1j2091230',
value: 'A Category',
subCategories: [
{
id: 'asdl;ka;lskdjas',
value: 'A SubCategory'
}
]
}
]

对于第二个例子,我会得到这样的结果:

[
{
id: 'asdjuo1j2091230',
value: 'A Category',
subCategories: [
{
_id: 'asdl;ka;lskdjas', //<----- want it to be id, but it's displaying as _id
value: 'A SubCategory'
}
]
}
]

这个功能是没有实现还是我在文档中遗漏了它?对此还有什么其他选择?

用于可用的修复程序,特别是用于Typegoose v.10.0Mongoose v.6.8用户。例如,它可能类似于这个。

在模型目录中。

import * as mongoose from 'mongoose'
import { prop, getModelForClass, modelOptions } from '@typegoose/typegoose'
import { Base } from '@typegoose/typegoose/lib/defaultClasses'
// extend Base in order to define _id and id
export interface ICommon extends Base {
value: string
}
// delete or rename _id to id in modelOptions using virtual property
@modelOptions({
schemaOptions: {
timestamps: true,
versionKey: false,
toJSON: {
virtuals: true,
transform: (_doc, ret) => {
ret.id = ret._id.toString()
delete ret._id
},
},
},
})
export class SubCategory implements ICommon {
_id!: mongoose.Types.ObjectId
id!: string

@prop({ required: true })
public value!: string

//...

}
export class Category implements ICommon {
_id!: mongoose.Types.ObjectId
id!: string

@prop({ required: true })
public value!: string

@prop() 
public subCategories?: [SubCategory] // if an array of subdocuments is preferred

// or an array of references if you'd rather just pick one format
@prop({ ref: () => SubCategory }) 
public subCategories?: Ref<SubCategory>[]

// ...

}
export const SubCategoryModel = getModelForClass(SubCategory)
export const CategoryModel = getModelForClass(Category)

在控制器或服务目录的某个位置填充引用数据。

import { Request, Response } from 'express'
import { CategoryModel, Category } from '../models'
// ....
const allCategories = async (_req: Request, res: Response) => {
const categories: Category[] = await CategoryModel.find({}).populate('subCategories', { __v: 0 })
if (!categories) throw Error('Problem fetching category list!')
return res.status(200).json(categories)
}
// ....
export default {
allCategories,
// ....
}

结束。将函数插入路由并开始检查端点。现在,_id实际上已经不存在了,在服务器中或者如果您向客户端发出请求,则会被id取代,但当然,在DB中不会。

编码快乐!

最新更新