aldeed:schema-index

v3.0.0Published 5 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

CircleCI

aldeed:schema-index

A Meteor package that allows you to control some MongoDB indexing from your SimpleSchema. This package is currently included automatically with the aldeed:collection2 package.

Installation

In your Meteor app directory, enter:

$ meteor add aldeed:schema-index

Usage

Use the index option to ensure a MongoDB index for a specific field:

1{
2  title: {
3    type: String,
4    index: 1
5  }
6}

Set to 1 or true for an ascending index. Set to -1 for a descending index. Or you may set this to another type of specific MongoDB index, such as "2d". Indexes work on embedded sub-documents as well.

If you have created an index for a field by mistake and you want to remove or change it, set index to false:

1{
2  "address.street": {
3    type: String,
4    index: false
5  }
6}

IMPORTANT: If you need to change anything about an index, you must first start the app with index: false to drop the old index, and then restart with the correct index properties.

If a field has the unique option set to true, the MongoDB index will be a unique index as well. Then on the server, Collection2 will rely on MongoDB to check uniqueness of your field, which is more efficient than our custom checking.

1{
2  "pseudo": {
3    type: String,
4    index: true,
5    unique: true
6  }
7}

For the unique option to work, index must be true, 1, or -1. The error message for uniqueness is very generic. It's best to define your own using MyCollection.simpleSchema().messages(). The error type string is "notUnique".

You can use the sparse option along with the index and unique options to tell MongoDB to build a sparse index. By default, MongoDB will only permit one document that lacks the indexed field. By setting the sparse option to true, the index will only contain entries for documents that have the indexed field. The index skips over any document that is missing the field. This is helpful when indexing on a key in an array of sub-documents. Learn more in the MongoDB docs.

All indexes are built in the background so indexing does not block other database queries.

Contributing

Anyone is welcome to contribute. Fork, make and test your changes (meteor test-packages ./), and then submit a pull request.

Running Tests

$ cd tests
$ npm i && npm test

Running Tests in Watch Mode

$ cd tests
$ npm i && npm run test:watch

Major Contributors

@mquandalle

(Add yourself if you should be listed here.)