From 74af5f5b34cc7fcf631e1e323f18df36c74f6201 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Wed, 2 Nov 2022 10:16:50 +0100 Subject: [PATCH 1/7] fix(type): pre hook with deleteOne should resolve this as query fix #12622 --- test/types/models.test.ts | 8 ++++++++ types/index.d.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 19c598c63a4..c6678a5a0d1 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -192,6 +192,14 @@ projectSchema.post('save', function() { // this => IProject }); +projectSchema.pre('deleteOne', function() { + this.model; +}); + +projectSchema.post('deleteOne', function() { + this.model; +}); + projectSchema.methods.myMethod = () => 10; projectSchema.statics.myStatic = () => 42; diff --git a/types/index.d.ts b/types/index.d.ts index 3c1e7c2563e..97663a46d84 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -274,19 +274,19 @@ declare module 'mongoose' { plugin, POptions extends Parameters[1] = Parameters[1]>(fn: PFunc, opts?: POptions): this; /** Defines a post hook for the model. */ - post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction): this; - post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction): this; post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction>): this; post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction>): this; + post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction): this; + post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction): this; post>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction>>): this; post>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction>>): this; post(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction): this; post(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction): this; - post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction): this; - post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction): this; post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; + post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction): this; + post>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; post>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction>): this; post>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction>): this; post(method: 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction): this; @@ -295,10 +295,10 @@ declare module 'mongoose' { /** Defines a pre hook for the model. */ pre>(method: 'save', fn: PreSaveMiddlewareFunction): this; pre>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction): this; - pre>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction): this; - pre>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction): this; pre>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction): this; pre>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction): this; + pre>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction): this; + pre>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction): this; pre>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction): this; pre>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction): this; pre(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array) => void | Promise): this; From 76e594036679771136ee3a4142db2896eb6012ec Mon Sep 17 00:00:00 2001 From: hasezoey Date: Wed, 2 Nov 2022 11:46:40 +0100 Subject: [PATCH 2/7] chore(workflows): update actions/checkout to 3.1.0 also consistenize the versions --- .github/workflows/benchmark.yml | 2 +- .github/workflows/codeql.yml | 2 +- .github/workflows/documentation.yml | 2 +- .github/workflows/test.yml | 8 ++++---- .github/workflows/tidelift-alignment.yml | 2 +- .github/workflows/tsd.yml | 6 +++--- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index f9530cf8676..131200b6b03 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-20.04 name: Benchmark TypeScript Types steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 with: fetch-depth: 0 - name: Setup node diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 9c78ef14c90..e3c4adee9f3 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -21,7 +21,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index d3ef9cacb67..74295debf20 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu-20.04 name: Test Generating Docs steps: - - uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3ee1c9b2fbf..d10fdcc29aa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest name: Lint JS-Files steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 @@ -59,7 +59,7 @@ jobs: MONGOMS_VERSION: ${{ matrix.mongodb }} MONGOMS_PREFER_GLOBAL_PATH: 1 steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 @@ -93,7 +93,7 @@ jobs: runs-on: ubuntu-latest name: Replica Set tests steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 with: @@ -110,6 +110,6 @@ jobs: contents: read steps: - name: Check out repo - uses: actions/checkout@v3 + uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Dependency review uses: actions/dependency-review-action@v2 diff --git a/.github/workflows/tidelift-alignment.yml b/.github/workflows/tidelift-alignment.yml index 8f5564f119d..f825b90e552 100644 --- a/.github/workflows/tidelift-alignment.yml +++ b/.github/workflows/tidelift-alignment.yml @@ -15,7 +15,7 @@ jobs: if: github.repository == 'Automattic/mongoose' steps: - name: Checkout - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 with: diff --git a/.github/workflows/tsd.yml b/.github/workflows/tsd.yml index 76fd0b7814a..4e6ba4b102c 100644 --- a/.github/workflows/tsd.yml +++ b/.github/workflows/tsd.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest name: Lint TS-Files steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 @@ -40,7 +40,7 @@ jobs: runs-on: ubuntu-latest name: Test Typescript Types steps: - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2 + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 - name: Setup node uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1 @@ -50,4 +50,4 @@ jobs: - run: npm install - name: Typings - run: npm run test-tsd \ No newline at end of file + run: npm run test-tsd From 80f597f3b592f7918d1aa6931d405af7a09880cd Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 10:58:27 -0400 Subject: [PATCH 3/7] fix(types): make array paths optional in inferred type of array default returns undefined Fix #12420 --- test/types/schema.test.ts | 12 ++++++++++++ types/inferschematype.d.ts | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 86e4d2668a4..7ca2dfa90f1 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -870,3 +870,15 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } + +function gh12420() { + const TestSchema = new Schema( + { + comments: { type: [String], default: () => undefined } + } + ); + + expectType<{ + comments?: string[] + }>({} as InferSchemaType); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index a0406388f00..27090b89eb4 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -60,6 +60,12 @@ declare module 'mongoose' { : unknown; } +type IsPathDefaultUndefined = PathType extends { default: undefined } ? + true : + PathType extends { default: (...args: any[]) => undefined } ? + true : + false; + /** * @summary Checks if a document path is required or optional. * @param {P} P Document path. @@ -69,7 +75,7 @@ type IsPathRequired = P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[] ? true : P extends (Record) - ? P extends { default: undefined } + ? IsPathDefaultUndefined

