Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database-update.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const url_1 = require("url");
  5. const clc = require("colorette");
  6. const fs = require("fs");
  7. const apiv2_1 = require("../apiv2");
  8. const command_1 = require("../command");
  9. const types_1 = require("../emulator/types");
  10. const error_1 = require("../error");
  11. const database_1 = require("../management/database");
  12. const commandUtils_1 = require("../emulator/commandUtils");
  13. const prompt_1 = require("../prompt");
  14. const api_1 = require("../database/api");
  15. const requirePermissions_1 = require("../requirePermissions");
  16. const logger_1 = require("../logger");
  17. const requireDatabaseInstance_1 = require("../requireDatabaseInstance");
  18. const utils = require("../utils");
  19. exports.command = new command_1.Command("database:update <path> [infile]")
  20. .description("update some of the keys for the defined path in your Firebase")
  21. .option("-d, --data <data>", "specify escaped JSON directly")
  22. .option("-f, --force", "pass this option to bypass confirmation prompt")
  23. .option("--instance <instance>", "use the database <instance>.firebaseio.com (if omitted, use default database instance)")
  24. .option("--disable-triggers", "suppress any Cloud functions triggered by this operation")
  25. .before(requirePermissions_1.requirePermissions, ["firebasedatabase.instances.update"])
  26. .before(requireDatabaseInstance_1.requireDatabaseInstance)
  27. .before(database_1.populateInstanceDetails)
  28. .before(commandUtils_1.printNoticeIfEmulated, types_1.Emulators.DATABASE)
  29. .action(async (path, infile, options) => {
  30. if (!path.startsWith("/")) {
  31. throw new error_1.FirebaseError("Path must begin with /");
  32. }
  33. const origin = (0, api_1.realtimeOriginOrEmulatorOrCustomUrl)(options.instanceDetails.databaseUrl);
  34. const url = utils.getDatabaseUrl(origin, options.instance, path);
  35. const confirmed = await (0, prompt_1.promptOnce)({
  36. type: "confirm",
  37. name: "force",
  38. default: false,
  39. message: `You are about to modify data at ${clc.cyan(url)}. Are you sure?`,
  40. }, options);
  41. if (!confirmed) {
  42. throw new error_1.FirebaseError("Command aborted.");
  43. }
  44. const inStream = utils.stringToStream(options.data) ||
  45. (infile && fs.createReadStream(infile)) ||
  46. process.stdin;
  47. const jsonUrl = new url_1.URL(utils.getDatabaseUrl(origin, options.instance, path + ".json"));
  48. if (options.disableTriggers) {
  49. jsonUrl.searchParams.set("disableTriggers", "true");
  50. }
  51. if (!infile && !options.data) {
  52. utils.explainStdin();
  53. }
  54. const c = new apiv2_1.Client({ urlPrefix: jsonUrl.origin, auth: true });
  55. try {
  56. await c.request({
  57. method: "PATCH",
  58. path: jsonUrl.pathname,
  59. body: inStream,
  60. queryParams: jsonUrl.searchParams,
  61. });
  62. }
  63. catch (err) {
  64. throw new error_1.FirebaseError("Unexpected error while setting data");
  65. }
  66. utils.logSuccess("Data updated successfully");
  67. logger_1.logger.info();
  68. logger_1.logger.info(clc.bold("View data at:"), utils.getDatabaseViewDataUrl(origin, options.project, options.instance, path));
  69. });