@janiscommerce/mongodb-index-creator
v4.1.0
Published
A package to create MongoDB indexes using Janis Models
Downloads
1,714
Readme
mongodb-index-creator
A package to create MongoDB Indexes for databases collections
:inbox_tray: Installation
npm install @janiscommerce/mongodb-index-creator
:warning: Breaking Change since 3.0.0
The package exports MongoDBIndexCreator class to be handled with @janiscommerce/lambda.
See Configuration section below.
:hammer_and_wrench: Configuration
Lambda function
'use strict';
const { Handler } = require('@janiscommerce/lambda');
const { MongoDBIndexCreator } = require('@janiscommerce/mongodb-index-creator');
module.exports.handler = (...args) => Handler.handle(MongoDBIndexCreator, ...args);
Lambda function hook
'use strict';
const { helper } = require('sls-helper'); // eslint-disable-line
const { serverlessFunction } = require('@janiscommerce/mongodb-index-creator');
module.exports = helper({
hooks: [
// other hooks
...serverlessFunction
]
});
Lambda function tests
'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'] });
});
});
Indexes configuration
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
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
}];
}
};
Index Struct
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
Examples
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'] });
})();