-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.min.js
1 lines (1 loc) · 3.93 KB
/
index.min.js
1
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n])}return e},_createClass=function(){function n(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,t,o){return t&&n(e.prototype,t),o&&n(e,o),e}}(),_en=require("faker/locale/en"),_en2=_interopRequireDefault(_en);function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var Seeder=function(){function o(e,t){_classCallCheck(this,o),e||this.throwSeederError("Please supply a MongoDB collection instance to seed."),this.isValidMongoDBCollection(e)||this.throwSeederError('Value passed for "collection" is not a valid MongoDB collection.'),t||this.throwSeederError("Please supply options for seeding."),t.environments||this.throwSeederError("Must pass an array of environments where seeding is allowed."),this.environmentAllowed(t.environments)?(t.data||this.throwSeederError("Must pass a data object with static array, dynamic object, or both."),!t.data||t.data.static||t.data.dynamic||this.throwSeederError("Must assign a static array or dynamic object to data in options."),this.collection=e,this.options=_extends({resetCollection:!1,seedIfExistingData:!1},t),Meteor&&Meteor.isServer?(this.options.resetCollection&&this.resetCollection(),this.seedCollection()):this.throwSeederError("Seeder is only intended to be run in a Meteor server environment.")):this.throwSeederError("Seeding not allowed in this environment.",!0)}return _createClass(o,[{key:"isValidMongoDBCollection",value:function(e){return!!(e&&e._driver&&e._driver.mongo)}},{key:"throwSeederError",value:function(e,t){var o="[@cleverbeagle/seeder] "+e+" See http://cleverbeagle.com/packages/seeder/v2 for usage instructions.";if(!t)throw new Error(o);console.warn(o)}},{key:"environmentAllowed",value:function(e){return-1<e.indexOf(process.env.NODE_ENV)}},{key:"resetCollection",value:function(){this.collection.remove({})}},{key:"seedCollection",value:function(){!this.options.seedIfExistingData&&this.collectionHasExistingData(this.collection)||(this.options.data.static&&this.seedCollectionWithStaticData(this.options.data.static),this.options.data.dynamic&&this.seedCollectionWithDynamicData(this.options.data.dynamic))}},{key:"collectionHasExistingData",value:function(e,t){var o=this.collection.find().count();return t?t<=o:0<o}},{key:"seedCollectionWithStaticData",value:function(e){var t=this;e instanceof Array||this.throwSeederError("Only an array can be passed to the static option."),e.forEach(function(e){t.createDataItem(e)})}},{key:"isUsersCollection",value:function(){return"users"===this.collection._name}},{key:"createDataItem",value:function(e){var t=void 0,o=e&&e.dependentData;(t=this.isUsersCollection()?this.createUser(e):this.collection.insert(e))&&o&&o(t)}},{key:"createUser",value:function(e){var t=e,o=[{"emails.address":t.email}];if(t.username&&o.push({username:t.username}),!this.collection.findOne({$or:o})){var n=t.roles;n&&delete t.roles;var r=Accounts.createUser(t);return n&&"undefined"!==Roles&&Roles.addUsersToRoles(r,n),r}}},{key:"seedCollectionWithDynamicData",value:function(e){"[object Object]"!==Object.prototype.toString.call(e)&&this.throwSeederError("Only an object can be passed to the dynamic option."),e&&e.count&&"number"!=typeof e.count&&this.throwSeederError("count property defined on the object passed to the dynamic option must be a number."),e&&e.seed&&"function"!=typeof e.seed&&this.throwSeederError("seed property defined on the object passed to the dynamic option must be a function.");for(var t=0;t<e.count;t+=1){var o=e.seed(t,_en2.default);this.createDataItem(o)}}}]),o}();exports.default=function(e,t){return new Seeder(e,t)};