emiljanitzek / mongoose-delete-ts

Mongoose Soft Delete Plugin
MIT License
10 stars 4 forks source link

Unable to find deleteById method #29

Open jdhrivas opened 1 week ago

jdhrivas commented 1 week ago

I'm applying the mongoose-delete-ts plugin globally. Any thoughts?

Error message: Property 'deleteById' does not exist on type 'Model<SubmissionDocument, {}, {}, {}, Document<unknown, {}, SubmissionDocument> & Submission & Document<unknown, any, any> & Required<...>, any> & DeletedMethods'.ts(2339)

import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { BadRequestException, Injectable } from '@nestjs/common';
import { DeletedMethods } from 'mongoose-delete-ts';
import { Submission, SubmissionDocument } from './schemas/submission.schema';
import { CreateSubmissionInput, DeleteSubmissionOutput, SubmissionOutput } from './dto';

@Injectable()
export class SubmissionsService {
  constructor(
    @InjectModel(Submission.name) 
    private SubmissionModel: Model<SubmissionDocument> & DeletedMethods,
  ) { }

  async remove(id: string): Promise<DeleteSubmissionOutput> {
    try {
      const submission = (await this.SubmissionModel.deleteById(id));
      console.log(submission, id)
      return this.toDeleteSubmissionOutput(submission)
    } catch (err) {
      throw new BadRequestException(err.message);
    }
  }

  private toDeleteSubmissionOutput(submission: any): DeleteSubmissionOutput {
    return {
      ...submission
    };
  }
}
emiljanitzek commented 3 days ago

It looks like you are using the wrong type reference. DeletedMethods are the instance methods on the document (submission.delete()), not the static methods. To use the static methods you need to import DeletedStaticMethods. See example in the README

import { DeletedStaticMethods, DeletedMethods } from 'mongoose-delete-ts';
...
private SubmissionModel: Model<SubmissionDocument, {}, DeletedMethods> & DeletedStaticMethods,