A package to create MongoDB Indexes for databases collections
npm install @janiscommerce/mongodb-index-creator
The package exports MongoDBIndexCreator class to be handled with @janiscommerce/lambda.
See Configuration section below.
Adding the lambda function at src/lambda/MongoDBIndexCreator/index.js
'use strict';
const { Handler } = require('@janiscommerce/lambda');
const { MongoDBIndexCreator } = require('@janiscommerce/mongodb-index-creator');
module.exports.handler = (...args) => Handler.handle(MongoDBIndexCreator, ...args);
Register the lambda function at serverless.js file
'use strict';
const { helper } = require('sls-helper'); // eslint-disable-line
const { serverlessFunction } = require('@janiscommerce/mongodb-index-creator');
module.exports = helper({
hooks: [
// other hooks
...serverlessFunction
]
});
Add tests for the function added tests/lambda/MongoDBIndexCreator/index.js
'use strict';
const sinon = require('sinon');
const { Handler } = require('@janiscommerce/lambda');
const { MongoDBIndexCreator } = require('@janiscommerce/mongodb-index-creator');
const { handler: FunctionHandler } = require('../../../src/lambda/MongoDBIndexCreator');
describe('MongoDBIndexCreator', () => {
beforeEach(() => sinon.stub(Handler, 'handle').resolves());
afterEach(() => sinon.restore());
it('Should handle with lambda handler when no payload was received', async () => {
await FunctionHandler();
sinon.assert.calledOnceWithExactly(Handler.handle, MongoDBIndexCreator);
});
it('Should handle with lambda handler when a client was received in payload', async () => {
await FunctionHandler({ clientCode: 'my-client' });
sinon.assert.calledOnceWithExactly(Handler.handle, MongoDBIndexCreator, { clientCode: 'my-client' });
});
it('Should handle with lambda handler when multiple client was received in payload', async () => {
await FunctionHandler({ clientCode: ['my-client', 'other-client'] });
sinon.assert.calledOnceWithExactly(Handler.handle, MongoDBIndexCreator, { clientCode: ['my-client', 'other-client'] });
});
});
This package uses models for maintain indexes, creating or dropping if needs.
If you need more information about how to configure a model, please check the following docs: @janiscommerce/model
In every model we need to add a static indexes
getter to provide the indexes for that model (that will apply for the collection associated to the model).
'use strict';
const Model = require('@janiscommerce/model');
module.exports = class Pet extends Model {
static get table() {
return 'pets';
}
static get indexes() {
return [{
name: 'code',
key: { code: 1 },
unique: true
}];
}
};
Each Index object in the indexes
getter will be validated according the following struct
name: 'string'
key: 'object'
unique: 'boolean?' # optional
sparse: 'boolean?' # optional
expireAfterSeconds: 'number?' # optional
partialFilterExpression: 'object?' # optional
For more information see MongoDB Indexes
const { Invoker } = require('@janiscommerce/lambda');
(async () => {
// execute for core and all clients databases
await Invoker.call('MongoDBIndexCreator');
// execute for specified client
await Invoker.call('MongoDBIndexCreator', { clientCode: 'some-client' });
// execute for multiple clients
await Invoker.call('MongoDBIndexCreator', { clientCode: ['some-client', 'other-client'] });
})();