gpt4 book ai didi

Google Cloud 2 firestore databases and cloud functions, avoiding duplicate code(Google Cloud 2 firestore数据库和云函数,避免重复代码)

转载 作者:bug小助手 更新时间:2023-10-25 13:31:13 25 4
gpt4 key购买 nike



For my company am I researching the possiblity to place the data in regions based on specific conditions to decrease latency, since this is possible since a few weeks.

对于我的公司,我正在研究根据特定条件将数据放置在区域中以减少延迟的可能性,因为这在几周后就可能实现。


The problem I faced is with using the onCall cloud function and avoiding duplicate code. When creating a function you have to specify its region, with second generation you can simply specify multiple regions like this: region: ["first", "second"] so that it great.
But based on that region I need it to connect to a different database, for example the first database needs to use the (default) database and the second region the us-version. I can simply use an environment variable to detect this, but I would like to keep it within the code to avoid a second step in the console.

我面临的问题是使用OnCall云功能和避免重复代码。当创建一个函数时,你必须指定它的区域,对于第二代,你可以简单地指定多个区域,就像这样:Region:[“First”,“Second”],这样它就很棒。但基于该区域,我需要它连接到不同的数据库,例如,第一个数据库需要使用(默认)数据库,第二个区域需要使用用户版本。我可以简单地使用环境变量来检测这一点,但我希望将其保留在代码中,以避免在控制台中执行第二步。


Furthermore when using the Firestore onWrite functions(or other equivilant) you have to specify the database it triggers by, otherwise it will use the (default) database as standard. Do I have to duplicate my code and do it for each database? I could make a function of the body and then specify it within each onWrite to improve it, but still this feels like the wrong think to do.

此外,在使用FiRestore onWite函数(或其他等价函数)时,您必须指定它触发的数据库,否则它将使用(默认)数据库作为标准。我是否必须复制我的代码并为每个数据库执行此操作?我可以创建身体的一个函数,然后在每个onWrite中指定它来改进它,但这仍然感觉是错误的想法。


Any suggestions are welcome

欢迎提出任何建议。


更多回答
优秀答案推荐

So I found out that there is an environment variable that gives you the name of the cloud function. Because every cloud function start with the region name, this can be used to get the location of the cloud function and therefor apply logic to use the correct database. This solves the problem for the onCall function as follows:

所以我发现有一个环境变量可以为您提供云函数的名称。因为每个云函数都以区域名开头,所以这可以用来获取云函数的位置,并因此应用逻辑来使用正确的数据库。这解决了OnCall函数的问题,如下所示:


const callCreate = onCall(
{
region: [constants.regions.default.functions, constants.regions.us.functions],
}, async (request) => {
const database = getFirestoreInstance();

Where the function is as follows:

其中,函数如下:


import constants from "../constants";
import {firestore} from "firebase-admin";

function getFirestoreInstance(): firestore.Firestore {
const functionName = process.env.K_SERVICE || "";
if (functionName.length === 0) {
throw new Error("Function name not found");
}
const databaseURL = functionName.startsWith(constants.regions.default.functions) ?
constants.regions.default.firestore.databaseURL :
constants.regions.us.firestore.databaseURL;

const database = firestore();
database.settings({
databaseId: databaseURL,
});
return database;
}

export default getFirestoreInstance;

Unfortunately this does not solve the problem of the trigger, because the trigger can only be on one database. So duplicate code like the following is necessary:

遗憾的是,这并没有解决触发器的问题,因为触发器只能位于一个数据库上。因此,需要如下所示的重复代码:


const onCreate = onDocumentCreated(
{
document: constants.functions.firestore.News.location,
region: constants.regions.default.functions,
database: constants.regions.default.firestore.databaseURL,
minInstances: constants.functions.firestore.News.onCreate.minInstances,
}, async (change) => {
if (change.data === undefined) {
throw new Error("Change is undefined");
}
// Do something
}
);

const onCreateUS = onDocumentCreated(
{
document: constants.functions.firestore.News.location,
region: constants.regions.us.functions,
database: constants.regions.us.firestore.databaseURL,
minInstances: constants.functions.firestore.News.onCreate.minInstances,
}, async (change) => {
if (change.data === undefined) {
throw new Error("Change is undefined");
}
// Do something
}
);

export {onCreate, onCreateUS};

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com