add script to reformat database

This commit is contained in:
catarak 2016-09-04 16:39:12 -04:00
parent b10d2063ed
commit 52fa553d8f
2 changed files with 71 additions and 0 deletions

View file

@ -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.');
// });
// });
// });

View file

@ -0,0 +1,3 @@
require('babel-register');
require('babel-polyfill');
require('./db_reformat');