diff --git a/server/src/external/mongo/db.test.ts b/server/src/external/mongo/db.test.ts new file mode 100644 index 000000000..1a5554598 --- /dev/null +++ b/server/src/external/mongo/db.test.ts @@ -0,0 +1,33 @@ +import t from "tap"; +import { CloseAllConnections } from "../../test-utils/close-connections"; +import ResetDBState from "../../test-utils/resets"; +import db from "./db"; + +t.beforeEach(ResetDBState); + +t.test("ID field autoprojection", async (t) => { + const res = await db.scores.findOne(); + t.equal(res!._id, undefined); + + // @ts-expect-error todo: #205 type properly + const res2 = await db.scores.findOne({}, { projectID: true }); + t.not(res2!._id, undefined); + + const res3 = await db.scores.findOne({}, { projection: { scoreID: 1 } }); + t.equal(res3!._id, undefined); + + // @ts-expect-error todo: #205 type properly + const res4 = await db.scores.findOne({}, { projection: { scoreID: 1 }, projectID: true }); + t.not(res4!._id, undefined); + + const res5 = await db.scores.findOne({}, { projection: { scoreID: 0 } }); + t.equal(res5!._id, undefined); + + // @ts-expect-error todo: #205 type properly + const res6 = await db.scores.findOne({}, { projection: { scoreID: 0 }, projectID: true }); + t.not(res6!._id, undefined); + + t.end(); +}); + +t.teardown(CloseAllConnections); diff --git a/server/src/external/mongo/db.ts b/server/src/external/mongo/db.ts index caf3c7760..1c29db6ab 100644 --- a/server/src/external/mongo/db.ts +++ b/server/src/external/mongo/db.ts @@ -72,10 +72,10 @@ const RemoveIDMiddleware: TMiddleware = // eslint-disable-next-line @typescript-eslint/no-explicit-any (args: any, method) => { if ((method === "find" || method === "findOne") && !args.options.projectID) { - if (args.options.project) { - args.options.project._id = 0; + if (args.options.projection) { + args.options.projection._id = 0; } else { - args.options.project = { _id: 0 }; + args.options.projection = { _id: 0 }; } }