Skip to content

Commit

Permalink
Applied PR corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Jan 16, 2020
1 parent d5ca443 commit 7cab4e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
43 changes: 31 additions & 12 deletions lib/helpers/indexes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

const Model = require('./model');
const Model = require('./model-generator');

const DEFAULT_ID_INDEX_NAME = '_id_';

class IndexesHelper {

static prepareCoreIndexes(coreSchemas) {
return this._generateModels(Object.entries(coreSchemas), Model.getInstanceByDatabaseKey);
return this._generateCoreModels(coreSchemas);
}

static prepareClientIndexes(clients, clientSchemas) {
return this._generateModels(clients.map(client => [client, clientSchemas]), Model.getSessionInstance);
return this._generateClientModels(clients, clientSchemas);
}

static async get(modelInstance) {
Expand All @@ -21,23 +21,42 @@ class IndexesHelper {
return indexes.filter(({ name }) => name !== DEFAULT_ID_INDEX_NAME);
}

static getIndexesDifference(indexesA, indexesB) {
static difference(indexesA, indexesB) {
return indexesA.filter(indexA => !indexesB.some(({ name }) => indexA.name === name));
}

static _generateModels(formattedIndexes, handler) {
static _generateCoreModels(schemas) {

return formattedIndexes.reduce((prev, [key, collections]) => {
return Object.entries(schemas).reduce((models, [databaseKey, collections]) => {

return [
...prev,
models.push(
...Object.entries(collections).map(([collection, indexes]) => (
{
modelInstance: handler(key, collection),
modelInstance: Model.getInstanceByDatabaseKey(databaseKey, collection),
indexes
})
)
];
}
))
);

return models;

}, []);
}

static _generateClientModels(clients, collections) {

return clients.reduce((models, client) => {

models.push(
...Object.entries(collections).map(([collection, indexes]) => (
{
modelInstance: Model.getSessionInstance(client, collection),
indexes
}
))
);

return models;

}, []);
}
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions lib/mongodb-index-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ class MongodbIndexCreator {

async _createIndexes(modelInstance, currentIndexes, indexes) {

const indexesToCreate = Indexes.getIndexesDifference(indexes, currentIndexes);

if(!indexesToCreate.length)
return Results.save('skipped', indexes.length);
const indexesToCreate = Indexes.difference(indexes, currentIndexes);

Results.save('skipped', indexes.length - indexesToCreate.length);

if(!indexesToCreate.length)
return;

const result = await modelInstance.createIndexes(indexesToCreate);

Results.save(
Expand All @@ -170,7 +170,7 @@ class MongodbIndexCreator {

async _dropIndexes(modelInstance, currentIndexes, indexes) {

const indexesToDrop = Indexes.getIndexesDifference(currentIndexes, indexes);
const indexesToDrop = Indexes.difference(currentIndexes, indexes);

if(!indexesToDrop.length)
return;
Expand Down

0 comments on commit 7cab4e3

Please sign in to comment.