mongoose-aggregate-paginate-v2 icon indicating copy to clipboard operation
mongoose-aggregate-paginate-v2 copied to clipboard

I can't use mongoose-aggregate-paginate-v2 and mongoose-paginate-v2 in the same project

Open CarlValenzuela96 opened this issue 3 years ago • 14 comments

in a part of the project I need to use mongoose-paginate-v2 and it works perfect

image

the problem occurs when I try to implement mongoose-aggregate-paginate-v2 in another data model

image

because when I install it it warns me that in the implementation of the first library (mongoose-paginate-v2) there is no "paginate ()" function

image

Is there a way i can use both plugins?

CarlValenzuela96 avatar Nov 17 '21 15:11 CarlValenzuela96

Is the issue happening with TS version ?

aravindnc avatar Dec 11 '21 13:12 aravindnc

Yes, TS version

CarlValenzuela96 avatar Dec 14 '21 20:12 CarlValenzuela96

Hermano, encontre la solucion, no es tanto una solucion potente, pero es lo que hay, toca que lo importes directamente asi bro, con el [ var aggregatePaginate = require("mongoose-aggregate-paginate-v2"); ] y ya te va a jalar.

ErickCincoAyon avatar Mar 01 '22 13:03 ErickCincoAyon

@ErickCincoAyon English please!

aravindnc avatar Mar 01 '22 15:03 aravindnc

Brother, I found the solution, it is not so much a powerful solution, but it is what it is, just import it directly like this bro, with [ var addedPaginate = require("mongoose-aggregate-paginate-v2"); ] and he's going to pull you.

ErickCincoAyon avatar Mar 03 '22 07:03 ErickCincoAyon

image

ErickCincoAyon avatar Mar 03 '22 07:03 ErickCincoAyon

And here is one query: image

ErickCincoAyon avatar Mar 03 '22 07:03 ErickCincoAyon

@ErickCincoAyon Appreciate it. Thank you.

aravindnc avatar Mar 03 '22 14:03 aravindnc

I forgot add this, if u want use the typed of result queries, you should add this image into image

ErickCincoAyon avatar Mar 03 '22 16:03 ErickCincoAyon

This comment could be a bit offtopic, please someone, tell me if i should open another issue or idk, smth like that.

@ErickCincoAyon I think you're using NestJS, correct me if i'm wrong. If you're using it, how did you're injecting the model? I injected it with the next code:

@InjectModel(PaymentsRequest.name)
private readonly paymentsRequestModel: AggregatePaginateModel<PaymentsRequest>

and i'm importing the plugin like you, with require, then calling Schema.plugin(aggregatePaginate) inside the entity file, but when i'm trying to call .aggregatePaginate, like this:

const aggregateQuery = this.paymentsRequestModel.aggregate([
	// query...
]);

return await this.paymentsRequestModel.aggregatePaginate(aggregateQuery, paginationData);

it throws me an error that says that .aggregatePaginate is undefined, i installed @types/mongoose-aggregate-paginate-v2 and mongoose-aggregate-paginate-v2 with yarn, but seems that it's not working for some reason, or well, looks like the plugin isn't right applied, and in fact, i don't really know why aggregatePaginate seems to be undefined, when i'm applying plugin to the Schema. I tried too using .plugin inside the module, like this:

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MyService } from './my.service';
import { MyController } from './my.controller';
import { PaymentsRequest, PaymentsRequestSchema } from '../payments-requests/entities/payments-request.entity';

const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');

@Module({
	providers: [MyService],
	controllers: [MyController],
	imports: [
		MongooseModule.forFeatureAsync([
			{
				name: PaymentsRequest.name,
				useFactory: () => {
					const schema = PaymentsRequestSchema
					schema.plugin(mongooseAggregatePaginate)
					return schema
				}
			}
		])
	]
})

but having the same problem :/

If anyone know what can i'm doing bad, please tell me anyways

AlphaTechnolog avatar Sep 11 '22 11:09 AlphaTechnolog

@AlphaTechnolog Hi, I'm using nestjs also, this import for injected model works as expected:

import { AggregatePaginateModel } from 'mongoose';

and then in service class:

 constructor(
    @InjectModel(User.name) private userModel: AggregatePaginateModel<UserDocument>,
  ) {}

AnnSamsonenko avatar Dec 21 '22 14:12 AnnSamsonenko

@AlphaTechnolog Hi, I'm using nestjs also, this import for injected model works as expected:

import { AggregatePaginateModel } from 'mongoose';

and then in service class:

 constructor(
    @InjectModel(User.name) private userModel: AggregatePaginateModel<UserDocument>,
  ) {}

Oh, thank you Anyways, but i ended up implementing the pagination myself lol, I'll try with this the next time then haha

AlphaTechnolog avatar Dec 21 '22 15:12 AlphaTechnolog

Is it possible to use both in the same model?

jc-granditude avatar Jan 28 '24 21:01 jc-granditude

Yes you can, you only need to add the plugin in the model and other configs:

In your schema

import paginateWithAgregate from 'mongoose-aggregate-paginate-v2';
...
MyBookSchema.plugin(paginateWithAgregate);

then in your Model:

import mongoose, { Document, PaginateModel, AggregatePaginateModel } from "mongoose";
...
export interface MyBookModel extends PaginateModel<MyBookDocument>, AggregatePaginateModel<MyBookDocument> {
...
findBooksWith: (
    this: AggregatePaginateModel<MyBookDocument>,
    param1: string,
    param2: number
  ) => Promise<Pagination>
}

and finally you can use it:

const pipeline = [{ $match: { author: "jhon doe" } }] 
const myAggregate = MyBookModel.aggregate(pipeline);
const queryResults = await MyBookModel.aggregatePaginate(myAggregate, options);

isadev avatar Apr 15 '24 17:04 isadev