extends true ? false : true : P extends (Record) From 1f6864f04f362ea4692ab2685f6014e067bcb2df Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 12:40:50 -0400 Subject: [PATCH 4/7] test: address some code review comments to streamline tests --- test/types/schema.test.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 7ca2dfa90f1..2ab4698e4bc 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -412,6 +412,7 @@ export function autoTypedSchema() { array5: any[]; array6: string[]; array7?: string[]; + array8?: string[]; decimal1?: Types.Decimal128; decimal2?: Types.Decimal128; decimal3?: Types.Decimal128; @@ -458,6 +459,7 @@ export function autoTypedSchema() { array5: [], array6: { type: [String] }, array7: { type: [String], default: undefined }, + array8: { type: [String], default: () => undefined }, decimal1: Schema.Types.Decimal128, decimal2: 'Decimal128', decimal3: 'decimal128' @@ -870,15 +872,3 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } - -function gh12420() { - const TestSchema = new Schema( - { - comments: { type: [String], default: () => undefined } - } - ); - - expectType<{ - comments?: string[] - }>({} as InferSchemaType); -} From f90e73d89344257c365134d58ace0a5f81eb89d4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 12:47:54 -0400 Subject: [PATCH 5/7] chore: release 6.7.1 --- CHANGELOG.md | 12 ++++++++++++ package.json | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0852af0eacf..b625c0cba1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +6.7.1 / 2022-11-02 +================== + * fix(query): select Map field with select: false when explicitly requested #12616 #12603 [lpizzinidev](https://github.com/lpizzinidev) + * fix: correctly find paths underneath single nested document with an array of mixed #12605 #12530 + * fix(populate): better support for populating maps of arrays of refs #12601 #12494 + * fix(types): add missing create constructor signature override type #12585 [naorpeled](https://github.com/naorpeled) + * fix(types): make array paths optional in inferred type of array default returns undefined #12649 #12420 + * fix(types): improve ValidateOpts type #12606 [Freezystem](https://github.com/Freezystem) + * docs: add Lodash guide highlighting issues with cloneDeep() #12609 + * docs: removed v5 link from v6 docs #12641 #12624 [lpizzinidev](https://github.com/lpizzinidev) + * docs: removed outdated connection example #12618 [lpizzinidev](https://github.com/lpizzinidev) + 6.7.0 / 2022-10-24 ================== * feat: upgrade to mongodb driver 4.11.0 #12446 diff --git a/package.json b/package.json index 3cf33237eb8..e1dd615e0a8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mongoose", "description": "Mongoose MongoDB ODM", - "version": "6.7.0", + "version": "6.7.1", "author": "Guillermo Rauch ", "keywords": [ "mongodb", From 4ddeb468d5bfe13269511b57405b15ea820c69e0 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 17:26:38 -0400 Subject: [PATCH 6/7] chore: clean up loadSponsorData --- docs/loadSponsorData.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/loadSponsorData.js b/docs/loadSponsorData.js index 168d0a13b45..84be29dcabd 100644 --- a/docs/loadSponsorData.js +++ b/docs/loadSponsorData.js @@ -5,9 +5,6 @@ const config = require('../.config'); const fs = require('fs'); const mongoose = require('../'); -const poralHost = 'https://staging.poral.io'; -const opencollectiveUrl = `${poralHost}/invoke/${config.poralId}/generateSponsors`; - run().catch(err => { console.error(err); process.exit(-1); @@ -91,6 +88,12 @@ async function run() { sponsor.alt = override.alt; } } + + const additionalSponsors = await OpenCollectiveSponsor.find({}). + then(docs => docs.filter(doc => doc.openCollectiveId == null)); + for (const sponsor of additionalSponsors) { + opencollectiveSponsors.push(sponsor); + } if (opencollectiveSponsors != null) { fs.writeFileSync(`${__dirname}/data/opencollective.json`, JSON.stringify(opencollectiveSponsors, null, ' ')); From ae14d0e52630bf65d255f52bf6baa6ef056d148e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 4 Nov 2022 14:31:14 -0400 Subject: [PATCH 7/7] fix(types): allow setting SchemaTypeOptions' `index` property to IndexOptions Fix #12562 --- test/types/schema.test.ts | 25 +++++++++++++++++++++++++ types/schematypes.d.ts | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 2ab4698e4bc..b0e148afce5 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -872,3 +872,28 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } + +function gh12562() { + const emailRegExp = /@/; + const userSchema = new Schema( + { + email: { + type: String, + trim: true, + validate: { + validator: (value: string) => emailRegExp.test(value), + message: 'Email is not valid' + }, + index: { // uncomment the index object and for me trim was throwing an error + partialFilterExpression: { + email: { + $exists: true, + $ne: null + } + } + }, + select: false + } + } + ); +} diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index 899660d9fe6..093651d1408 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -100,7 +100,7 @@ declare module 'mongoose' { * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will * build an index on this path when the model is compiled. */ - index?: boolean | IndexDirection; + index?: boolean | IndexDirection | IndexOptions; /** * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose