Geen omschrijving
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-get.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const fs = require("fs");
  5. const url = require("url");
  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 logger_1 = require("../logger");
  15. const requireDatabaseInstance_1 = require("../requireDatabaseInstance");
  16. const responseToError_1 = require("../responseToError");
  17. const utils = require("../utils");
  18. function applyStringOpts(dest, src, keys, jsonKeys) {
  19. for (const key of keys) {
  20. if (src[key]) {
  21. dest[key] = src[key];
  22. }
  23. }
  24. for (const key of jsonKeys) {
  25. let jsonVal;
  26. try {
  27. jsonVal = JSON.parse(src[key]);
  28. }
  29. catch (_) {
  30. jsonVal = src[key];
  31. }
  32. if (src[key]) {
  33. dest[key] = JSON.stringify(jsonVal);
  34. }
  35. }
  36. }
  37. exports.command = new command_1.Command("database:get <path>")
  38. .description("fetch and print JSON data at the specified path")
  39. .option("-o, --output <filename>", "save output to the specified file")
  40. .option("--pretty", "pretty print response")
  41. .option("--shallow", "return shallow response")
  42. .option("--export", "include priorities in the output response")
  43. .option("--order-by <key>", "select a child key by which to order results")
  44. .option("--order-by-key", "order by key name")
  45. .option("--order-by-value", "order by primitive value")
  46. .option("--limit-to-first <num>", "limit to the first <num> results")
  47. .option("--limit-to-last <num>", "limit to the last <num> results")
  48. .option("--start-at <val>", "start results at <val> (based on specified ordering)")
  49. .option("--end-at <val>", "end results at <val> (based on specified ordering)")
  50. .option("--equal-to <val>", "restrict results to <val> (based on specified ordering)")
  51. .option("--instance <instance>", "use the database <instance>.firebaseio.com (if omitted, use default database instance)")
  52. .before(requirePermissions_1.requirePermissions, ["firebasedatabase.instances.get"])
  53. .before(requireDatabaseInstance_1.requireDatabaseInstance)
  54. .before(database_1.populateInstanceDetails)
  55. .before(commandUtils_1.printNoticeIfEmulated, types_1.Emulators.DATABASE)
  56. .action(async (path, options) => {
  57. if (!path.startsWith("/")) {
  58. return utils.reject("Path must begin with /", { exit: 1 });
  59. }
  60. const dbHost = (0, api_1.realtimeOriginOrEmulatorOrCustomUrl)(options.instanceDetails.databaseUrl);
  61. const dbUrl = utils.getDatabaseUrl(dbHost, options.instance, path + ".json");
  62. const query = {};
  63. if (options.shallow) {
  64. query.shallow = "true";
  65. }
  66. if (options.pretty) {
  67. query.print = "pretty";
  68. }
  69. if (options.export) {
  70. query.format = "export";
  71. }
  72. if (options.orderByKey) {
  73. options.orderBy = "$key";
  74. }
  75. if (options.orderByValue) {
  76. options.orderBy = "$value";
  77. }
  78. applyStringOpts(query, options, ["limitToFirst", "limitToLast"], ["orderBy", "startAt", "endAt", "equalTo"]);
  79. const urlObj = new url.URL(dbUrl);
  80. const client = new apiv2_1.Client({
  81. urlPrefix: urlObj.origin,
  82. auth: true,
  83. });
  84. const res = await client.request({
  85. method: "GET",
  86. path: urlObj.pathname,
  87. queryParams: query,
  88. responseType: "stream",
  89. resolveOnHTTPError: true,
  90. });
  91. const fileOut = !!options.output;
  92. const outStream = fileOut ? fs.createWriteStream(options.output) : process.stdout;
  93. if (res.status >= 400) {
  94. const r = await res.response.text();
  95. let d;
  96. try {
  97. d = JSON.parse(r);
  98. }
  99. catch (e) {
  100. throw new error_1.FirebaseError("Malformed JSON response", { original: e, exit: 2 });
  101. }
  102. throw (0, responseToError_1.responseToError)({ statusCode: res.status }, d);
  103. }
  104. res.body.pipe(outStream, { end: false });
  105. return new Promise((resolve) => {
  106. res.body.once("end", () => {
  107. if (outStream === process.stdout) {
  108. outStream.write("\n");
  109. resolve();
  110. }
  111. else if (outStream instanceof fs.WriteStream) {
  112. outStream.write("\n");
  113. outStream.on("close", () => resolve());
  114. outStream.close();
  115. }
  116. else {
  117. logger_1.logger.debug("[database:get] Could not write line break to outStream");
  118. resolve();
  119. }
  120. });
  121. });
  122. });