1
1
var Parse = require ( 'parse/node' ) ;
2
+ var inquirer = require ( 'inquirer' ) ;
3
+
2
4
var schemas = require ( './schemas' ) ;
5
+ var transfer = require ( './transfer' ) ;
6
+ var questions = require ( './questions.js' ) ;
7
+
8
+ module . exports = initialize ;
9
+
10
+ function initialize ( config ) {
11
+ questions ( config ) . then ( function ( answers ) {
12
+ config = Object . assign ( config , answers ) ;
13
+ console . log ( JSON . stringify ( config , null , 2 ) ) ;
14
+ return inquirer . prompt ( {
15
+ type : 'confirm' ,
16
+ name : 'next' ,
17
+ message : 'About to start the file transfer. Does the above look correct?' ,
18
+ default : true ,
19
+ } ) ;
20
+ } ) . then ( function ( answers ) {
21
+ if ( ! answers . next ) {
22
+ console . log ( 'Aborted!' ) ;
23
+ process . exit ( ) ;
24
+ }
25
+ Parse . initialize ( config . applicationId , null , config . masterKey ) ;
26
+ Parse . serverURL = config . serverURL ;
27
+ return transfer . init ( config ) ;
28
+ } ) . then ( function ( ) {
29
+ return getAllFileObjects ( ) ;
30
+ } ) . then ( function ( objects ) {
31
+ return transfer . run ( objects ) ;
32
+ } ) . then ( function ( ) {
33
+ console . log ( 'Complete!' ) ;
34
+ process . exit ( ) ;
35
+ } ) . catch ( function ( error ) {
36
+ console . log ( error ) ;
37
+ process . exit ( 1 ) ;
38
+ } ) ;
39
+ }
40
+
41
+ function getAllFileObjects ( ) {
42
+ console . log ( "Fetching schema..." ) ;
43
+ return schemas . get ( ) . then ( function ( res ) {
44
+ console . log ( "Fetching all objects with files..." ) ;
45
+ var schemasWithFiles = onlyFiles ( res ) ;
46
+ return Promise . all ( schemasWithFiles . map ( getObjectsWithFilesFromSchema ) ) ;
47
+ } ) . then ( function ( results ) {
48
+ var files = results . reduce ( function ( c , r ) {
49
+ return c . concat ( r ) ;
50
+ } , [ ] ) ;
51
+ return Promise . resolve ( files ) ;
52
+ } ) ;
53
+ }
3
54
4
55
function onlyFiles ( schemas ) {
5
56
return schemas . map ( function ( schema ) {
@@ -18,53 +69,43 @@ function onlyFiles(schemas) {
18
69
19
70
function getAllObjects ( baseQuery ) {
20
71
var allObjects = [ ] ;
21
- var next = function ( startIndex ) {
22
- baseQuery . skip ( startIndex ) ;
72
+ var next = function ( ) {
73
+ if ( allObjects . length ) {
74
+ baseQuery . greaterThan ( 'createdAt' , allObjects [ allObjects . length - 1 ] . createdAt ) ;
75
+ }
23
76
return baseQuery . find ( { useMasterKey : true } ) . then ( function ( r ) {
24
77
allObjects = allObjects . concat ( r ) ;
25
78
if ( r . length == 0 ) {
26
79
return Promise . resolve ( allObjects ) ;
27
80
} else {
28
- return next ( startIndex + r . length ) ;
81
+ return next ( ) ;
29
82
}
30
83
} ) ;
31
84
}
32
- return next ( 0 ) ;
85
+ return next ( ) ;
33
86
}
34
87
35
- function getFilesFromSchema ( schema ) {
88
+ function getObjectsWithFilesFromSchema ( schema ) {
36
89
var query = new Parse . Query ( schema . className ) ;
37
- query . select ( schema . fields ) ;
90
+ query . select ( schema . fields . concat ( 'createdAt' ) ) ;
91
+ query . ascending ( 'createdAt' ) ;
92
+ query . limit ( 1000 ) ;
38
93
schema . fields . forEach ( function ( field ) {
39
94
query . exists ( field ) ;
40
- } )
95
+ } ) ;
41
96
return getAllObjects ( query ) . then ( function ( results ) {
42
97
return results . reduce ( function ( current , result ) {
43
- return current . concat ( schema . fields . map ( function ( field ) {
44
- return result . get ( field ) . url ( ) ;
45
- } ) )
98
+ return current . concat (
99
+ schema . fields . map ( function ( field ) {
100
+ return {
101
+ className : schema . className ,
102
+ objectId : result . id ,
103
+ fieldName : field ,
104
+ fileName : result . get ( field ) . name ( ) ,
105
+ url : result . get ( field ) . url ( )
106
+ }
107
+ } )
108
+ ) ;
46
109
} , [ ] ) ;
47
110
} ) ;
48
- }
49
-
50
- module . exports = function ( applicationId , masterKey , serverURL ) {
51
- Parse . initialize ( applicationId , null , masterKey ) ;
52
- Parse . serverURL = serverURL || "https://api.parse.com/1" ;
53
- schemas . get ( ) . then ( function ( res ) {
54
- var schemasWithFiles = onlyFiles ( res ) ;
55
- return Promise . all ( schemasWithFiles . map ( getFilesFromSchema ) ) ;
56
- } ) . then ( function ( results ) {
57
- var files = results . reduce ( function ( c , r ) {
58
- return c . concat ( r ) ;
59
- } , [ ] ) ;
60
- files . forEach ( function ( file ) {
61
- process . stdout . write ( file ) ;
62
- process . stdout . write ( "\n" ) ;
63
- } ) ;
64
- process . exit ( 0 ) ;
65
- } ) . catch ( function ( err ) {
66
- process . stderr . write ( err ) ;
67
- process . stderr . write ( "\n" ) ;
68
- process . exit ( 1 ) ;
69
- } )
70
- }
111
+ }
0 commit comments