Skip to content

Commit

Permalink
Merge pull request #13513 from Automattic/vkarpov15/schema-collection…
Browse files Browse the repository at this point in the history
…-options

feat(schema): add collectionOptions option to schemas
  • Loading branch information
vkarpov15 committed Jun 19, 2023
2 parents 855008f + faefe56 commit 643d55a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ Valid options:
- [skipVersioning](#skipVersioning)
- [timestamps](#timestamps)
- [storeSubdocValidationError](#storeSubdocValidationError)
- [collectionOptions](#collectionOptions)
- [methods](#methods)
- [query](#query-helpers)

Expand Down Expand Up @@ -1399,6 +1400,30 @@ const Parent = mongoose.model('Parent', parentSchema);
new Parent({ child: {} }).validateSync().errors;
```

<h2 id="collectionOptions">
<a href="#collectionOptions">
option: collectionOptions
</a>
</h2>

Options like [`collation`](#collation) and [`capped`](#capped) affect the options Mongoose passes to MongoDB when creating a new collection.
Mongoose schemas support most [MongoDB `createCollection()` options](https://www.mongodb.com/docs/manual/reference/method/db.createCollection/), but not all.
You can use the `collectionOptions` option to set any `createCollection()` options; Mongoose will use `collectionOptions` as the default values when calling `createCollection()` for your schema.

```javascript
const schema = new Schema({ name: String }, {
autoCreate: false,
collectionOptions: {
capped: true,
max: 1000
}
});
const Test = mongoose.model('Test', schema);

// Equivalent to `createCollection({ capped: true, max: 1000 })`
await Test.createCollection();
```

<h2 id="es6-classes"><a href="#es6-classes">With ES6 Classes</a></h2>

Schemas have a [`loadClass()` method](api/schema.html#schema_Schema-loadClass)
Expand Down
8 changes: 8 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,14 @@ Model.createCollection = async function createCollection(options) {
throw new MongooseError('Model.createCollection() no longer accepts a callback');
}

const collectionOptions = this &&
this.schema &&
this.schema.options &&
this.schema.options.collectionOptions;
if (collectionOptions != null) {
options = Object.assign({}, collectionOptions, options);
}

const schemaCollation = this &&
this.schema &&
this.schema.options &&
Expand Down
1 change: 1 addition & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ let id = 0;
* - [timestamps](https://mongoosejs.com/docs/guide.html#timestamps): object or boolean - defaults to `false`. If true, Mongoose adds `createdAt` and `updatedAt` properties to your schema and manages those properties for you.
* - [pluginTags](https://mongoosejs.com/docs/guide.html#pluginTags): array of strings - defaults to `undefined`. If set and plugin called with `tags` option, will only apply that plugin to schemas with a matching tag.
* - [virtuals](https://mongoosejs.com/docs/tutorials/virtuals.html#virtuals-via-schema-options): object - virtuals to define, alias for [`.virtual`](https://mongoosejs.com/docs/api/schema.html#Schema.prototype.virtual())
* - [collectionOptions]: object with options passed to [`createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createCollection/) when calling `Model.createCollection()` or `autoCreate` set to true.
*
* #### Options for Nested Schemas:
*
Expand Down
13 changes: 13 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7028,6 +7028,19 @@ describe('Model', function() {
assert(bypass);
});
});

it('respects schema-level `collectionOptions` for setting options to createCollection()', async function() {
const testSchema = new Schema({
name: String
}, { collectionOptions: { capped: true, size: 1024 } });
const TestModel = db.model('Test', testSchema);
await TestModel.init();
await TestModel.collection.drop();
await TestModel.createCollection();

const isCapped = await TestModel.collection.isCapped();
assert.ok(isCapped);
});
});


Expand Down
3 changes: 3 additions & 0 deletions types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ declare module 'mongoose' {
/** Sets a default collation for every query and aggregation. */
collation?: mongodb.CollationOptions;

/** Arbitrary options passed to `createCollection()` */
collectionOptions?: mongodb.CreateCollectionOptions;

/** The timeseries option to use when creating the model's collection. */
timeseries?: mongodb.TimeSeriesCollectionOptions;

Expand Down

0 comments on commit 643d55a

Please sign in to comment.