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

TypeScript: Query Helper types do not work on findOneAndUpdate() #10658

Closed
Davies-Owen opened this issue Sep 1, 2021 · 1 comment
Closed
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@Davies-Owen
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
TypeScript does not recognize my custom Query Helpers on findOneAndUpdate and findByIdAndUpdate queries.

If the current behavior is a bug, please provide the steps to reproduce.

Adapted from the example here: https://mongoosejs.com/docs/typescript/query-helpers.html.
I am trying to make a query helper to lean the results of a query with virtuals. The query helper works correctly for other find queries.

import { Document, Model, Query, Schema, connect, model } from 'mongoose';

interface Project {
  name: string;
  stars: number;
}

const schema = new Schema<Project>({
  name: { type: String, required: true },
  stars: { type: Number, required: true }
});
// Query helpers should return `Query<any, Document<DocType>> & ProjectQueryHelpers`
// to enable chaining.
interface ProjectQueryHelpers {
  leaned(): Query<any, Document<Project>> & ProjectQueryHelpers;
}
schema.query.leaned = function(): Query<any, Document<Project>> & ProjectQueryHelpers {
  return this.lean({ defaults: true, virtuals: true });
};

// 2nd param to `model()` is the Model class to return.
const ProjectModel = model<Project, Model<Project, ProjectQueryHelpers>>('Project', schema);

run().catch(err => console.log(err));

async function run(): Promise<void> {
  await connect('mongodb://localhost:27017/test');

  // TS error: Property 'leaned' does not exist on type 'Query<Project, Document<any, any, Project> & Project, Project, Document<any, any, Project> & Project>'.ts(2339)
  await ProjectModel.findOneAndUpdate({name: 'mongoose'}, {stars: 1}).where('stars').gt(1000).leaned();
}

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "experimentalDecorators": true,
    "incremental": true,
    "jsx": "react",
    "lib": ["ES2020"],
    "module": "commonjs",
    "moduleResolution": "node",
    "pretty": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "target": "ES2020",
    "traceResolution": false
  }
  "include": ["src"]
}

What is the expected behavior?
TypeScript should recognize the query helper on findOneAndUpdate and related queries.
I believe this is happening because findOneAndUpdate and fingByIdAndUpdate return the Query type instead of the QueryWithHelpers type that other find queries return:

findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, callback: (err: CallbackError, doc: T | null, res: any) => void): Query<T | null, EnforceDocument<T, TMethods>, T>;

I apologize if this is intended behavior that I'm just not understanding.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js v14.16.1
TypeScript 4.2.4
Mongoose 6.0.3
MongoDB 4.1.1

Thanks in advance!

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 2, 2021
@vkarpov15 vkarpov15 added this to the 6.0.6 milestone Sep 2, 2021
@yoshigev
Copy link

yoshigev commented Sep 5, 2021

Also the regular helpers, like save(), are not recognized.

We're using findOneAndUpdate and then save inside a transaction to achieve atomicity of several operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

4 participants