Skip to content

Commit

Permalink
Merge bc7796f into 1726289
Browse files Browse the repository at this point in the history
  • Loading branch information
maguimarijuan committed Apr 30, 2020
2 parents 1726289 + bc7796f commit f2061d6
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .nycrc
Expand Up @@ -4,7 +4,8 @@
"tests/",
"mocks/",
".eslintrc.js",
".travis.yml"
".travis.yml",
"serverless-function.js"
],
"extension": [
".js"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Serverless function getter
- TTL indexes support

## [1.1.3] - 2020-02-26
### Fixed
- Unexisting target DBs or collections crashes the execution
Expand Down
41 changes: 41 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,7 @@ This file is an `[Object]` export with the following structure:
- name (required): A `[String]` with the internal name of the MongoDB index
- key (required): An `[Object]` with the field and the index type for that field, for an ascending index use `1` or `-1` for a descending index
- unique (optional): `[Boolean]` Specify if the index will be unique
- expireAfterSeconds (optional): `[Number]` Indicates which documents remove from a collection after a certain amount of time or at a specific clock time

#### Clients schemas file
This file is an `[Object]` export with the following structure:
Expand All @@ -43,6 +44,7 @@ This file is an `[Object]` export with the following structure:
- name (required): A `[String]` with the internal name of the MongoDB index
- key (required): An `[Object]` with the field and the index type for that field, for an ascending index use `1` or `-1` for a descending index
- unique (optional): `[Boolean]` Specify if the index will be unique
- expireAfterSeconds (optional): `[Number]` Indicates which documents remove from a collection after a certain amount of time or at a specific clock time.

### Running the utility
```sh
Expand All @@ -64,6 +66,14 @@ module.exports = {
key: { myIndex: 1 },
unique: true
}
],

'other-collection': [
{
name: 'indicates-expiration',
key: { modificationDateIndex: 1 },
expireAfterSeconds: 3600
}
]
},

Expand Down Expand Up @@ -95,6 +105,14 @@ module.exports = {
{
key: { someIndex: 1 }
}
],

'other-collection': [
{
name: 'indicates-expiration',
key: { modificationDateIndex: 1 },
expireAfterSeconds: 3600
}
]
}
```
Expand Down Expand Up @@ -134,6 +152,10 @@ Creates the indexes for the specified client by `clientCode` using the schemas f

Creates the indexes for core and clients databases using the schemas files located in the `schemasPath`.

### **`async get serverlessFunction`**
Returns a an array that contains the serverless function that can be use at the service `serverless.js` file.


## Examples

```js
Expand Down Expand Up @@ -170,6 +192,25 @@ const mongodbIndexCreator = new MongodbIndexCreator();
})();
```

At a serverless.js file:
```js
'use strict';

const { helper } = require('sls-helper'); // eslint-disable-line
const functions = require('./serverless/functions.json');
const { MongodbIndexCreator } = require('@janiscommerce/mongodb-index-creator');


module.exports = helper({
hooks: [
// other hooks
...functions,
...MongodbIndexCreator.serverlessFunction
]
});
```


## Notes
- **If the schemas files contains indexes for databases and/or collections that not exists, they will be created in the process.**

Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/collections.js
Expand Up @@ -10,7 +10,8 @@ const collectionStruct = struct([
{
name: 'string',
key: 'object',
unique: 'boolean?'
unique: 'boolean?',
expireAfterSeconds: 'number?'
}
]);

Expand Down
6 changes: 5 additions & 1 deletion lib/mongodb-index-creator.js
Expand Up @@ -5,7 +5,7 @@ const Settings = require('@janiscommerce/settings');
const MongodbIndexCreatorError = require('./mongodb-index-creator-error');
const ModelClient = require('./model-client');
const logger = require('./colorful-lllog')();

const serverlessFunction = require('./serverless-function');
const Indexes = require('./helpers/indexes');
const Schemas = require('./helpers/schemas');
const Results = require('./helpers/results');
Expand Down Expand Up @@ -184,6 +184,10 @@ class MongodbIndexCreator {
indexesToDrop.length
);
}

static get serverlessFunction() {
return serverlessFunction;
}
}

module.exports = MongodbIndexCreator;
14 changes: 14 additions & 0 deletions lib/serverless-function.js
@@ -0,0 +1,14 @@
'use strict';

const path = require('path');

const handlerPath = path.join(process.env.MS_PATH || '', 'lambda', 'MongoDBIndexCreator', 'index.handler');

module.exports = [
'function', {
functionName: 'MongoDBIndexCreator',
handler: handlerPath,
description: 'MongoDB Indexes Creation Lambda',
timeout: 60,
package: { include: ['schemas/mongo/**'] }
}];
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions tests/mongodb-index-creator-test.js
Expand Up @@ -13,6 +13,7 @@ const Schemas = require('../lib/helpers/schemas');

const MongodbIndexCreator = require('../lib/mongodb-index-creator');
const MongodbIndexCreatorError = require('../lib/mongodb-index-creator-error');
const serverlessFunction = require('../lib/serverless-function');

require('../lib/colorful-lllog')('none');

Expand Down Expand Up @@ -202,7 +203,19 @@ describe('MongodbIndexCreator', () => {
});
});

[null, undefined, 'string', 1, { some: 'object' }, { key: { someIndex: 1 } }].forEach(indexes => {
[
null,
undefined,
'string',
1,
{ some: 'object' },
{ key: { someIndex: 1 } },
{
name: 'myIndex',
key: { myIndex: 1 },
unique: true,
expireAfterSeconds: 'not-a-number'
}].forEach(indexes => {

it('Should throw when the indexes from the received databaseKeys is not an array or not exists', async () => {

Expand Down Expand Up @@ -360,7 +373,19 @@ describe('MongodbIndexCreator', () => {
});
});

[null, undefined, 'string', 1, { some: 'object' }].forEach(indexes => {
[
null,
undefined,
'string',
1,
{ some: 'object' },
{
name: 'myIndex',
key: { myIndex: 1 },
unique: true,
expireAfterSeconds: 'not-a-number'
}
].forEach(indexes => {

it('Should throw when the indexes from the received client schemas is not an array or not exists', async () => {

Expand Down Expand Up @@ -583,4 +608,11 @@ describe('MongodbIndexCreator', () => {
sandbox.assert.calledOnce(MongodbIndexCreator.prototype.executeForClientDatabases);
});
});

describe('get serverlessFunction', () => {

it('Should return the serverless function', () => {
assert.deepStrictEqual(MongodbIndexCreator.serverlessFunction, serverlessFunction);
});
});
});

0 comments on commit f2061d6

Please sign in to comment.