Skip to content

Instantly share code, notes, and snippets.

@pajtai
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pajtai/e00d191f421a4675e13a to your computer and use it in GitHub Desktop.
Save pajtai/e00d191f421a4675e13a to your computer and use it in GitHub Desktop.
mongo import / export grunt task breaking it apart into one file per document using a configurable db endpoint
/*jshint node:true*/
module.exports = function (grunt) {
'use strict';
var _ = require('lodash'),
path = require('path'),
sep = path.sep,
databases = require('./databases');
grunt.registerTask('data:deleteTemp', function() {
grunt.file.delete('.data');
});
grunt.registerTask('data:loadMeta', 'Load types and nodes to the server.',
function () {
grunt.task.run(['prompt:environment', 'data:loadMeta:continue']);
});
grunt.registerTask('data:loadMeta:continue', 'Helper task', function () {
grunt.task.run('data:load:writeMeta:grasshopper');
});
grunt.registerTask('data:load:writeMeta', function(database) {
var tasks = [];
grunt.config.set('database', database);
console.log('database: ' + database);
grunt.config.set('fixtureFolder', getFixtureFolder());
console.log('environment: ' + grunt.config.get('environment'));
grunt.config.set('mongo', getMongoConfigs(database));
_.each(databases[database + 'Meta'].collections, function (collection) {
tasks.push('data:set:collection:' + collection);
tasks.push('shell:mongoImport');
});
grunt.task.run(tasks);
});
// Load
grunt.registerTask('data:load', 'Load the data from JSON to the server. This will wipe out all existing data.',
function () {
grunt.task.run(['prompt:dataLoad', 'data:load:continue']);
});
grunt.registerTask('data:load:continue', 'Helper task', function () {
grunt.task.run('data:load:write:grasshopper');
grunt.task.run('data:load:write:ptw');
});
grunt.registerTask('data:load:write', function(database) {
var tasks = [];
grunt.config.set('database', database);
grunt.config.set('fixtureFolder', getFixtureFolder());
grunt.config.set('mongo', getMongoConfigs(database));
_.each(databases[database].collections, function (collection) {
var filepath = grunt.config.get('fixtureFolder') + '/' + collection,
arr = [];
_.each(grunt.file.expand('fixtures' + sep + filepath + sep + '*.json'), function(onePath) {
arr.push(grunt.file.readJSON(onePath));
});
grunt.file.write('.data' + sep + filepath + '.json', JSON.stringify(arr));
tasks.push('data:set:collection:' + collection);
tasks.push('shell:mongoImport');
});
tasks.push('data:deleteTemp');
grunt.task.run(tasks);
});
// Save
grunt.registerTask('data:save', 'Save data from the database to JSON.', function () {
grunt.task.run(['prompt:environment', 'data:save:continue']);
});
grunt.registerTask('data:save:continue', function () {
grunt.task.run('data:save:write:grasshopper');
grunt.task.run('data:save:write:ptw');
});
grunt.registerTask('data:save:write', function (database) {
var tasks = [];
grunt.config.set('database', database);
grunt.config.set('fixtureFolder', getFixtureFolder());
grunt.config.set('mongo', getMongoConfigs(database));
_.each(databases[database].collections, function (collection) {
console.log('setting collection to: ' + collection);
tasks.push('data:set:collection:' + collection);
tasks.push('shell:mongoExport');
tasks.push('data:prettify:collection:' + collection);
});
grunt.task.run(tasks);
});
// Utilities
grunt.registerTask('data:set:collection', 'Helper task', function (collection) {
grunt.log.writeln('collection: ' + collection);
grunt.config.set('collection', collection);
});
grunt.registerTask('data:prettify:collection', 'Helper task', function (collection) {
var filePath = grunt.config.get('fixtureFolder') + '/' + collection,
collectionData = grunt.file.readJSON('.data' + path.sep + filePath + '.json');
_.each(collectionData, function(collectionItem) {
grunt.file.write('fixtures' + path.sep + filePath + path.sep + collectionItem._id.$oid + '.json',
JSON.stringify(collectionItem, null, 4));
});
grunt.file.delete('.data');
});
function getFixtureFolder() {
return grunt.config.get('database');
}
function getMongoConfigs(database) {
var env = grunt.config.get('environment'),
config = require('../../configsApp/environments/' + env + '.json').mongo[database];
if ('production' === env) {
config.host = 'yourste.com:27017';
}
return config;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment