2

我目前正在运行代码:

export class SystemInformationContent {
  createdAt: number;

  createdBy: User | mongoose.Schema.Types.ObjectId | null;

  updatedAt?: number;

  updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}

@Schema()
export class SystemInformation {
  @Prop(
    raw({
      createdAt: { type: Number, required: true },
      createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
      updatedAt: { type: Number, default: 0 },
      updatedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        default: null,
      },
    }),
  )
  system: SystemInformationContent;
}

我没有找到任何“扩展”架构的方法SystemInformationContent,因此raw()在装饰器中使用了该函数@Prop(),但我想知道是否有办法做这样的事情:

export class SystemInformationContent {
  @Prop({ required: true })
  createdAt: number;

  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
  createdBy: User | mongoose.Schema.Types.ObjectId | null;

  @Prop({ default: 0 })
  updatedAt?: number;

  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null })
  updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}

@Schema()
export class SystemInformation {
  @Prop(???)
  system: SystemInformationContent;
}

SystemInformation.system @Prop()考虑到SystemInformationContent.

你们知道是否有其他方法,raw或者我是否遗漏了什么?

编辑:我的 NestJS 应用程序的所有类都在扩展 SystemInformation,所以它们看起来都像:

{
  ...,
  system: {
    createdAt: 1616778310610,
    createdBy: "605e14469d860eb1f0641cad",
    editedAt: 0,
    createdBy: null,
  },
}
4

3 回答 3

2

找到了解决方案!

我编辑SystemInformationContent为:

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { User } from '~/schemas/user.schema';

@Schema({ _id: false })
export class SystemInformationContent {
  @Prop({ type: Number, required: true })
  createdAt: number;

  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
  createdBy: User;

  @Prop({ type: Number, default: 0 })
  updatedAt?: number;

  @Prop({
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    default: null,
  })
  updatedBy?: User;
}

export const SystemInformationContentSchema = SchemaFactory.createForClass(
  SystemInformationContent,
);

然后在 SystemInformation 我编辑为:

import { Prop, Schema } from '@nestjs/mongoose';
import {
  SystemInformationContent,
  SystemInformationContentSchema,
} from '~/schemas/systemInformationContent.schema';

@Schema()
export default class SystemInformation {
  @Prop({ required: true, type: SystemInformationContentSchema })
  system: SystemInformationContent;
}

现在一切正常,我使用@Schema({ _id: false })来删除由 SchemaFactory 生成的 ID,所以我最终在 DB 中使用了它:

{
  ...,
  "system": {
    "updatedBy": null,
    "updatedAt": 0,
    "createdAt": 1616847116986,
    "createdBy": {
      "$oid": "605f210cc9fe3bcbdf01c95d"
    }
  },
  ...,
}
于 2021-03-27T12:20:37.700 回答
0

如果我做对了,这就是你想要的。您只是错过了@Schema()注释,SystemInformationContent并且需要在您想要的任何地方继承此架构。我有一个类似的问题。所以,如果这不是你要求的,请告诉我。

@Schema()
export class SystemInformationContent {
  // put your @Prop() attributes here, that need to be available in every other schema
}

@Schema()
export class SystemInformation extends SystemInformationContent {
  // no need to do anything else here
  // the attributes are inherited from SystemInformationContent 
}
于 2021-03-27T10:25:38.487 回答
0

如果您正在使用(7.0 或更高版本),那么如果您在 tsconfig 中启用了typegoose,那么您目前拥有的应该足够了emitDecoratorMetadata

带有 typegoose 的示例(下面的代码使用 ~7.4 中的选项扩展):

export class SystemInformationContent {
  @Prop({ required: true })
  createdAt: number;

  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User })
  createdBy: Ref<User>;

  @Prop({ default: 0 })
  updatedAt?: number;

  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User }) // default is "undefined"
  updatedBy?: Ref<User>;
}

export class SystemInformation {
  @Prop() // thanks to "emitDecoratorMetadata" no explicit types are needed 
  system: SystemInformationContent;

  // but if wanting to do explicit types
  @Prop({ type: () => SystemInformationContent })
  system: SystemInformationContent;
}

PS:我不知道你的函数raw来自哪里,但这不是 typegoose 函数

于 2021-03-24T12:21:15.443 回答