From abefd6b94ccb95404a3a3879b2e23749d783cf66 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 14:59:41 +0300 Subject: [PATCH 01/15] Collection to lowerCase not working _.each is not changing the value of collections. So I replaced it with _.map --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f64afa0..4e71fae 100644 --- a/index.js +++ b/index.js @@ -119,8 +119,8 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { preserveLoadOrder = false; } else { - _.each(collections, function(collection) { - collection = collection.toLowerCase(); + collections = _.map(collections, function(collection) { + return collection.toLowerCase(); }); } autoAssociations = !(autoAssociations === false); From 84c94b37832dfe4b0aafc876bd4059e8a5266864 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:29:25 +0300 Subject: [PATCH 02/15] Add support for mongoDb id's --- index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4e71fae..6f951df 100644 --- a/index.js +++ b/index.js @@ -81,11 +81,14 @@ Barrels.prototype.associate = function(collections, done) { return nextAttr(); var joined = association[association.type]; - if (!_.isArray(item[attr])) - model[attr] = that.idMap[joined][item[attr]-1]; + if (!_.isArray(item[attr])) { + var idx = that.idMap[joined].indexOf(item[attr]); + model[attr] = that.idMap[joined][idx]; + } else { for (var j = 0; j < item[attr].length; j++) { - model[attr].add(that.idMap[joined][item[attr][j]-1]); + var idx = that.idMap[joined].indexOf(item[attr][j]); + model[attr].add(that.idMap[joined][idx]); } } From 37e2bc0e32b6e84afbc9b6bcac388872038c3838 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:30:29 +0300 Subject: [PATCH 03/15] ver up --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index df7c5ec..ae1f32d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "barrels", - "version": "1.6.5", + "version": "1.6.6", "description": "Simple DB Fixtures for Sails.js with associations support", "dependencies": { "async": "^1.5.0", From f489814999c082d392bdcee3a4c8cd1374a7c187 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:42:59 +0300 Subject: [PATCH 04/15] Add assosiation to model only if the model exists --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6f951df..88b553d 100644 --- a/index.js +++ b/index.js @@ -83,11 +83,13 @@ Barrels.prototype.associate = function(collections, done) { if (!_.isArray(item[attr])) { var idx = that.idMap[joined].indexOf(item[attr]); - model[attr] = that.idMap[joined][idx]; + if(model) + model[attr] = that.idMap[joined][idx]; } else { for (var j = 0; j < item[attr].length; j++) { var idx = that.idMap[joined].indexOf(item[attr][j]); + if(model) model[attr].add(that.idMap[joined][idx]); } } From 527c5fbede31cdcc40ba81f4c5ba873c6ec89ce6 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:43:34 +0300 Subject: [PATCH 05/15] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ae1f32d..724eaa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "barrels", - "version": "1.6.6", + "version": "1.6.7", "description": "Simple DB Fixtures for Sails.js with associations support", "dependencies": { "async": "^1.5.0", From d6e6560f5e3e624c349cb8e8c62753393fcd2bcf Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:46:38 +0300 Subject: [PATCH 06/15] if no model found just skip adding assosiations --- index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 88b553d..c013e04 100644 --- a/index.js +++ b/index.js @@ -71,6 +71,9 @@ Barrels.prototype.associate = function(collections, done) { Model.findOne(that.idMap[modelName][itemIndex]).exec(function(err, model) { if (err) return nextItem(err); + + if(!model) + return nextItem(); // Pick associations only item = _.pick(item, Object.keys(that.associations[modelName])); @@ -83,14 +86,12 @@ Barrels.prototype.associate = function(collections, done) { if (!_.isArray(item[attr])) { var idx = that.idMap[joined].indexOf(item[attr]); - if(model) - model[attr] = that.idMap[joined][idx]; + model[attr] = that.idMap[joined][idx]; } else { for (var j = 0; j < item[attr].length; j++) { var idx = that.idMap[joined].indexOf(item[attr][j]); - if(model) - model[attr].add(that.idMap[joined][idx]); + model[attr].add(that.idMap[joined][idx]); } } From e43dad9309f0a4670542a6da5b6fc7297b7395a3 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Wed, 16 Aug 2017 20:47:37 +0300 Subject: [PATCH 07/15] adding myself as contributer --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 724eaa9..a0bbe56 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,10 @@ { "name": "Marnus Weststrate", "email": "marnusw@gmail.com" + }, + { + "name": "Stanislav Arshanski", + "email": "stuk88@gmail.com" } ], "license": "MIT" From 7c86c823700531983d53443046ee9a86da4fc69f Mon Sep 17 00:00:00 2001 From: stuk88 Date: Sun, 27 Aug 2017 04:16:41 +0300 Subject: [PATCH 08/15] Indentation --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c013e04..dd65250 100644 --- a/index.js +++ b/index.js @@ -90,8 +90,8 @@ Barrels.prototype.associate = function(collections, done) { } else { for (var j = 0; j < item[attr].length; j++) { - var idx = that.idMap[joined].indexOf(item[attr][j]); - model[attr].add(that.idMap[joined][idx]); + var idx = that.idMap[joined].indexOf(item[attr][j]); + model[attr].add(that.idMap[joined][idx]); } } From e20e0a0630fbba8eaf9dd96770e978112f45c753 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Sun, 27 Aug 2017 04:19:19 +0300 Subject: [PATCH 09/15] ver down --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a0bbe56..34c5ed6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "barrels", - "version": "1.6.7", + "version": "1.6.6", "description": "Simple DB Fixtures for Sails.js with associations support", "dependencies": { "async": "^1.5.0", From db5e8d3897a7fe3d49401e54fa2cf6d05d09b6f3 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Thu, 31 Aug 2017 17:16:48 +0300 Subject: [PATCH 10/15] make associations work with mongo ids Assosiations functions update + Populate function update to use find function, and add a checkes for array of ids have related objects, and not fail. --- index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index dd65250..24e8bb3 100644 --- a/index.js +++ b/index.js @@ -71,8 +71,8 @@ Barrels.prototype.associate = function(collections, done) { Model.findOne(that.idMap[modelName][itemIndex]).exec(function(err, model) { if (err) return nextItem(err); - - if(!model) + + if (!model) return nextItem(); // Pick associations only @@ -85,12 +85,14 @@ Barrels.prototype.associate = function(collections, done) { var joined = association[association.type]; if (!_.isArray(item[attr])) { - var idx = that.idMap[joined].indexOf(item[attr]); - model[attr] = that.idMap[joined][idx]; + var idx = that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr]) || false; + if (idx !== false) + model[attr] = that.idMap[joined][idx]; } else { for (var j = 0; j < item[attr].length; j++) { - var idx = that.idMap[joined].indexOf(item[attr][j]); + var idx = that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr][j]) || false; + if (idx !== false) model[attr].add(that.idMap[joined][idx]); } } @@ -167,12 +169,17 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { if (!that.idMap[collectionName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); for (var i = 0; i < item[alias].length; i++) { - item[alias][i] = that.idMap[collectionName][item[alias][i] - 1]; + var idx = that.idMap[collectionName] && Array.isArray(that.idMap[collectionName]) && that.idMap[collectionName].indexOf(item[alias]) || false; + if (idx !== false) + item[alias][i] = that.idMap[collectionName][idx]; } } else if (associatedModelName) { if (!that.idMap[associatedModelName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); - item[alias] = that.idMap[associatedModelName][item[alias] - 1]; + + var idx = that.idMap[associatedModelName] && Array.isArray(that.idMap[associatedModelName]) && that.idMap[associatedModelName].indexOf(item[alias]) || false; + if (idx !== false) + item[alias] = that.idMap[associatedModelName][idx]; } } else if (autoAssociations) { // The order is not important, so we can strip From 6a0942abf74265b58022ab49b9b7adb886d7632a Mon Sep 17 00:00:00 2001 From: stuk88 Date: Thu, 31 Aug 2017 18:22:03 +0300 Subject: [PATCH 11/15] support numerical ids (zero were leading to false) --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 24e8bb3..eae4b2d 100644 --- a/index.js +++ b/index.js @@ -85,13 +85,13 @@ Barrels.prototype.associate = function(collections, done) { var joined = association[association.type]; if (!_.isArray(item[attr])) { - var idx = that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr]) || false; + var idx = (that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr]) !== -1 ) ? that.idMap[joined].indexOf(item[attr]) : false; if (idx !== false) model[attr] = that.idMap[joined][idx]; } else { for (var j = 0; j < item[attr].length; j++) { - var idx = that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr][j]) || false; + var idx = (that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr][j]) !== -1) ? that.idMap[joined].indexOf(item[attr][j]) : false; if (idx !== false) model[attr].add(that.idMap[joined][idx]); } @@ -169,7 +169,7 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { if (!that.idMap[collectionName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); for (var i = 0; i < item[alias].length; i++) { - var idx = that.idMap[collectionName] && Array.isArray(that.idMap[collectionName]) && that.idMap[collectionName].indexOf(item[alias]) || false; + var idx = (that.idMap[collectionName] && Array.isArray(that.idMap[collectionName]) && that.idMap[collectionName].indexOf(item[alias]) !== -1) ? that.idMap[collectionName].indexOf(item[alias]) : false; if (idx !== false) item[alias][i] = that.idMap[collectionName][idx]; } @@ -177,7 +177,7 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { if (!that.idMap[associatedModelName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); - var idx = that.idMap[associatedModelName] && Array.isArray(that.idMap[associatedModelName]) && that.idMap[associatedModelName].indexOf(item[alias]) || false; + var idx = (that.idMap[associatedModelName] && Array.isArray(that.idMap[associatedModelName]) && that.idMap[associatedModelName].indexOf(item[alias]) !== -1) ? that.idMap[associatedModelName].indexOf(item[alias]) : false; if (idx !== false) item[alias] = that.idMap[associatedModelName][idx]; } From ff4465a4d7fcdf672cb114474565f8c63d035ca0 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Thu, 31 Aug 2017 21:37:12 +0300 Subject: [PATCH 12/15] fix bug with numeric ids --- index.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index eae4b2d..7421802 100644 --- a/index.js +++ b/index.js @@ -85,15 +85,15 @@ Barrels.prototype.associate = function(collections, done) { var joined = association[association.type]; if (!_.isArray(item[attr])) { - var idx = (that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr]) !== -1 ) ? that.idMap[joined].indexOf(item[attr]) : false; - if (idx !== false) - model[attr] = that.idMap[joined][idx]; + var id = getId(that.idMap[joined], item[attr]); + if (id !== false) + model[attr] = id; } else { for (var j = 0; j < item[attr].length; j++) { - var idx = (that.idMap[joined] && Array.isArray(that.idMap[joined]) && that.idMap[joined].indexOf(item[attr][j]) !== -1) ? that.idMap[joined].indexOf(item[attr][j]) : false; - if (idx !== false) - model[attr].add(that.idMap[joined][idx]); + var id = getId(that.idMap[joined], item[attr][j]); + if (id !== false) + model[attr].add(id); } } @@ -169,17 +169,18 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { if (!that.idMap[collectionName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); for (var i = 0; i < item[alias].length; i++) { - var idx = (that.idMap[collectionName] && Array.isArray(that.idMap[collectionName]) && that.idMap[collectionName].indexOf(item[alias]) !== -1) ? that.idMap[collectionName].indexOf(item[alias]) : false; - if (idx !== false) - item[alias][i] = that.idMap[collectionName][idx]; + var id = getId(that.idMap[collectionName], item[alias][i]); + if (id !== false) + item[alias][i] = id; } } else if (associatedModelName) { if (!that.idMap[associatedModelName]) return nextItem(new Error('Please provide a loading order acceptable for required associations')); - var idx = (that.idMap[associatedModelName] && Array.isArray(that.idMap[associatedModelName]) && that.idMap[associatedModelName].indexOf(item[alias]) !== -1) ? that.idMap[associatedModelName].indexOf(item[alias]) : false; - if (idx !== false) - item[alias] = that.idMap[associatedModelName][idx]; + + var id = getId(that.idMap[associatedModelName], item[alias]); + if (id !== false) + item[alias] = id; } } else if (autoAssociations) { // The order is not important, so we can strip @@ -189,15 +190,16 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { } // Insert - Model.create(item).exec(function(err, model) { + Model.create(item).exec(function(item, err, model) { if (err) return nextItem(err); + // Primary key mapping that.idMap[modelName][itemIndex] = model[Model.primaryKey]; nextItem(); - }); + }.bind(this, item)); }, nextModel); }); } else { @@ -214,3 +216,14 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { done(); }); }; + + +function getId(collection, id) { + if(!(collection && Array.isArray(collection))) + return false; + if(Number.isInteger(id)) + return collection[id - 1]; + + if(collection.indexOf(id) !== -1 ) + return collection[collection.indexOf(id)] +} From 2526aa92b21bd78999dd7552b1c36c4a0690f591 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Thu, 31 Aug 2017 21:38:14 +0300 Subject: [PATCH 13/15] add space between if and brackets --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7421802..6a5df9a 100644 --- a/index.js +++ b/index.js @@ -219,11 +219,11 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { function getId(collection, id) { - if(!(collection && Array.isArray(collection))) + if (!(collection && Array.isArray(collection))) return false; - if(Number.isInteger(id)) + if (Number.isInteger(id)) return collection[id - 1]; - if(collection.indexOf(id) !== -1 ) + if (collection.indexOf(id) !== -1 ) return collection[collection.indexOf(id)] } From b9ea6d3c917065f77daf1c7ec3f2619aca326785 Mon Sep 17 00:00:00 2001 From: stuk88 Date: Thu, 31 Aug 2017 21:42:57 +0300 Subject: [PATCH 14/15] Number.isInteger not support switching to !isNaN --- index.js | 342 +++++++++++++++++++++++++++---------------------------- 1 file changed, 171 insertions(+), 171 deletions(-) diff --git a/index.js b/index.js index 6a5df9a..ad3b4e2 100644 --- a/index.js +++ b/index.js @@ -20,31 +20,31 @@ module.exports = Barrels; * @param {string} sourceFolder defaults to /test/fixtures */ function Barrels(sourceFolder) { - if (!(this instanceof Barrels)) - return new Barrels(sourceFolder); + if (!(this instanceof Barrels)) + return new Barrels(sourceFolder); - // Fixture objects loaded from the JSON files - this.data = {}; + // Fixture objects loaded from the JSON files + this.data = {}; - // Map fixture positions in JSON files to the real DB IDs - this.idMap = {}; + // Map fixture positions in JSON files to the real DB IDs + this.idMap = {}; - // The list of associations by model - this.associations = {}; + // The list of associations by model + this.associations = {}; - // Load the fixtures - sourceFolder = sourceFolder || process.cwd() + '/test/fixtures'; - var files = fs.readdirSync(sourceFolder); + // Load the fixtures + sourceFolder = sourceFolder || process.cwd() + '/test/fixtures'; + var files = fs.readdirSync(sourceFolder); - for (var i = 0; i < files.length; i++) { - if (['.json', '.js'].indexOf(path.extname(files[i]).toLowerCase()) !== -1) { - var modelName = path.basename(files[i]).split('.')[0].toLowerCase(); - this.data[modelName] = require(path.join(sourceFolder, files[i])); + for (var i = 0; i < files.length; i++) { + if (['.json', '.js'].indexOf(path.extname(files[i]).toLowerCase()) !== -1) { + var modelName = path.basename(files[i]).split('.')[0].toLowerCase(); + this.data[modelName] = require(path.join(sourceFolder, files[i])); + } } - } - // The list of the fixtures model names - this.modelNames = Object.keys(this.data); + // The list of the fixtures model names + this.modelNames = Object.keys(this.data); } /** @@ -52,64 +52,64 @@ function Barrels(sourceFolder) { * @param {function} done callback */ Barrels.prototype.associate = function(collections, done) { - if (!_.isArray(collections)) { - done = collections; - collections = this.modelNames; - } - var that = this; - - // Add associations whenever needed - async.each(collections, function(modelName, nextModel) { - var Model = sails.models[modelName]; - if (Model) { - var fixtureObjects = _.cloneDeep(that.data[modelName]); - async.each(fixtureObjects, function(item, nextItem) { - // Item position in the file - var itemIndex = fixtureObjects.indexOf(item); - - // Find and associate - Model.findOne(that.idMap[modelName][itemIndex]).exec(function(err, model) { - if (err) - return nextItem(err); - - if (!model) - return nextItem(); - - // Pick associations only - item = _.pick(item, Object.keys(that.associations[modelName])); - async.each(Object.keys(item), function(attr, nextAttr) { - var association = that.associations[modelName][attr]; - // Required associations should have beed added earlier - if (association.required) - return nextAttr(); - var joined = association[association.type]; - - if (!_.isArray(item[attr])) { - var id = getId(that.idMap[joined], item[attr]); - if (id !== false) - model[attr] = id; - } - else { - for (var j = 0; j < item[attr].length; j++) { - var id = getId(that.idMap[joined], item[attr][j]); - if (id !== false) - model[attr].add(id); - } - } - - model.save(function(err) { - if (err) - return nextAttr(err); - - nextAttr(); - }); - }, nextItem); - }); - }, nextModel); - } else { - nextModel(); + if (!_.isArray(collections)) { + done = collections; + collections = this.modelNames; } - }, done); + var that = this; + + // Add associations whenever needed + async.each(collections, function(modelName, nextModel) { + var Model = sails.models[modelName]; + if (Model) { + var fixtureObjects = _.cloneDeep(that.data[modelName]); + async.each(fixtureObjects, function(item, nextItem) { + // Item position in the file + var itemIndex = fixtureObjects.indexOf(item); + + // Find and associate + Model.findOne(that.idMap[modelName][itemIndex]).exec(function(err, model) { + if (err) + return nextItem(err); + + if (!model) + return nextItem(); + + // Pick associations only + item = _.pick(item, Object.keys(that.associations[modelName])); + async.each(Object.keys(item), function(attr, nextAttr) { + var association = that.associations[modelName][attr]; + // Required associations should have beed added earlier + if (association.required) + return nextAttr(); + var joined = association[association.type]; + + if (!_.isArray(item[attr])) { + var id = getId(that.idMap[joined], item[attr]); + if (id !== false) + model[attr] = id; + } + else { + for (var j = 0; j < item[attr].length; j++) { + var id = getId(that.idMap[joined], item[attr][j]); + if (id !== false) + model[attr].add(id); + } + } + + model.save(function(err) { + if (err) + return nextAttr(err); + + nextAttr(); + }); + }, nextItem); + }); + }, nextModel); + } else { + nextModel(); + } + }, done); }; /** @@ -119,110 +119,110 @@ Barrels.prototype.associate = function(collections, done) { * @param {boolean} autoAssociations automatically associate based on the order in the fixture files */ Barrels.prototype.populate = function(collections, done, autoAssociations) { - var preserveLoadOrder = true; - if (!_.isArray(collections)) { - autoAssociations = done; - done = collections; - collections = this.modelNames; - preserveLoadOrder = false; - } - else { - collections = _.map(collections, function(collection) { - return collection.toLowerCase(); - }); - } - autoAssociations = !(autoAssociations === false); - var that = this; - - // Populate each table / collection - async[preserveLoadOrder ? 'eachSeries' : 'each'](collections, function(modelName, nextModel) { - var Model = sails.models[modelName]; - if (Model) { - // Cleanup existing data in the table / collection - Model.destroy().exec(function(err) { - if (err) - return nextModel(err); - - // Save model's association information - that.associations[modelName] = {}; - for (var i = 0; i < Model.associations.length; i++) { - var alias = Model.associations[i].alias; - that.associations[modelName][alias] = Model.associations[i]; - that.associations[modelName][alias].required = !!(Model._validator.validations[alias].required); - } - - // Insert all the fixture items - that.idMap[modelName] = []; - var fixtureObjects = _.cloneDeep(that.data[modelName]); - async.each(fixtureObjects, function(item, nextItem) { - // Item position in the file - var itemIndex = fixtureObjects.indexOf(item); - - for (var alias in that.associations[modelName]) { - if (that.associations[modelName][alias].required) { - // With required associations present, the associated fixtures - // must be already loaded, so we can map the ids - var collectionName = that.associations[modelName][alias].collection; // many-to-many - var associatedModelName = that.associations[modelName][alias].model; // one-to-many - - if ((_.isArray(item[alias]))&&(collectionName)) { - if (!that.idMap[collectionName]) - return nextItem(new Error('Please provide a loading order acceptable for required associations')); - for (var i = 0; i < item[alias].length; i++) { - var id = getId(that.idMap[collectionName], item[alias][i]); - if (id !== false) - item[alias][i] = id; - } - } else if (associatedModelName) { - if (!that.idMap[associatedModelName]) - return nextItem(new Error('Please provide a loading order acceptable for required associations')); - - - var id = getId(that.idMap[associatedModelName], item[alias]); - if (id !== false) - item[alias] = id; - } - } else if (autoAssociations) { - // The order is not important, so we can strip - // associations data and associate later - item = _.omit(item, alias); - } - } - - // Insert - Model.create(item).exec(function(item, err, model) { - if (err) - return nextItem(err); - - - // Primary key mapping - that.idMap[modelName][itemIndex] = model[Model.primaryKey]; - - nextItem(); - }.bind(this, item)); - }, nextModel); - }); - } else { - nextModel(); + var preserveLoadOrder = true; + if (!_.isArray(collections)) { + autoAssociations = done; + done = collections; + collections = this.modelNames; + preserveLoadOrder = false; } - }, function(err) { - if (err) - return done(err); + else { + collections = _.map(collections, function(collection) { + return collection.toLowerCase(); + }); + } + autoAssociations = !(autoAssociations === false); + var that = this; + + // Populate each table / collection + async[preserveLoadOrder ? 'eachSeries' : 'each'](collections, function(modelName, nextModel) { + var Model = sails.models[modelName]; + if (Model) { + // Cleanup existing data in the table / collection + Model.destroy().exec(function(err) { + if (err) + return nextModel(err); + + // Save model's association information + that.associations[modelName] = {}; + for (var i = 0; i < Model.associations.length; i++) { + var alias = Model.associations[i].alias; + that.associations[modelName][alias] = Model.associations[i]; + that.associations[modelName][alias].required = !!(Model._validator.validations[alias].required); + } + + // Insert all the fixture items + that.idMap[modelName] = []; + var fixtureObjects = _.cloneDeep(that.data[modelName]); + async.each(fixtureObjects, function(item, nextItem) { + // Item position in the file + var itemIndex = fixtureObjects.indexOf(item); + + for (var alias in that.associations[modelName]) { + if (that.associations[modelName][alias].required) { + // With required associations present, the associated fixtures + // must be already loaded, so we can map the ids + var collectionName = that.associations[modelName][alias].collection; // many-to-many + var associatedModelName = that.associations[modelName][alias].model; // one-to-many + + if ((_.isArray(item[alias]))&&(collectionName)) { + if (!that.idMap[collectionName]) + return nextItem(new Error('Please provide a loading order acceptable for required associations')); + for (var i = 0; i < item[alias].length; i++) { + var id = getId(that.idMap[collectionName], item[alias][i]); + if (id !== false) + item[alias][i] = id; + } + } else if (associatedModelName) { + if (!that.idMap[associatedModelName]) + return nextItem(new Error('Please provide a loading order acceptable for required associations')); + + + var id = getId(that.idMap[associatedModelName], item[alias]); + if (id !== false) + item[alias] = id; + } + } else if (autoAssociations) { + // The order is not important, so we can strip + // associations data and associate later + item = _.omit(item, alias); + } + } + + // Insert + Model.create(item).exec(function(item, err, model) { + if (err) + return nextItem(err); + + + // Primary key mapping + that.idMap[modelName][itemIndex] = model[Model.primaryKey]; + + nextItem(); + }.bind(this, item)); + }, nextModel); + }); + } else { + nextModel(); + } + }, function(err) { + if (err) + return done(err); - // Create associations if requested - if (autoAssociations) - return that.associate(collections, done); + // Create associations if requested + if (autoAssociations) + return that.associate(collections, done); - done(); - }); + done(); + }); }; function getId(collection, id) { if (!(collection && Array.isArray(collection))) - return false; - if (Number.isInteger(id)) - return collection[id - 1]; + return false; + if (!isNaN(id)) + return collection[id - 1]; if (collection.indexOf(id) !== -1 ) return collection[collection.indexOf(id)] From 7b6292381d9352b2879ce81205a97d763f7d810f Mon Sep 17 00:00:00 2001 From: Stas Arshanski Date: Mon, 16 Apr 2018 17:53:32 +0300 Subject: [PATCH 15/15] Make models load in series (so we could test models) --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ad3b4e2..548351c 100644 --- a/index.js +++ b/index.js @@ -154,7 +154,7 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) { // Insert all the fixture items that.idMap[modelName] = []; var fixtureObjects = _.cloneDeep(that.data[modelName]); - async.each(fixtureObjects, function(item, nextItem) { + async.eachSeries(fixtureObjects, function(item, nextItem) { // Item position in the file var itemIndex = fixtureObjects.indexOf(item);