Skip to content

Commit

Permalink
🤖 Merge PR #69633 [mongoose-delete] Update SoftDeteleModel with missi…
Browse files Browse the repository at this point in the history
…ng arguments from the mongoose.Model to work with model methods by @facularrauri
  • Loading branch information
facularrauri committed May 17, 2024
1 parent 597a0b3 commit 2352fc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions types/mongoose-delete/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ declare namespace MongooseDelete {
| "updateOne"
| "updateMany"
| "aggregate";
interface SoftDeleteModel<T extends Omit<mongoose.Document, "delete">, QueryHelpers = {}>
extends mongoose.Model<T, QueryHelpers>
interface SoftDeleteModel<
T extends Omit<mongoose.Document, "delete">,
QueryHelpers = {},
TInstanceMethods = {},
TVirtuals = {},
THydratedDocumentType = mongoose.HydratedDocument<T, TVirtuals & TInstanceMethods, QueryHelpers>,
TSchema = any,
> extends
mongoose.Model<
T,
QueryHelpers,
TInstanceMethods,
TVirtuals,
THydratedDocumentType,
TSchema
>
{
/** Count only deleted documents */
countDocumentsDeleted: this["countDocuments"];
Expand Down
16 changes: 16 additions & 0 deletions types/mongoose-delete/mongoose-delete-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import MongooseDelete = require("mongoose-delete");
interface PetDocument extends MongooseDelete.SoftDeleteDocument {
name: string;
}

// Custom methods
interface PetMethods {
checkName(name: string): boolean;
}

// Custom Static methods
interface PetModel extends MongooseDelete.SoftDeleteModel<PetDocument, {}, PetMethods> {
getNames(): string[];
}

const PetSchema = new mongoose.Schema<PetDocument>({
name: String,
});
Expand Down Expand Up @@ -42,6 +53,8 @@ const Pet = mongoose.model<PetDocument, MongooseDelete.SoftDeleteModel<PetDocume

const Pet2 = mongoose.model("Pet", PetSchema) as MongooseDelete.SoftDeleteModel<PetDocument>;

const Pet3 = mongoose.model<PetDocument, PetModel>("Pet", PetSchema);

const fluffy = new Pet({ name: "Fluffy" });

fluffy.delete(() => {});
Expand Down Expand Up @@ -102,3 +115,6 @@ Pet.updateManyDeleted({ age: 10 }, { name: "Fluffy" });
Pet.updateManyWithDeleted({ age: 10 }, { name: "Fluffy" });
Pet.aggregateDeleted([{ $match: { age: 10 } }]);
Pet.aggregateWithDeleted([{ $match: { age: 10 } }]);

// $ExpectType string[]
Pet3.getNames();

0 comments on commit 2352fc2

Please sign in to comment.