mirror of
https://github.com/zhigang1992/firebase-tools.git
synced 2026-05-22 23:28:33 +08:00
Full suite of data commands: get, set, update, push, remove
This commit is contained in:
52
commands/data-push.js
Normal file
52
commands/data-push.js
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
var Command = require('../lib/command');
|
||||
var requireAccess = require('../lib/requireAccess');
|
||||
var getFirebaseName = require('../lib/getFirebaseName');
|
||||
var request = require('request');
|
||||
var api = require('../lib/api');
|
||||
var responseToError = require('../lib/responseToError');
|
||||
var FirebaseError = require('../lib/error');
|
||||
var RSVP = require('rsvp');
|
||||
var utils = require('../lib/utils');
|
||||
var querystring = require('querystring');
|
||||
var chalk = require('chalk');
|
||||
var logger = require('../lib/logger');
|
||||
var fs = require('fs');
|
||||
var Firebase = require('firebase');
|
||||
|
||||
module.exports = new Command('data:push <path>')
|
||||
.description('add a new JSON object to a list of data in your Firebase')
|
||||
.option('-f, --firebase <app>', 'override the app specified in firebase.json')
|
||||
.option('-a, --auth <token>', 'authorization token to use (defaults to admin token)')
|
||||
.option('-i, --input <filename>', 'read data from the specified file')
|
||||
.before(requireAccess)
|
||||
.action(function(path, options) {
|
||||
var firebase = getFirebaseName(options);
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
var fileIn = !!options.input;
|
||||
var inStream = fileIn ? fs.createReadStream(options.input) : process.stdin;
|
||||
|
||||
var url = utils.addSubdomain(api.realtimeOrigin, firebase) + path + '.json?';
|
||||
var query = {auth: options.auth || options.dataToken};
|
||||
|
||||
url += querystring.stringify(query);
|
||||
|
||||
inStream.pipe(request.post(url, {json: true}, function(err, res, body) {
|
||||
logger.info();
|
||||
if (err) {
|
||||
return reject(new FirebaseError('Unexpected error while pushing data', {exit: 2}));
|
||||
} else if (res.statusCode >= 400) {
|
||||
return reject(responseToError(res, body));
|
||||
}
|
||||
|
||||
var refurl = utils.addSubdomain(api.realtimeOrigin, firebase) + path + '/' + body.name;
|
||||
|
||||
utils.logSuccess('Data pushed successfully');
|
||||
logger.info();
|
||||
logger.info(chalk.bold('View data at:'), refurl);
|
||||
return resolve(new Firebase(refurl));
|
||||
}));
|
||||
});
|
||||
});
|
||||
39
commands/data-remove.js
Normal file
39
commands/data-remove.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
var Command = require('../lib/command');
|
||||
var requireAccess = require('../lib/requireAccess');
|
||||
var getFirebaseName = require('../lib/getFirebaseName');
|
||||
var request = require('request');
|
||||
var api = require('../lib/api');
|
||||
var responseToError = require('../lib/responseToError');
|
||||
var FirebaseError = require('../lib/error');
|
||||
var RSVP = require('rsvp');
|
||||
var utils = require('../lib/utils');
|
||||
var querystring = require('querystring');
|
||||
|
||||
module.exports = new Command('data:remove <path>')
|
||||
.description('remove data from your Firebase at the specified path')
|
||||
.option('-f, --firebase <app>', 'override the app specified in firebase.json')
|
||||
.option('-a, --auth <token>', 'authorization token to use (defaults to admin token)')
|
||||
.before(requireAccess)
|
||||
.action(function(path, options) {
|
||||
var firebase = getFirebaseName(options);
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
var url = utils.addSubdomain(api.realtimeOrigin, firebase) + path + '.json?';
|
||||
var query = {auth: options.auth || options.dataToken};
|
||||
|
||||
url += querystring.stringify(query);
|
||||
|
||||
request.del(url, {json: true}, function(err, res, body) {
|
||||
if (err) {
|
||||
return reject(new FirebaseError('Unexpected error while removing data', {exit: 2}));
|
||||
} else if (res.statusCode >= 400) {
|
||||
return reject(responseToError(res, body));
|
||||
}
|
||||
|
||||
utils.logSuccess('Data removed successfully');
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,7 @@ var utils = require('../lib/utils');
|
||||
var querystring = require('querystring');
|
||||
var chalk = require('chalk');
|
||||
var logger = require('../lib/logger');
|
||||
var fs = require('fs');
|
||||
|
||||
module.exports = new Command('data:set <path>')
|
||||
.description('store JSON data in your Firebase at the specified path')
|
||||
@@ -30,7 +31,6 @@ module.exports = new Command('data:set <path>')
|
||||
var query = {auth: options.auth || options.dataToken};
|
||||
|
||||
url += querystring.stringify(query);
|
||||
console.log(url);
|
||||
|
||||
inStream.pipe(request.put(url, {json: true}, function(err, res, body) {
|
||||
logger.info();
|
||||
|
||||
49
commands/data-update.js
Normal file
49
commands/data-update.js
Normal file
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
var Command = require('../lib/command');
|
||||
var requireAccess = require('../lib/requireAccess');
|
||||
var getFirebaseName = require('../lib/getFirebaseName');
|
||||
var request = require('request');
|
||||
var api = require('../lib/api');
|
||||
var responseToError = require('../lib/responseToError');
|
||||
var FirebaseError = require('../lib/error');
|
||||
var RSVP = require('rsvp');
|
||||
var utils = require('../lib/utils');
|
||||
var querystring = require('querystring');
|
||||
var chalk = require('chalk');
|
||||
var logger = require('../lib/logger');
|
||||
var fs = require('fs');
|
||||
|
||||
module.exports = new Command('data:update <path>')
|
||||
.description('update some of the keys for the defined path in your Firebase')
|
||||
.option('-f, --firebase <app>', 'override the app specified in firebase.json')
|
||||
.option('-a, --auth <token>', 'authorization token to use (defaults to admin token)')
|
||||
.option('-i, --input <filename>', 'read data from the specified file')
|
||||
.before(requireAccess)
|
||||
.action(function(path, options) {
|
||||
var firebase = getFirebaseName(options);
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
var fileIn = !!options.input;
|
||||
var inStream = fileIn ? fs.createReadStream(options.input) : process.stdin;
|
||||
|
||||
var url = utils.addSubdomain(api.realtimeOrigin, firebase) + path + '.json?';
|
||||
var query = {auth: options.auth || options.dataToken};
|
||||
|
||||
url += querystring.stringify(query);
|
||||
|
||||
inStream.pipe(request.patch(url, {json: true}, function(err, res, body) {
|
||||
logger.info();
|
||||
if (err) {
|
||||
return reject(new FirebaseError('Unexpected error while setting data', {exit: 2}));
|
||||
} else if (res.statusCode >= 400) {
|
||||
return reject(responseToError(res, body));
|
||||
}
|
||||
|
||||
utils.logSuccess('Data updated successfully');
|
||||
logger.info();
|
||||
logger.info(chalk.bold('View data at:'), utils.addSubdomain(api.realtimeOrigin, firebase) + path);
|
||||
return resolve();
|
||||
}));
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,10 @@ module.exports = function(client) {
|
||||
// client.collab.remove = loadCommand('collab-remove');
|
||||
client.data = {
|
||||
get: loadCommand('data-get'),
|
||||
set: loadCommand('data-set')
|
||||
push: loadCommand('data-push'),
|
||||
set: loadCommand('data-set'),
|
||||
remove: loadCommand('data-remove'),
|
||||
update: loadCommand('data-update')
|
||||
};
|
||||
client.deploy = loadCommand('deploy');
|
||||
client.disable = {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var FirebaseError = require('./error');
|
||||
var _ = require('lodash');
|
||||
|
||||
module.exports = function(response, body, options) {
|
||||
if (response.statusCode < 400) {
|
||||
|
||||
Reference in New Issue
Block a user