@janiscommerce/mongodb-index-creator

4.1.0 • Public • Published

mongodb-index-creator

Build Status Coverage Status npm version

A package to create MongoDB Indexes for databases collections

📥 Installation

npm install @janiscommerce/mongodb-index-creator

⚠️ Breaking Change since 3.0.0

The package exports MongoDBIndexCreator class to be handled with @janiscommerce/lambda.

See Configuration section below.

🛠️ Configuration

Lambda function

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);

Lambda function hook

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
	]
});

Lambda function tests

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'] });
	});
});

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'] });

})();

Dependencies (4)

Dev Dependencies (10)

Package Sidebar

Install

npm i @janiscommerce/mongodb-index-creator

Weekly Downloads

473

Version

4.1.0

License

ISC

Unpacked Size

15.9 kB

Total Files

12

Last publish

Collaborators

  • janiscommerce