Skip to content

Commit

Permalink
Merge pull request #12067 from Automattic/gh-11966
Browse files Browse the repository at this point in the history
added global `id` option to disable id on schemas
  • Loading branch information
vkarpov15 committed Jul 20, 2022
2 parents 0c92558 + 85f18f8 commit 6fcc0dc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/schema.js
Expand Up @@ -456,8 +456,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 ?
Expand All @@ -476,7 +476,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));

Expand Down
1 change: 1 addition & 0 deletions lib/validoptions.js
Expand Up @@ -15,6 +15,7 @@ const VALID_OPTIONS = Object.freeze([
'bufferTimeoutMS',
'cloneSchemas',
'debug',
'id',
'timestamps.createdAt.immutable',
'maxTimeMS',
'objectIdGetter',
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Expand Up @@ -1086,4 +1086,22 @@ describe('mongoose module:', function() {
assert.deepEqual(res.toObject(), { answer: 42 });
});
});
describe('global id option', function() {
it('can disable 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);
});
});
});
6 changes: 6 additions & 0 deletions types/mongooseoptions.d.ts
Expand Up @@ -79,6 +79,12 @@ declare module 'mongoose' {
| stream.Writable
| ((collectionName: string, methodName: string, ...methodArgs: any[]) => void);

/**
* If `true`, adds a `id` virtual to all schemas unless overwritten on a per-schema basis.
* @defaultValue true
*/
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`.
Expand Down

0 comments on commit 6fcc0dc

Please sign in to comment.