Typegoose: using plugins: mongoose-paginate

Created on 14 Apr 2020  路  6Comments  路  Source: typegoose/typegoose

I am trying to use the paginate plugin but I don't get IntelliSense for the paginate() method provided by this plugin. Typescript complains about paginate() method Property 'paginate' does not exist on type 'ReturnModelType<typeof Dummy, unknown>'. Though, it works after transpilation.

import { Typegoose, plugin } from '@typegoose/typegoose';
import { FilterQuery, PaginateOptions, PaginateResult } from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';

@plugin(mongoosePaginate)
export class Dummy {
  @prop()
  name!: string;
}

I tried this solution https://github.com/szokodiakos/typegoose/issues/63. It somehow worked but it gives deprecation warning
(node:15629) DeprecationWarning: The Typegoose Class is deprecated, please try to remove it.
Anyway, It would be troublesome to add the paginate method every time I need to add pagination for a model.

Versions

  • "@typegoose/typegoose": "^6.4.0",
  • "mongoose": "^5.9.7",
question

All 6 comments

This solution worked for me

import mongoosePaginate from 'mongoose-paginate-v2';
import { FilterQuery, PaginateOptions, PaginateResult } from 'mongoose';

type PaginateMethod<T> = (
  query?: FilterQuery<T>,
  options?: PaginateOptions,
  callback?: (err: any, result: PaginateResult<T>) => void,
) => Promise<PaginateResult<T>>;

@plugin(mongoosePaginate)
export class Dummy {
  @prop()
  name!: string;

  static paginate: PaginateMethod<Dummy>;
}

It somehow worked but it gives deprecation warning

you extended the deprecated (and useless) Typegoose class


from what i read, this issue is solved

@hasezoey

why should I add the static method every time?
I believe the @plugin() decorator should be responsible for that.

@kerolloz an decorator cant add an type, so you either need to do it for every class, or extend an class that has it defined

The problem with this approach is I still don't get IntelliSense until I write the static method again in the subclass.

import { plugin } from '@typegoose/typegoose';
import mongoosePaginate from 'mongoose-paginate-v2';


type PaginateMethod<T> = (
  query?: FilterQuery<T>,
  options?: PaginateOptions,
  callback?: (err: any, result: PaginateResult<T>) => void,
) => Promise<PaginateResult<T>>;

@plugin(mongoosePaginate)
export abstract class PaginatedModel {
  static paginate: PaginateMethod<PaginatedModel>;
}

export class Dummy extends PaginatedModel {
  @prop()
  name!: string;

  static paginate: PaginateMethod<Dummy>; // have to be repeated
}

functions that have just an type in an abstract class need to be implemented by all child classes

it should work when you just remove the abstract keyword (the class will persist anyway)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Negan1911 picture Negan1911  路  10Comments

mohamedGamalAbuGalala picture mohamedGamalAbuGalala  路  5Comments

hasezoey picture hasezoey  路  6Comments

richarddd picture richarddd  路  8Comments

rubenvereecken picture rubenvereecken  路  5Comments