From 52fa553d8fa3ca6fe458f731e5ea30b33394367e Mon Sep 17 00:00:00 2001 From: catarak Date: Sun, 4 Sep 2016 16:39:12 -0400 Subject: [PATCH] add script to reformat database --- server/utils/db_reformat.js | 68 +++++++++++++++++++++++++++++++ server/utils/db_reformat_start.js | 3 ++ 2 files changed, 71 insertions(+) create mode 100644 server/utils/db_reformat.js create mode 100644 server/utils/db_reformat_start.js diff --git a/server/utils/db_reformat.js b/server/utils/db_reformat.js new file mode 100644 index 00000000..a709ca02 --- /dev/null +++ b/server/utils/db_reformat.js @@ -0,0 +1,68 @@ +import mongoose from 'mongoose'; +const ObjectId = mongoose.Types.ObjectId; +mongoose.connect('mongodb://localhost:27017/p5js-web-editor'); +mongoose.connection.on('error', () => { + console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); + process.exit(1); +}); + +import Project from '../models/project'; + +let projectsNotToUpdate; +Project.find({'files.name': 'root'}) + .exec((err, projects) => { + projectsNotToUpdate = projects.map(project => project.id); + console.log(projectsNotToUpdate); + + Project.find({}) + .exec((err, projects) => { + projects.forEach( (project, projectIndex) => { + if (!projectsNotToUpdate.find(projectId => projectId === project.id)) { + const childIdArray = project.files.map(file => file._id.valueOf()); + const newId = new ObjectId(); + project.files.push({ + name: 'root', + _id: newId, + id: newId, + fileType: 'folder', + children: childIdArray, + content: '' + }); + + project.files = project.files.map(file => { + if (file.name === "sketch.js") { + file.isSelected = true; + return file; + } + return file; + }); + project.save((err, savedProject) => { + console.log('project', projectIndex, 'is saved.'); + }); + } + }); + }); + }); + +// Project.find({'files.name': 'root'}) +// .exec((err, projects) => { +// projects.forEach((project, projectIndex) => { +// project.files = project.files.map(file => { +// if (file.name === "sketch.js") { +// file.isSelected = true; +// return file; +// } else if (file.name === "root") { +// file.content = ''; +// return file; +// } +// return file; +// }); + +// project.save((err, savedProject) => { +// console.log('project', projectIndex, 'is saved.'); +// }); +// }); +// }); + + + diff --git a/server/utils/db_reformat_start.js b/server/utils/db_reformat_start.js new file mode 100644 index 00000000..9d949a02 --- /dev/null +++ b/server/utils/db_reformat_start.js @@ -0,0 +1,3 @@ +require('babel-register'); +require('babel-polyfill'); +require('./db_reformat'); \ No newline at end of file