Created
September 17, 2013 17:59
-
-
Save jgable/6598091 to your computer and use it in GitHub Desktop.
Ghost Magical Export thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!process.env.NODE_ENV) { | |
console.log("Please explicitly set your node environment before running this command: > NODE_ENV=production node exporter.js"); | |
return; | |
} | |
var fs = require('fs'), | |
path = require('path'), | |
_ = require('underscore'), | |
when = require('when'), | |
config = require('./config'), | |
knex = require('./core/server/models/base').Knex; | |
// Get all table names | |
knex.Raw("SELECT * FROM sqlite_master WHERE type='table';").then(function (data) { | |
// Select all data from each table | |
return when.map(data, function (tableInfo) { | |
return knex(tableInfo.name).select().then(function (data) { | |
return when.resolve({ | |
name: tableInfo.name, | |
data: data | |
}); | |
}); | |
}); | |
}).then(function (tablesAndData) { | |
// Aggregate up data onto big object | |
return when.resolve(_.reduce(tablesAndData, function (aggregate, table) { | |
if (table.name && table.name.slice(0, 6) !== 'sqlite') { | |
aggregate[table.name] = table.data; | |
} | |
return aggregate; | |
}, {})); | |
}).then(function (aggregatedData) { | |
var fileName = 'exported-' + process.env.NODE_ENV + '.json'; | |
if (aggregatedData && aggregatedData.settings) { | |
} | |
fs.writeFile(fileName, JSON.stringify(aggregatedData, null, 2), function (err) { | |
if (err) { return when.reject(err); } | |
console.log("Exported to " + fileName); | |
process.exit(0); | |
}); | |
}).otherwise(function (e) { | |
console.log("Error occurred: " + e.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment