NestJS Mongoose模式继承

4
我试图在NestJS中继承Mongoose Schemas或SchemaDefitions,但是我没有太多的运气。
我这样做是为了共享基本和常见的模式定义细节,例如我们附加到每个实体的虚拟('id')和nonce。每个模式定义都应该有自己的Mongo集合,所以鉴别器不起作用。
我尝试通过以下不同的方式实现这一点。
首先,我定义了以下基本模式定义:
base.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { TimeStamps } from './timestamps.schema';

export type BaseDocument = BaseSchemaDefinition & Document;

@Schema({
  toJSON: {
    virtuals: true,
    transform: function (doc: any, ret: any) {
      delete ret._id;
      delete ret.__v;
      return ret;
    },
  },
})
export class BaseSchemaDefinition {
  @Prop({
    type: Types.ObjectId,
    required: true,
    default: Types.ObjectId,
  })
  nonce: Types.ObjectId;

  @Prop()
  timestamps: TimeStamps;
}

我随后继承架构定义并创建架构,以便在我的服务和控制器中稍后使用,方式如下:

person.schema.ts

import { Prop, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { Document } from 'mongoose';
import { Address } from './address.schema';
import { BaseSchemaDefinition } from './base.schema';

export type PersonDocument = PersonSchemaDefintion & Document;

export class PersonSchemaDefintion extends BaseSchemaDefinition {
  @Prop({ required: true })
  first_name: string;

  @Prop({ required: true })
  last_name: string;

  @Prop()
  middle_name: string;

  @Prop()
  data_of_birth: Date;

  @Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Address' }] })
  addresses: [Address];
}

const PersonSchema = SchemaFactory.createForClass(PersonSchemaDefintion);

PersonSchema.virtual('id').get(function (this: PersonDocument) {
  return this._id;
});

export { PersonSchema };

这导致我只能创建和获取在BaseSchemaDefinition中定义的属性。

{ "timestamps": { "deleted": null, "updated": "2021-09-21T16:55:17.094Z", "created": "2021-09-21T16:55:17.094Z" }, "_id": "614a0e75eb6cb52aa0ccd026", "nonce": "614a0e75eb6cb52aa0ccd028", "__v": 0 }

其次,我尝试通过使用此处描述的方法继承Mongoose模式(不同的MongoDB集合)来实现继承。

base.schema.ts

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { TimeStamps } from './timestamps.schema';

export type BaseDocument = BaseSchemaDefinition & Document;

@Schema({
  toJSON: {
    virtuals: true,
    transform: function (doc: any, ret: any) {
      delete ret._id;
      delete ret.__v;
      return ret;
    },
  },
})
export class BaseSchemaDefinition {
  @Prop({
    type: Types.ObjectId,
    required: true,
    default: Types.ObjectId,
  })
  nonce: Types.ObjectId;

  @Prop()
  timestamps: TimeStamps;
}

const BaseSchema = SchemaFactory.createForClass(BaseSchemaDefinition);

BaseSchema.virtual('id').get(function (this: BaseDocument) {
  return this._id;
});

export { BaseSchema };

person.schema.ts

import { Prop } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { Document } from 'mongoose';
import { Address } from './address.schema';
import { BaseSchema, BaseSchemaDefinition } from './base.schema';

export type PersonDocument = PersonSchemaDefintion & Document;

export class PersonSchemaDefintion extends BaseSchemaDefinition {
  @Prop({ required: true })
  first_name: string;

  @Prop({ required: true })
  last_name: string;

  @Prop()
  middle_name: string;

  @Prop()
  data_of_birth: Date;

  @Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Address' }] })
  addresses: [Address];
}

export const PersonSchema = Object.assign(
  {},
  BaseSchema.obj,
  PersonSchemaDefintion,
);

输出结果相同,但不确定为什么继承没有生效。

以下是使用模式构建模型的服务代码:

person.service.ts

import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import {
  PersonSchemaDefintion,
  PersonDocument,
} from 'src/schemas/person.schema';
import { TimeStamps } from 'src/schemas/timestamps.schema';

@Injectable()
export class PersonService {
  constructor(
    @InjectModel(PersonSchemaDefintion.name)
    private personModel: Model<PersonDocument>,
  ) {}

  async create(
    personModel: PersonSchemaDefintion,
  ): Promise<PersonSchemaDefintion> {
    personModel.timestamps = new TimeStamps();
    const createdPerson = new this.personModel(personModel);

    return createdPerson.save();
  }

  async update(
    id: string,
    changes: Partial<PersonSchemaDefintion>,
  ): Promise<PersonSchemaDefintion> {
    const existingPerson = this.personModel
      .findByIdAndUpdate(id, changes)
      .exec()
      .then(() => {
        return this.personModel.findById(id);
      });
    if (!existingPerson) {
      throw Error('Id does not exist');
    }
    return existingPerson;
  }

  async findAll(): Promise<PersonSchemaDefintion[]> {
    return this.personModel.find().exec();
  }

  async findOne(id: string): Promise<PersonSchemaDefintion> {
    return this.personModel.findById(id).exec();
  }

  async delete(id: string): Promise<string> {
    return this.personModel.deleteOne({ _id: id }).then(() => {
      return Promise.resolve(`${id} has been deleted`);
    });
  }
}

如有需要,我可以提供更多细节。

3个回答

4

我认为我遇到了相同的问题。

这是我的解决方案:

首先,您需要自定义@Schema装饰器。

schema.decorator.ts

import * as mongoose from 'mongoose';
import { TypeMetadataStorage } from '@nestjs/mongoose/dist/storages/type-metadata.storage';
import * as _ from 'lodash';

export type SchemaOptions = mongoose.SchemaOptions & {
    inheritOption?: boolean
}

function mergeOptions(parentOptions: SchemaOptions, childOptions: SchemaOptions) {
    for (const key in childOptions) {
        if (Object.prototype.hasOwnProperty.call(childOptions, key)) {
            parentOptions[key] = childOptions[key];
        }
    }
    return parentOptions;
}

export function Schema(options?: SchemaOptions): ClassDecorator {
    return (target: Function) => {
        const isInheritOptions = options.inheritOption;

        if (isInheritOptions) {
            let parentOptions = TypeMetadataStorage.getSchemaMetadataByTarget((target as any).__proto__).options;
            parentOptions = _.cloneDeep(parentOptions)  
            options = mergeOptions(parentOptions, options);
        }

        TypeMetadataStorage.addSchemaMetadata({
            target,
            options
        })
    }
}

这是基本架构。

cat.schema.ts

import { Prop, SchemaFactory } from "@nestjs/mongoose";
import { Schema } from '../../common/decorators/schema.decorator'
import { Document } from "mongoose";

export type CatDocument = Cat & Document;

@Schema({
    timestamps: true,
    toJSON: {
        virtuals: true,
        transform: function (doc: any, ret: any) {
            delete ret._id;
            delete ret.__v;
            return ret;
        },
    },
})
export class Cat {
    @Prop()
    name: string;

    @Prop()
    age: number;

    @Prop()
    breed: string;
}

const CatSchema = SchemaFactory.createForClass(Cat);

CatSchema.virtual("id").get(function (this: CatDocument) {
    return this._id;
});

export { CatSchema };

england-cat.schema.ts

import { Prop, SchemaFactory } from "@nestjs/mongoose";
import { Schema } from "../../common/decorators/schema.decorator";
import { Document } from "mongoose";
import { Cat } from "../../cats/schemas/cat.schema";

export type EnglandCatDocument = EnglandCat & Document;

@Schema({
    inheritOption: true
})
export class EnglandCat extends Cat {
    @Prop()
    numberLegs: number;
}

export const EnglandCatSchema = SchemaFactory.createForClass(EnglandCat)

EnglandCat是Cat类的子类,它继承了Cat类的所有选项,如果您想要,可以覆盖一些选项。


我会在有时间的时候测试一下。谢谢。 - thxmike

2

在花费一些时间尝试后,我找到了正确的组合,似乎可以在利用这些技术时起作用。

这是基础类。

base.schema.ts

import { Prop, Schema } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { TimeStamps } from './timestamps.schema';

export type BaseDocument = Base & Document;

@Schema()
export class Base {
  @Prop({
    type: Types.ObjectId,
    required: true,
    default: Types.ObjectId,
  })
  nonce: Types.ObjectId;

  @Prop()
  timestamps: TimeStamps;
}

这是继承了base.schema的类。

person.schema.ts

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { Address } from './address.schema';
import { Base } from './base.schema';

export type PersonDocument = Person & Document;

@Schema({
  toJSON: {
    virtuals: true,
    transform: function (doc: any, ret: any) {
      delete ret._id;
      delete ret.__v;
      return ret;
    },
  },
})
export class Person extends Base {
  @Prop({ required: true })
  first_name: string;

  @Prop({ required: true })
  last_name: string;

  @Prop()
  middle_name: string;

  @Prop()
  data_of_birth: Date;

  @Prop({ type: [{ type: Types.ObjectId, ref: 'Address' }] })
  addresses: [Address];
}
const PersonSchema = SchemaFactory.createForClass(Person);

PersonSchema.virtual('id').get(function (this: PersonDocument) {
  return this._id;
});

export { PersonSchema };

我希望改进的唯一一点是将虚拟(“id”)移到基类中。但是,模式继承不起作用。此时,它只能与模式定义一起使用。这至少让我朝着正确的方向前进了一步。如果有人有改进的方法,请贡献出来。

-1

那些Hieu Cao的回答是正确的,因为问题是“模式继承”。你勾选的答案与扩展模式无关,它只是关于基本继承类的,你在基础中没有任何模式选项。


这并不为问题提供答案。一旦您拥有足够的声望,您就可以评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Noel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接