Skip to content

Commit cd2b3d7

Browse files
committed
Initial version
1 parent acc7492 commit cd2b3d7

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# parse-files-utils
22
Utilities to list and migrate Parse files
3+
4+
This utility will print in the terminal all the files URL's from the parse server
5+
6+
This can be really useful when you migrate your files and want to move the files from the Parse S3 host to you own.
7+
8+
This utility won't save the files anywhere else. You can save the results to a file or pipe the results to another program:
9+
10+
## usage
11+
12+
```
13+
$ node index.js MY_APP_ID MY_MASTER_KEY
14+
```
15+
16+
you can optionally specify a server URL
17+
18+
```
19+
$ node index.js MY_APP_ID MY_MASTER_KEY MY_SERVER_URL
20+
```

index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var appID = process.argv[2];
2+
var masterKey = process.argv[3];
3+
var serverURL = process.argv[4];
4+
5+
if (!appID || !masterKey) {
6+
process.stderr.write('An appId and a masterKey are required\n');
7+
process.exit(1);
8+
}
9+
10+
var utils = require('./lib')(appID, masterKey, serverURL);

lib/index.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var Parse = require('parse/node');
2+
var schemas = require('./schemas');
3+
4+
function onlyFiles(schemas) {
5+
return schemas.map(function(schema) {
6+
var fileFields = Object.keys(schema.fields).filter(function(key){
7+
var value = schema.fields[key];
8+
return value.type == "File";
9+
});
10+
if (fileFields.length > 0) {
11+
return {
12+
className: schema.className,
13+
fields: fileFields
14+
}
15+
}
16+
}).filter(function(s){ return s != undefined })
17+
}
18+
19+
function getAllObjects(baseQuery) {
20+
var allObjects = [];
21+
var next = function(startIndex) {
22+
baseQuery.skip(startIndex);
23+
return baseQuery.find({useMasterKey: true}).then(function(r){
24+
allObjects = allObjects.concat(r);
25+
if (r.length == 0) {
26+
return Promise.resolve(allObjects);
27+
} else {
28+
return next(startIndex+r.length);
29+
}
30+
});
31+
}
32+
return next(0);
33+
}
34+
35+
function getFilesFromSchema(schema) {
36+
var query = new Parse.Query(schema.className);
37+
query.select(schema.fields);
38+
schema.fields.forEach(function(field) {
39+
query.exists(field);
40+
})
41+
return getAllObjects(query).then(function(results) {
42+
return results.reduce(function(current, result){
43+
return current.concat(schema.fields.map(function(field){
44+
return result.get(field).url();
45+
}))
46+
}, []);
47+
});
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+
}

lib/schemas.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var request = require('request');
2+
var Parse = require('parse/node');
3+
4+
function get() {
5+
return new Promise(function(resolve, reject) {
6+
request({
7+
method: 'GET',
8+
url: Parse.serverURL+"/schemas",
9+
json: true,
10+
headers: {
11+
'Content-Type': 'application/json',
12+
'X-Parse-Application-Id': Parse.applicationId,
13+
'X-Parse-Master-Key': Parse.masterKey
14+
}
15+
}, function(err, res, body) {
16+
if (err) {
17+
return reject(err);
18+
}
19+
if (!body.results) {
20+
return reject(JSON.stringify(body));
21+
}
22+
resolve(body.results);
23+
})
24+
});
25+
}
26+
27+
module.exports = Object.freeze({
28+
get: get
29+
})

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "parse-files-utils",
3+
"version": "0.0.1",
4+
"description": "Utilities to list and migrate Parse files",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/parse-server-modules/parse-files-utils.git"
12+
},
13+
"keywords": [
14+
"parse",
15+
"files",
16+
"utils",
17+
"migration"
18+
],
19+
"author": "Florent Vilmart",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/parse-server-modules/parse-files-utils/issues"
23+
},
24+
"homepage": "https://github.com/parse-server-modules/parse-files-utils#readme",
25+
"dependencies": {
26+
"parse": "^1.8.5",
27+
"request": "^2.72.0"
28+
}
29+
}

0 commit comments

Comments
 (0)