aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

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

Closed CarlValenzuela96 closed 2 months ago

CarlValenzuela96 commented 2 years ago

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?

aravindnc commented 2 years ago

Is the issue happening with TS version ?

CarlValenzuela96 commented 2 years ago

Yes, TS version

ErickCincoAyon commented 2 years ago

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.

aravindnc commented 2 years ago

@ErickCincoAyon English please!

ErickCincoAyon commented 2 years ago

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 commented 2 years ago

image

ErickCincoAyon commented 2 years ago

And here is one query: image

aravindnc commented 2 years ago

@ErickCincoAyon Appreciate it. Thank you.

ErickCincoAyon commented 2 years ago

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

AlphaTechnolog commented 1 year ago

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

AnnSamsonenko commented 1 year ago

@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>,
  ) {}
AlphaTechnolog commented 1 year ago

@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

jc-granditude commented 7 months ago

Is it possible to use both in the same model?

isadev commented 4 months ago

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);