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

Add null to select override return type for findOne #14545

Merged

Conversation

sderrow
Copy link
Contributor

@sderrow sderrow commented Apr 28, 2024

Summary

Normally, a findOne or findById returns DocType | null. When using Query.prototype.select, we can provide a RawDocTypeOverride in order to define the true return type of the projected document. Currently, this makes findOne and find queries return RawDocTypeOverride directly, but in order to be consistent, it should return RawDocTypeOverride | null.

This is consistent with how you don't currently have to specify the array version for find; that method automatically returns the array version of RawDocTypeOverride. Therefore, select on findOne should return the nullable version of RawDocTypeOverride automatically.

Examples

import mongoose from "mongoose";

export const selectReturnType = async (): Promise<void> => {
  type Test = {
    _id: mongoose.Types.ObjectId;

    prop: string;
    another: string;

    createdAt: number;
    updatedAt: number;
  };

  const schema = new mongoose.Schema<Test>({
    prop: { type: String },
    another: {type: String },
    createdAt: { type: Number },
    updatedAt: { type: Number },
  });

  type TestDocument = mongoose.HydratedDocument<Test>;
  type TestModel = mongoose.Model<Test, {}, {}, {}, TestDocument>;

  type SlimTest = Pick<Test, "_id" | "prop">;
  type SlimTestDocument = mongoose.HydratedDocument<SlimTest>;

  const M = mongoose.model<Test, TestModel>("Test", schema);

  const myDocs = await M.find({}).exec();
  const myDoc = await M.findOne({}).exec();

  const myProjections = await M.find({}).select<SlimTest>({ prop: 1 }).exec();
  const myProjection = await M.findOne({}).select<SlimTest>({ prop: 1 }).exec();

  /*
  CURRENTLY:
    myDocs is of type TestDocument[]
    myDoc is of type TestDocument | null
    myProjections is of type SlimTestDocument[]
    myProjection is of type SlimTestDocument - THIS IS INCONSISTENT WITH THE NULLABLE RETURN TYPE OF myDoc (two lines above)

  WITH THIS CHANGE:
    [unchanged] myDocs is still of type TestDocument[]
    [unchanged] myDoc is still of type TestDocument | null
    [unchanged] myProjections is still of type SlimTestDocument[]
    [CHANGED] myProjection is now of type SlimTestDocument | null
  */
};

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.

LGTM. I'll make some lint fixes and add tests in master but code looks good.

@vkarpov15 vkarpov15 added this to the 8.3.3 milestone Apr 29, 2024
@vkarpov15 vkarpov15 merged commit 16e6985 into Automattic:master Apr 29, 2024
3 checks passed
vkarpov15 added a commit that referenced this pull request Apr 29, 2024
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