Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

database-push.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const clc = require("colorette");
  5. const fs = require("fs");
  6. const apiv2_1 = require("../apiv2");
  7. const command_1 = require("../command");
  8. const types_1 = require("../emulator/types");
  9. const error_1 = require("../error");
  10. const database_1 = require("../management/database");
  11. const commandUtils_1 = require("../emulator/commandUtils");
  12. const api_1 = require("../database/api");
  13. const requirePermissions_1 = require("../requirePermissions");
  14. const url_1 = require("url");
  15. const logger_1 = require("../logger");
  16. const requireDatabaseInstance_1 = require("../requireDatabaseInstance");
  17. const utils = require("../utils");
  18. exports.command = new command_1.Command("database:push <path> [infile]")
  19. .description("add a new JSON object to a list of data in your Firebase")
  20. .option("-d, --data <data>", "specify escaped JSON directly")
  21. .option("--instance <instance>", "use the database <instance>.firebaseio.com (if omitted, use default database instance)")
  22. .option("--disable-triggers", "suppress any Cloud functions triggered by this operation")
  23. .before(requirePermissions_1.requirePermissions, ["firebasedatabase.instances.update"])
  24. .before(requireDatabaseInstance_1.requireDatabaseInstance)
  25. .before(database_1.populateInstanceDetails)
  26. .before(commandUtils_1.printNoticeIfEmulated, types_1.Emulators.DATABASE)
  27. .action(async (path, infile, options) => {
  28. if (!path.startsWith("/")) {
  29. throw new error_1.FirebaseError("Path must begin with /");
  30. }
  31. const inStream = utils.stringToStream(options.data) || (infile ? fs.createReadStream(infile) : process.stdin);
  32. const origin = (0, api_1.realtimeOriginOrEmulatorOrCustomUrl)(options.instanceDetails.databaseUrl);
  33. const u = new url_1.URL(utils.getDatabaseUrl(origin, options.instance, path + ".json"));
  34. if (options.disableTriggers) {
  35. u.searchParams.set("disableTriggers", "true");
  36. }
  37. if (!infile && !options.data) {
  38. utils.explainStdin();
  39. }
  40. logger_1.logger.debug(`Database URL: ${u}`);
  41. const c = new apiv2_1.Client({ urlPrefix: u.origin, auth: true });
  42. let res;
  43. try {
  44. res = await c.request({
  45. method: "POST",
  46. path: u.pathname,
  47. body: inStream,
  48. queryParams: u.searchParams,
  49. });
  50. }
  51. catch (err) {
  52. logger_1.logger.debug(err);
  53. throw new error_1.FirebaseError(`Unexpected error while pushing data: ${err}`, { exit: 2 });
  54. }
  55. if (!path.endsWith("/")) {
  56. path += "/";
  57. }
  58. const consoleUrl = utils.getDatabaseViewDataUrl(origin, options.project, options.instance, path + res.body.name);
  59. utils.logSuccess("Data pushed successfully");
  60. logger_1.logger.info();
  61. logger_1.logger.info(clc.bold("View data at:"), consoleUrl);
  62. return { key: res.body.name };
  63. });