Skip to content

Instantly share code, notes, and snippets.

@PatrickHeneise
Created February 27, 2016 14:47
Show Gist options
  • Save PatrickHeneise/910c5b8e4570304fb5b9 to your computer and use it in GitHub Desktop.
Save PatrickHeneise/910c5b8e4570304fb5b9 to your computer and use it in GitHub Desktop.
requirable index.js file to return an array of all .js and .json files within a directory
'use strict';
var fs = require('fs');
var contents = [];
var files = fs.readdirSync(__dirname);
files.reduce(function (error, key) {
var file = key.replace(/.json|.js/g, '');
if (file !== 'index') {
contents.push(require('./' + key));
}
});
module.exports = exports = contents;
@PatrickHeneise
Copy link
Author

put all .json files into a data folder and add the index.js from above.

var data = require('../couchdb/data');

var insertData = function (db) {
  var promises = [];

  data.forEach(file => {
    file.forEach(obj => {
      var promise = new Promise((resolve, reject) => {
        db.insert(obj, obj._id, function (error) {
          if (error) {
            reject(error);
          } else {
            resolve(db);
          }
        });
      });

      promises.push(promise);
    });
  });

  return Promise.all(promises).then(() => db);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment