Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

database-set.js 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 prompt_1 = require("../prompt");
  13. const api_1 = require("../database/api");
  14. const requirePermissions_1 = require("../requirePermissions");
  15. const url_1 = require("url");
  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:set <path> [infile]")
  20. .description("store JSON data at the specified path via STDIN, arg, or file")
  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 dbPath = utils.getDatabaseUrl(origin, options.instance, path);
  35. const dbJsonURL = new url_1.URL(utils.getDatabaseUrl(origin, options.instance, path + ".json"));
  36. if (options.disableTriggers) {
  37. dbJsonURL.searchParams.set("disableTriggers", "true");
  38. }
  39. const confirm = await (0, prompt_1.promptOnce)({
  40. type: "confirm",
  41. name: "force",
  42. default: false,
  43. message: "You are about to overwrite all data at " + clc.cyan(dbPath) + ". Are you sure?",
  44. }, options);
  45. if (!confirm) {
  46. throw new error_1.FirebaseError("Command aborted.");
  47. }
  48. const inStream = utils.stringToStream(options.data) || (infile ? fs.createReadStream(infile) : process.stdin);
  49. if (!infile && !options.data) {
  50. utils.explainStdin();
  51. }
  52. const c = new apiv2_1.Client({ urlPrefix: dbJsonURL.origin, auth: true });
  53. try {
  54. await c.request({
  55. method: "PUT",
  56. path: dbJsonURL.pathname,
  57. body: inStream,
  58. queryParams: dbJsonURL.searchParams,
  59. });
  60. }
  61. catch (err) {
  62. logger_1.logger.debug(err);
  63. throw new error_1.FirebaseError(`Unexpected error while setting data: ${err}`, { exit: 2 });
  64. }
  65. utils.logSuccess("Data persisted successfully");
  66. logger_1.logger.info();
  67. logger_1.logger.info(clc.bold("View data at:"), utils.getDatabaseViewDataUrl(origin, options.project, options.instance, path));
  68. });