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

Typescript support #15

Closed duongkhoangiam closed 3 years ago

duongkhoangiam commented 4 years ago

This library support typescript ?

aravindnc commented 4 years ago

@duongkhoangiam Not now. Contributions are welcome.

0xknon commented 4 years ago

@aravindnc, could you pease review the following index.d.ts? I just referenced mongoose-paginate-v2 to make the type file.

// Type definitions for mongoose-paginate-v2 1.3
// Project: https://github.com/webgangster/mongoose-paginate-v2
// Definitions by: Linus Brolin <https://github.com/linusbrolin>
//                 simonxca <https://github.com/simonxca>
//                 woutgg <https://github.com/woutgg>
//                 oktapodia <https://github.com/oktapodia>
//                 Dongjun Lee <https://github.com/ChazEpps>
//                 gamsterX <https://github.com/gamsterx>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.2
//
// Based on type declarations for mongoose-paginate 5.0.0.

declare module 'mongoose' {
  interface CustomLabels {
    totalDocs?: string;
    limit?: string;
    page?: string;
    totalPages?: string;
    docs?: string;
    nextPage?: string;
    prevPage?: string;
    pagingCounter?: string;
    hasPrevPage?: string;
    hasNextPage?: string;
  }

  interface PaginateOptions {
    /* tslint:disable-next-line: ban-types */
    sort?: Object | string;
    offset?: number;
    page?: number;
    limit?: number;
    customLabels?: CustomLabels;
    /* If pagination is set to `false`, it will return all docs without adding limit condition. (Default: `true`) */
    pagination?: boolean;
    allowDiskUse?: boolean;
    countQuery?: Object;
  }

  interface QueryPopulateOptions {
    /** space delimited path(s) to populate */
    path: string;
    /** optional fields to select */
    select?: any;
    /** optional query conditions to match */
    match?: any;
    /** optional model to use for population */
    model?: string | Model<any>;
    /** optional query options like sort, limit, etc */
    options?: any;
    /** deep populate */
    populate?: QueryPopulateOptions | QueryPopulateOptions[];
  }

  interface AggregatePaginateResult<T> {
    docs: T[];
    totalDocs: number;
    limit: number;
    page?: number;
    totalPages: number;
    nextPage?: number | null;
    prevPage?: number | null;
    pagingCounter: number;
    hasPrevPage: boolean;
    hasNextPage: boolean;
    // meta?: any;
    // [customLabel: string]: T[] | number | boolean | null | undefined;
  }

  interface AggregatePaginateModel<T extends Document> extends Model<T> {
    aggregatePaginate(
      query?: mongoose.Aggregate<T>,
      options?: PaginateOptions,
      callback?: (err: any, result: AggregatePaginateResult<T>) => void
    ): Promise<AggregatePaginateResult<T>>;
  }

  function model(
    name: string,
    schema?: Schema,
    collection?: string,
    skipInit?: boolean
  ): AggregatePaginateModel<any>;
}

import mongoose = require('mongoose');
declare function _(schema: mongoose.Schema): void;
export = _;
declare namespace _ {
  const aggregatePaginate: { options: mongoose.PaginateOptions };
}
aravindnc commented 4 years ago

@knyuwork Thanks for this. But I'm not so experienced with TS. Let's wait for some other person to review it.

tuliren commented 4 years ago

@knyuwork, thank you!

The type definition looks good except that these two lines should be deleted:

// meta?: any;
// [customLabel: string]: T[] | number | boolean | null | undefined;

This plugin does not support meta. So these comments are irrelevant.

Do you have any plan to submit this to DefinitelyTyped any time soon?

Thank you.

acrilex1 commented 3 years ago

This was never published on DefinitelyTyped, is it normal? The definition seems ok, @knyuwork should I submit it or will you do it?

acrilex1 commented 3 years ago

Just a detail, if found a small bug:

interface AggregatePaginateModel<T extends Document> extends Model<T> {
    aggregatePaginate(
      query?: mongoose.Aggregate<T[]>,
      options?: PaginateOptions,
      callback?: (err: any, result: AggregatePaginateResult<T>) => void
    ): Promise<AggregatePaginateResult<T>>;
  }

Parameter query should be T[] because an aggregate is always an array of Documents.

acrilex1 commented 3 years ago

@aravindnc types are added in DefinitelyTyped. This issue can be closed.

aravindnc commented 3 years ago

Thanks for the contribution @acrilex1