-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (97 loc) · 2.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
var Minimist = require('minimist')(process.argv.slice(2));
var path = require('path');
var Parse = require('parse/node');
var SchemaController = require('parse-server/lib/Controllers/SchemaController');
var Transform = require('parse-server/lib/transform');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
const importData = function(className, file, db, callback) {
var data = require("./" + file).results;
if (!data || !data.length) throw 'Invalid data format, please check your data file!';
console.log("Start importing " + className);
var index = 0;
promiseWhile(() => {
return index < data.length;
}, () => {
var objData = data[index++];
objData = beforeTransformKeys( className, objData );
objData = Transform.transformCreate( SchemaController.defaultColumns[className], className, objData );
objData = afterTransformKeys( className, objData );
var promise = new Parse.Promise();
db.collection(className).insertOne( objData, function(err, result) {
try{
assert.equal(err, null);
console.log("Inserted a document into the " + className + " collection.");
}catch(e){
console.log(e.message);
}
promise.resolve();
});
return promise;
}).then(() => {
callback();
});
}
const promiseWhile = function(condition, body) {
var promise = new Parse.Promise();
function loop() {
if (!condition()) return promise.resolve();
body().then(loop, promise.reject);
}
loop();
return promise;
}
function beforeTransformKeys(className, obj){
switch(className){
default:
for( var el in obj){
if( typeof obj[el].__type != 'undefined' ){
if( obj[el].__type == 'File' ){
obj[el].__type = '__TEMPFILE';
}
}
}
}
return obj;
}
function afterTransformKeys(className, obj){
for( var el in obj ){
if( typeof obj[el].__type != 'undefined' ){
if( obj[el].__type == '__TEMPFILE' ){
obj[el].__type = 'File';
}
}
switch( className ){
case '_User':
if( el == "bcryptPassword" ){
obj._hashed_password = obj[el];
delete obj[el];
}
break;
}
}
return obj;
}
try{
var mongodbConnectionString = Minimist.db || "mongodb://localhost:27017/parse";
MongoClient.connect(mongodbConnectionString, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to MongoDB...");
if(typeof Minimist._[0] == 'undefined'){
console.error("Missing file to import");
db.close();
}else{
var file = Minimist._[0];
var className = path.basename(file);
className = className.substr(0, className.lastIndexOf('.')) || className;
importData( className , file , db, function(){
console.log('Importing finished!');
db.close();
console.log("MongoDB connection closed.");
});
}
});
}catch(e){
console.log(e.message);
}