Git Product home page Git Product logo

Comments (4)

atTheShikhar avatar atTheShikhar commented on August 17, 2024 4

Almost 3 years still not TypeScript support , sad :(

from mongo-cursor-pagination.

ExtraBB avatar ExtraBB commented on August 17, 2024 2

For any of you that are using Typegoose, I created the following:

import { Model, DocumentQuery } from "mongoose";
import { InstanceType } from "typegoose";

export interface IPaginateOptions {
    query?: Object;
    limit?: number;
    fields?: Object;
    paginatedField?: string;
    sortAscending?: Boolean;
    next?: string;
    previous?: string;
}

export interface IPaginateResult<T> {
    hasNext: boolean;
    hasPrevious: boolean;
    next?: string;
    previous?: string;
    results: T[];
}

export interface IPaginateModel<T> extends Model<InstanceType<T>> {
    paginate(options: IPaginateOptions): DocumentQuery<IPaginateResult<T>, InstanceType<T>>;
}

and export your typegoose model as follows:

export const UserModel = new User().getModelForClass(User) as IPaginateModel<InstanceType<User>> & User & typeof User;

Now you can use paginate() anywhere as you would find():

const query = {}; // The query that you would normally use
const users = await UserModel.paginate({ query: query, limit: 10, paginatedField: "email", sortAscending: true });

from mongo-cursor-pagination.

limianwang avatar limianwang commented on August 17, 2024

I agree. Being new to ts as well made it a bit of a challenge. But I did some stack overflow research and one of them led to this:

declare module 'mongoose' {
  export interface IPaginateOptions {
    query: Object;
    limit?: number;
    next?: string;
  }

  export interface PaginateResult<T> {
    results: Array<T>;
    next: string;
  }

  export interface IPaginateModel<T extends Document> extends Model<T> {
    paginate(options: IPaginateOptions): Promise<PaginateResult<T>>;
  }

  export function model<T extends Document>(
    name: string,
    schema?: Schema,
    collection?: string,
    skipInit?: boolean,
  ): IPaginateModel<T>;

  export function model<T extends Document, U extends IPaginateModel<T>>(
    name: string,
    schema?: Schema,
    collection?: string,
    skipInit?: boolean,
  ): U;
}

declare module "mongo-cursor-pagination" {
  import mongoose from 'mongoose';
  export function mongoosePlugin(schema: mongoose.Schema): void;
}

I don't think it's perfect though. Just something that got it working. I definitely would appreciate if any advanced TS folks can help to sort out the typedef for mongo-cursor-pagination. :)

from mongo-cursor-pagination.

cerinoligutom avatar cerinoligutom commented on August 17, 2024

Building on top of @ExtraBB's answer w/ Typegoose v7.2.0 and Mongoose 5.9.20 as of writing.

I created a BaseModelSchema class which has a static paginate() function to be inherited by other domain-specific schema.

import { plugin, defaultClasses, DocumentType } from '@typegoose/typegoose';
import { DocumentQuery } from 'mongoose';

const MongoCursorPagination = require('mongo-cursor-pagination');

@plugin(MongoCursorPagination.mongoosePlugin) // https://github.com/mixmaxhq/mongo-cursor-pagination#with-mongoose
export class BaseModelSchema extends defaultClasses.Base {
  get id(): string {
    return this._id.toHexString();
  }

  static paginate<T>(this: T, options: IPaginateOptions): DocumentQuery<IPaginateResult<T>, DocumentType<T>> {
    return (this as any).paginate(options);
  }
}

interface IPaginateOptions {
  query?: object;
  limit?: number;
  fields?: object;
  paginatedField?: string;
  sortAscending?: boolean;
  next?: string;
  previous?: string;
}

interface IPaginateResult<T> {
  hasNext: boolean;
  hasPrevious: boolean;
  next?: string;
  previous?: string;
  results: T[];
}

Then usage:

class UserModelSchema extends BaseModelSchema {
  // your custom props
}

const UserModel = getModelForClass(UserModelSchema);

const users = await UserModel.paginate({ query: query, limit: 10, paginatedField: "email", sortAscending: true });

from mongo-cursor-pagination.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.