重新创建Firebase后端功能并重新设置时,项目是否会忽略原始功能集?

huangapple go评论96阅读模式
英文:

When you recreate your Firebase backend functions and redo the setup, does the project ignore the original set of functions?

问题

I have a Firebase project that uses some Firestore triggers and backend Typescript functions. Let's call the folder for the original set of backend functions CloudFunctions1. This was working great. Recently, I completed recreated my cloud functions, going through the whole process of doing firebase init, firebase login, even adding the extensions I wanted in VSCode. I did this so that my project starts with the latest base skeleton since it had been years since I started the project initially. The new folder for this is called CloudFunctions2.

How does my app know to call the functions in CloudFunctions2 instead of the functions in CloudFunctions1? When I did the setup, I selected a project, sure, but does that mean that that project will now completely ignore/disregard the functions in CloudFunctions1?

index.ts:

import {onDocumentCreated} from "firebase-functions/v2/firestore";
import {onDocumentUpdated} from "firebase-functions/v2/firestore";
import {onDocumentDeleted} from "firebase-functions/v2/firestore";
import {onCall} from "firebase-functions/v2/https";
import {HttpsError} from "firebase-functions/v2/https";

exports.callGroupRTDBSetup = onCall({cors: true}, async (req) => {
  try {
    const groupID = req.data.groupID;
    const adminUID = req.data.adminUID;
    const adminUsername = req.data.adminUsername;
    const createdTimestamp = req.data.createdTimestamp;

    await setGroupAdminRTDB(groupID, adminUID, adminUsername, createdTimestamp);

    await addMemberRTDB(groupID, adminUID, adminUsername, createdTimestamp);

    return {
      returnVal: 1,
      returnMsg: "callGroupRTDBSetup success!",
    };
  } catch (error) {
    throw new HttpsError("invalid-argument", "Error: " + error);
  }
});

/**
 * Sets the admin information into the group node in RTDB
 * @param {string} groupID
 * @param {string} adminUID
 * @param {string} adminUsername
 * @param {number} createdAdminTimestamp
 * @return {Promise<{returnVal: number, returnMsg: string}>} A Promise that
 * resolves with an object containing the return value and message.
 */
async function setGroupAdminRTDB(
  groupID: string,
  adminUID: string,
  adminUsername: string,
  createdAdminTimestamp: number
): Promise<{returnVal: number; returnMsg: string}> {
  let returnVal = 0;
  let returnMsg = "";

  const nodeRef = await admin
    .database()
    .ref("app/groups/" + groupID + "/admin/")
    .once("value");

  const data = nodeRef.val();

  if (data == null) {
    try {
      const adminData = {
        adminUsername: adminUsername,
        createdAdminTimestamp: createdAdminTimestamp,
      };

      await admin
        .database()
        .ref("app/groups/" + groupID + "/admin/" + adminUID)
        .set(adminData);

      returnVal = 1;
      returnMsg = "Data successfully added!";
    } catch (error) {
      returnVal = 0;
      returnMsg = "ERROR: " + error;
    }
  } else {
    returnVal = 0;
    returnMsg = "Error: data already exists at admin node";
  }

  return {returnVal: returnVal, returnMsg: returnMsg};
}

I deploy these functions using firebase deploy.

英文:

I have a Firebase project that uses some Firestore triggers and backend Typescript functions. Let's call the folder for the original set of backend functions CloudFunctions1. This was working great. Recently, I completed recreated my cloud functions, going through the whole process of doing firebase init, firebase login, even adding the extensions I wanted in VSCode. I did this so that my project starts with the latest base skeleton since it had been years since I started the project initially. The new folder for this is called CloudFunctions2.

How does my app know to call the functions in CloudFunctions2 instead of the functions in CloudFunctions1? When I did the setup, I selected a project, sure, but does that mean that that project will now completely ignore/disregard the functions in CloudFunctions1?

index.ts:

import {onDocumentCreated} from &quot;firebase-functions/v2/firestore&quot;;
import {onDocumentUpdated} from &quot;firebase-functions/v2/firestore&quot;;
import {onDocumentDeleted} from &quot;firebase-functions/v2/firestore&quot;;
import {onCall} from &quot;firebase-functions/v2/https&quot;;
import {HttpsError} from &quot;firebase-functions/v2/https&quot;;
exports.callGroupRTDBSetup = onCall({cors: true}, async (req) =&gt; {
try {
const groupID = req.data.groupID;
const adminUID = req.data.adminUID;
const adminUsername = req.data.adminUsername;
const createdTimestamp = req.data.createdTimestamp;
await setGroupAdminRTDB(groupID, adminUID, adminUsername, createdTimestamp);
await addMemberRTDB(groupID, adminUID, adminUsername, createdTimestamp);
return {
returnVal: 1,
returnMsg: &quot;callGroupRTDBSetup success!&quot;,
};
} catch (error) {
throw new HttpsError(&quot;invalid-argument&quot;, &quot;Error: &quot; + error);
}
});
/**
* Sets the admin information into the group node in RTDB
* @param {string} groupID
* @param {string} adminUID
* @param {string} adminUsername
* @param {number} createdAdminTimestamp
* @return {Promise&lt;{returnVal: number, returnMsg: string}&gt;} A Promise that
* resolves with an object containing the return value and message.
*/
async function setGroupAdminRTDB(
groupID: string,
adminUID: string,
adminUsername: string,
createdAdminTimestamp: number
): Promise&lt;{returnVal: number; returnMsg: string}&gt; {
let returnVal = 0;
let returnMsg = &quot;&quot;;
const nodeRef = await admin
.database()
.ref(&quot;app/groups/&quot; + groupID + &quot;/admin/&quot;)
.once(&quot;value&quot;);
const data = nodeRef.val();
if (data == null) {
try {
const adminData = {
adminUsername: adminUsername,
createdAdminTimestamp: createdAdminTimestamp,
};
await admin
.database()
.ref(&quot;app/groups/&quot; + groupID + &quot;/admin/&quot; + adminUID)
.set(adminData);
returnVal = 1;
returnMsg = &quot;Data successfully added!&quot;;
} catch (error) {
returnVal = 0;
returnMsg = &quot;ERROR: &quot; + error;
}
} else {
returnVal = 0;
returnMsg = &quot;Error: data already exists at admin node&quot;;
}
return {returnVal: returnVal, returnMsg: returnMsg};
}

I deploy these functions using firebase deploy.

答案1

得分: 1

"firebase deploy" 将简单地使用新的函数覆盖所有先前部署的函数,一旦执行完成,无论您在哪里运行该命令。旧函数将被替换,新函数立即生效。客户端不能选择调用旧版本。

英文:

firebase deploy will simply overwrite all previously deployed functions with the new functions by the time it's done executing. It doesn't matter where your run the command. The old functions are gone and the new ones take their place immediately. The client cannot choose to invoke the old versions.

huangapple
  • 本文由 发表于 2023年8月5日 03:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定