Esempio n. 1
0
  db.fetch({keys: designDocIds}, function(err, res){
    if (err) return winston.error('problem getting design docs: ' + err); 
    var docs = res.rows;
    
    if (!docs.length) return winston.info('no exising design docs found');
    
    //compile new docs
    compile(__dirname + "/design", function(err, design) {
      if (err) return winston.error('problem compiling design docs: ' + err); 
      var ids = {}, purge = [];
      for (var name in design) ids[design[name]._id] = name;

      docs.forEach(function(doc){
        if (!ids[doc.id]) purge.push({id:doc.id, rev:doc.doc._rev});
      });
      
      if (!purge.length) return winston.info("no design documents to remove");
      
      purge.forEach(function(doc) {
        db.destroy(doc.id, doc.rev, function(err, res){
          if (err) return winston.error('problem removing doc: ', doc.id, err); 
          winston.info(doc.id+" sucessfully removed");
        });        
      });
    });
  });
    function processShared(dir, next) {
      compile(dir, options, function(err, doc) {
        if (err) {
          return next(err);
        }

        _.merge(shared, doc);

        grunt.log.write('Compiling shared ' + dir + '...').ok();
        next(null, doc);
      });
    }
    function processSource(source, next) {
      compile(source, options, function(err, doc) {
        if (err) {
          return next(err);
        }

        _.merge(doc, shared);
        
        grunt.log.write('Compiling ' + source + '...').ok();
        next(null, doc);
      });
    }
db.list({ startkey:'_design/', endkey:'_e' }, function(err, res){
  if (err) return winston.error('problem getting design doc list: ' + err);

  var designDocIds = _.map(res.rows, function(row){ return row.key; });

  //compile new docs
  compile(__dirname + "/design", function(err, design) {
    if (err) return winston.error('problem compiling design docs: ' + err);

    // get design docs
    db.fetch({keys: designDocIds}, function(err, res){
      if (err) return winston.error('problem getting design docs: ' + err);
      var docs = res.rows;

      if (!docs.length) winston.info('no exising design docs found');

      var ids = {}, idsu = {}, purge = [], insert = [];
      for (var name in design) ids[design[name]._id] = name;
      for (var name in docs) idsu[docs[name].id] = name;

      docs.forEach(function(doc){
        if (!ids[doc.id]) purge.push({id:doc.id, rev:doc.doc._rev});
      });

      for (var name in design) {
        if (!idsu[design[name]._id]) insert.push(design[name]);
      }

      if (!insert.length) winston.info("no design documents to create");

      insert.forEach(function(doc) {
        db.insert(doc, doc._id, function(err, res) {
          if (err) return winston.error("problem inserting doc: ", doc._id, err);
          winston.info(doc._id + " succesfully inserted");
        });
      });

      if (!purge.length) winston.info("no design documents to remove");

      purge.forEach(function(doc) {
        db.destroy(doc.id, doc.rev, function(err, res){
          if (err) return winston.error('problem removing doc: ', doc.id, err);
          winston.info(doc.id + " sucessfully removed");
        });
      });
    });
  });
});