@janiscommerce/mongodb-index-creator

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

})();

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.1.0-beta.10beta
4.1.1224latest

Version History

VersionDownloads (Last 7 Days)Published
4.1.1224
4.1.016
4.1.0-beta.10
4.1.0-beta.00
4.0.0203
3.0.10
3.0.00
2.5.00
2.4.20
2.4.10
2.4.00
2.3.30
2.3.20
2.3.10
2.3.00
2.2.30
2.2.20
2.2.10
2.2.00
2.1.20
2.1.10
2.1.00
2.0.10
2.0.00
1.2.10
1.2.00
1.1.30
1.1.20
1.1.10
1.1.00
1.0.00

Package Sidebar

Install

npm i @janiscommerce/mongodb-index-creator

Weekly Downloads

443

Version

4.1.1

License

ISC

Unpacked Size

16.1 kB

Total Files

12

Last publish

Collaborators

  • janiscommerce