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

added global id option to disable id on schemas #12067

Merged
merged 4 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/schema.js
Expand Up @@ -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 ?
Expand All @@ -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));

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