Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow QueryOptions populate parameter type PopulateOptions #10587

Merged
merged 1 commit into from
Aug 23, 2021

Conversation

osmanakol
Copy link
Contributor

Summary
Mongoose 5.10.x version QueryOptions method populate property type allow to use string, ModelPopulateOptions. This make it easy to develop generic mongoose crud operation. I checked and realized after mongoose version 5.10.x some method is changed. I changed to populate property type for adapt new version.

Examples
Mongoose 5.10.x index.d.ts :

interface QueryFindBaseOptions {
        /** Sets a default collation for every query and aggregation. */
        collation?: CollationOptions;
        explain?: any;
        lean?: boolean;
        populate?: string | ModelPopulateOptions;
        /** like select, it determines which fields to return */
        projection?: any;
        /** use client session for transaction */
        session?: ClientSession;
    }

Mongoose 5.13.7 index.d.ts :

  interface QueryOptions {
    arrayFilters?: { [key: string]: any }[];
    batchSize?: number;
    collation?: mongodb.CollationDocument;
    comment?: any;
    context?: string;
    explain?: any;
    fields?: any | string;
    hint?: any;
    /**
     * If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
     */
    lean?: boolean | any;
    limit?: number;
    maxTimeMS?: number;
    maxscan?: number;
    multi?: boolean;
    multipleCastError?: boolean;
    /**
     * By default, `findOneAndUpdate()` returns the document as it was **before**
     * `update` was applied. If you set `new: true`, `findOneAndUpdate()` will
     * instead give you the object after `update` was applied.
     */
    new?: boolean;
    omitUndefined?: boolean;
    overwrite?: boolean;
    overwriteDiscriminatorKey?: boolean;
    populate?: string | string[] | PopulateOptions | PopulateOptions[];
    projection?: any;
    /**
     * if true, returns the raw result from the MongoDB driver
     */
    rawResult?: boolean;
    readPreference?: mongodb.ReadPreferenceMode;
    /**
     * An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
     */
    returnOriginal?: boolean;
    /**
     * Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
     */
    returnDocument?: string;
    runValidators?: boolean;
    sanitizeProjection?: boolean;
    /** The session associated with this query. */
    session?: mongodb.ClientSession;
    setDefaultsOnInsert?: boolean;
    skip?: number;
    snapshot?: any;
    sort?: any;
    /** overwrites the schema's strict mode option */
    strict?: boolean | string;
    tailable?: number;
    /**
     * If set to `false` and schema-level timestamps are enabled,
     * skip timestamps for this update. Note that this allows you to overwrite
     * timestamps. Does nothing if schema-level timestamps are not set.
     */
    timestamps?: boolean;
    upsert?: boolean;
    useFindAndModify?: boolean;
    writeConcern?: any;
  }

I changed populate method :

// Before : 
populate: string
// After:
populate?: string | string[] | PopulateOptions | PopulateOptions[];

Through of this change We can use the find method as in the 5.10.x version:
find(FilterQuery<T>, projection ,options?: {populate:{path:"<relation path>, model:<relation db schema>"}}) .

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@vkarpov15 vkarpov15 added this to the 5.13.8 milestone Aug 23, 2021
@vkarpov15 vkarpov15 merged commit 8d00f62 into Automattic:master Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants