No Description
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.

indexes.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initIndexes = void 0;
  4. const clc = require("colorette");
  5. const fs = require("fs");
  6. const error_1 = require("../../../error");
  7. const iv2 = require("../../../firestore/indexes");
  8. const fsutils = require("../../../fsutils");
  9. const prompt_1 = require("../../../prompt");
  10. const logger_1 = require("../../../logger");
  11. const indexes = new iv2.FirestoreIndexes();
  12. const INDEXES_TEMPLATE = fs.readFileSync(__dirname + "/../../../../templates/init/firestore/firestore.indexes.json", "utf8");
  13. function initIndexes(setup, config) {
  14. logger_1.logger.info();
  15. logger_1.logger.info("Firestore indexes allow you to perform complex queries while");
  16. logger_1.logger.info("maintaining performance that scales with the size of the result");
  17. logger_1.logger.info("set. You can keep index definitions in your project directory");
  18. logger_1.logger.info("and publish them with " + clc.bold("firebase deploy") + ".");
  19. logger_1.logger.info();
  20. return (0, prompt_1.prompt)(setup.config.firestore, [
  21. {
  22. type: "input",
  23. name: "indexes",
  24. message: "What file should be used for Firestore indexes?",
  25. default: "firestore.indexes.json",
  26. },
  27. ])
  28. .then(() => {
  29. const filename = setup.config.firestore.indexes;
  30. if (fsutils.fileExistsSync(filename)) {
  31. const msg = "File " +
  32. clc.bold(filename) +
  33. " already exists." +
  34. " Do you want to overwrite it with the Firestore Indexes from the Firebase Console?";
  35. return (0, prompt_1.promptOnce)({
  36. type: "confirm",
  37. message: msg,
  38. default: false,
  39. });
  40. }
  41. return Promise.resolve(true);
  42. })
  43. .then((overwrite) => {
  44. if (!overwrite) {
  45. return Promise.resolve();
  46. }
  47. if (!setup.projectId) {
  48. return config.writeProjectFile(setup.config.firestore.indexes, INDEXES_TEMPLATE);
  49. }
  50. return getIndexesFromConsole(setup.projectId).then((contents) => {
  51. return config.writeProjectFile(setup.config.firestore.indexes, contents);
  52. });
  53. });
  54. }
  55. exports.initIndexes = initIndexes;
  56. function getIndexesFromConsole(projectId) {
  57. const indexesPromise = indexes.listIndexes(projectId);
  58. const fieldOverridesPromise = indexes.listFieldOverrides(projectId);
  59. return Promise.all([indexesPromise, fieldOverridesPromise])
  60. .then((res) => {
  61. return indexes.makeIndexSpec(res[0], res[1]);
  62. })
  63. .catch((e) => {
  64. if (e.message.indexOf("is not a Cloud Firestore enabled project") >= 0) {
  65. return INDEXES_TEMPLATE;
  66. }
  67. throw new error_1.FirebaseError("Error fetching Firestore indexes", {
  68. original: e,
  69. });
  70. });
  71. }