From 08f1179ed447f8104e47185854430a69a118dd3c Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Thu, 7 Jul 2022 14:59:45 -0400 Subject: [PATCH 1/4] added global `id` option --- lib/schema.js | 4 ++-- lib/validoptions.js | 1 + test/index.test.js | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index 5177e5c9590..e706378e6b2 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -434,8 +434,8 @@ Schema.prototype.pick = function(paths, options) { Schema.prototype.defaultOptions = function(options) { this._userProvidedOptions = options == null ? {} : utils.clone(options); const baseOptions = this.base && this.base.options || {}; - const strict = 'strict' in baseOptions ? baseOptions.strict : true; + const id = 'id' in baseOptions ? baseOptions.id : true; options = utils.options({ strict: strict, strictQuery: 'strict' in this._userProvidedOptions ? @@ -454,7 +454,7 @@ Schema.prototype.defaultOptions = function(options) { validateBeforeSave: true, // the following are only applied at construction time _id: true, - id: true, + id: id, typeKey: 'type' }, utils.clone(options)); diff --git a/lib/validoptions.js b/lib/validoptions.js index 0f7b697c415..a42e552c7af 100644 --- a/lib/validoptions.js +++ b/lib/validoptions.js @@ -15,6 +15,7 @@ const VALID_OPTIONS = Object.freeze([ 'bufferTimeoutMS', 'cloneSchemas', 'debug', + 'id', 'timestamps.createdAt.immutable', 'maxTimeMS', 'objectIdGetter', diff --git a/test/index.test.js b/test/index.test.js index 9c80b5ae644..b2ede2451de 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1086,4 +1086,22 @@ describe('mongoose module:', function() { assert.deepEqual(res.toObject(), { answer: 42 }); }); }); + describe('global id option', function() { + it('can diable the id virtual on schemas gh-11966', async function() { + const m = new mongoose.Mongoose(); + m.set('id', false); + + const db = await m.connect(start.uri); + + const schema = new m.Schema({ title: String }); + + const falseID = db.model('gh11966', schema); + + + const entry = await falseID.create({ + title: 'The IDless master' + }); + assert.equal(entry.id, undefined); + }); + }); }); From 97a63a46b7f2fca6d0de1006322c008c18c493d3 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Thu, 7 Jul 2022 15:10:31 -0400 Subject: [PATCH 2/4] typo fix --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index b2ede2451de..831b817f7a5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1087,7 +1087,7 @@ describe('mongoose module:', function() { }); }); describe('global id option', function() { - it('can diable the id virtual on schemas gh-11966', async function() { + it('can disable the id virtual on schemas gh-11966', async function() { const m = new mongoose.Mongoose(); m.set('id', false); From 80d1aaae67dd5e1eabdf803434c6aef6b7c1f768 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 19 Jul 2022 11:56:16 -0400 Subject: [PATCH 3/4] feat(types): add `id` to MongooseOptions re: #11966 --- types/mongooseoptions.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/types/mongooseoptions.d.ts b/types/mongooseoptions.d.ts index dabae7e5333..2f80ff38ba0 100644 --- a/types/mongooseoptions.d.ts +++ b/types/mongooseoptions.d.ts @@ -79,6 +79,9 @@ declare module 'mongoose' { | stream.Writable | ((collectionName: string, methodName: string, ...methodArgs: any[]) => void); + /** Defaults to `true`. If `true`, adds a `id` virtual to all schemas by default. */ + id?: boolean; + /** * If `false`, it will change the `createdAt` field to be [`immutable: false`](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-immutable) * which means you can update the `createdAt`. From 85f18f834f1fc6a64597cba511f2f9a439bad87c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 20 Jul 2022 11:06:29 -0400 Subject: [PATCH 4/4] Update types/mongooseoptions.d.ts Co-authored-by: hasezoey --- types/mongooseoptions.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/mongooseoptions.d.ts b/types/mongooseoptions.d.ts index 2f80ff38ba0..b470e1aa9b6 100644 --- a/types/mongooseoptions.d.ts +++ b/types/mongooseoptions.d.ts @@ -79,7 +79,10 @@ declare module 'mongoose' { | stream.Writable | ((collectionName: string, methodName: string, ...methodArgs: any[]) => void); - /** Defaults to `true`. If `true`, adds a `id` virtual to all schemas by default. */ + /** + * If `true`, adds a `id` virtual to all schemas unless overwritten on a per-schema basis. + * @defaultValue true + */ id?: boolean; /**