function seed(doc, options) {
  var Product = mongoose.model('Product');

  return new Promise(function (resolve, reject) {

    skipDocument()
      .then(findAdminUser)
      .then(add)
      .then(function (response) {
        return resolve(response);
      })
      .catch(function (err) {
        return reject(err);
      });

    function findAdminUser(skip) {
      var User = mongoose.model('User');

      return new Promise(function (resolve, reject) {
        if (skip) {
          return resolve(true);
        }

        User
          .findOne({
            roles: { $in: ['admin'] }
          })
          .exec(function (err, admin) {
            if (err) {
              return reject(err);
            }

            doc.user = admin;

            return resolve();
          });
      });
    }

    function skipDocument() {
      return new Promise(function (resolve, reject) {
        Product
          .findOne({
            title: doc.title
          })
          .exec(function (err, existing) {
            if (err) {
              return reject(err);
            }

            if (!existing) {
              return resolve(false);
            }

            if (existing && !options.overwrite) {
              return resolve(true);
            }

            // Remove Article (overwrite)

            existing.remove(function (err) {
              if (err) {
                return reject(err);
              }

              return resolve(false);
            });
          });
      });
    }

    function add(skip) {
      return new Promise(function (resolve, reject) {
        if (skip) {
          return resolve({
            message: chalk.yellow('Database Seeding: Product\t' + doc.title + ' skipped')
          });
        }

        var product = new Product(doc);

        product.save(function (err) {
          if (err) {
            return reject(err);
          }

          return resolve({
            message: 'Database Seeding: Product\t' + product.name + ' added'
          });
        });
      });
    }
  });
}
/**
* Seeds the User collection with document (Article)
* and provided options.
*/
Seeds the User collection with document (Article)
and provided options.
Seeds the User collection with document ( Article ) and provided options .
function seed(doc, options) {
  var Product = mongoose.model('Product');
  return new Promise(function (resolve, reject) {
    skipDocument()
      .then(findAdminUser)
      .then(add)
      .then(function (response) {
        return resolve(response);
      })
      .catch(function (err) {
        return reject(err);
      });
    function findAdminUser(skip) {
      var User = mongoose.model('User');
      return new Promise(function (resolve, reject) {
        if (skip) {
          return resolve(true);
        }
        User
          .findOne({
            roles: { $in: ['admin'] }
          })
          .exec(function (err, admin) {
            if (err) {
              return reject(err);
            }
            doc.user = admin;
            return resolve();
          });
      });
    }
    function skipDocument() {
      return new Promise(function (resolve, reject) {
        Product
          .findOne({
            title: doc.title
          })
          .exec(function (err, existing) {
            if (err) {
              return reject(err);
            }
            if (!existing) {
              return resolve(false);
            }
            if (existing && !options.overwrite) {
              return resolve(true);
            }
            existing.remove(function (err) {
              if (err) {
                return reject(err);
              }
              return resolve(false);
            });
          });
      });
    }
    function add(skip) {
      return new Promise(function (resolve, reject) {
        if (skip) {
          return resolve({
            message: chalk.yellow('Database Seeding: Product\t' + doc.title + ' skipped')
          });
        }
        var product = new Product(doc);
        product.save(function (err) {
          if (err) {
            return reject(err);
          }
          return resolve({
            message: 'Database Seeding: Product\t' + product.name + ' added'
          });
        });
      });
    }
  });
}
function seed ( doc , options ) { var Product = mongoose . model ( 'Product' ) ; return new Promise ( function ( resolve , reject ) { skipDocument ( ) . then ( findAdminUser ) . then ( add ) . then ( function ( response ) { return resolve ( response ) ; } ) . catch ( function ( err ) { return reject ( err ) ; } ) ; function findAdminUser ( skip ) { var User = mongoose . model ( 'User' ) ; return new Promise ( function ( resolve , reject ) { if ( skip ) { return resolve ( true ) ; } User . findOne ( { roles : { $in : [ 'admin' ] } } ) . exec ( function ( err , admin ) { if ( err ) { return reject ( err ) ; } doc . user = admin ; return resolve ( ) ; } ) ; } ) ; } function skipDocument ( ) { return new Promise ( function ( resolve , reject ) { Product . findOne ( { title : doc . title } ) . exec ( function ( err , existing ) { if ( err ) { return reject ( err ) ; } if ( ! existing ) { return resolve ( false ) ; } if ( existing && ! options . overwrite ) { return resolve ( true ) ; } existing . remove ( function ( err ) { if ( err ) { return reject ( err ) ; } return resolve ( false ) ; } ) ; } ) ; } ) ; } function add ( skip ) { return new Promise ( function ( resolve , reject ) { if ( skip ) { return resolve ( { message : chalk . yellow ( 'Database Seeding: Product\t' + doc . title + ' skipped' ) } ) ; } var product = new Product ( doc ) ; product . save ( function ( err ) { if ( err ) { return reject ( err ) ; } return resolve ( { message : 'Database Seeding: Product\t' + product . name + ' added' } ) ; } ) ; } ) ; } } ) ; }
Seeds the User collection with document (Article)
and provided options.
Seeds the User collection with document ( Article ) and provided options .
// Remove Article (overwrite)