Firestore:在没有源代码控制的情况下对查找表进行更改。

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

FireStore: Make Changes to Lookup Tables without Source Control

问题

I want to seed a Firestore Database containing a lookup table. We are using Java 8 with Github/Intellij.

We are thinking of using this example https://stackoverflow.com/questions/63004211/how-do-i-seed-data-to-firebase-firestore to seed/update lookup table data every calendar quarter.

Is there any way to seed/update data in Firestore using Java (or any other coding language), while avoiding making changes to Git source control? What is google's recommended way? Changes need to be made automatically across Devops environments, Dev, QA, Staging, Production.

Changes need to be able to change by Product Owners team in UI, without using code if possible.

https://firebase.google.com/docs/firestore/manage-data/add-data#java

{
id: 1,
productName: 'Book',
description: 'reading books',

id: 2,
productName: 'Car',
description: 'automobile cars used for driving',

id: 3,
productName: 'Television',
description: 'Screen used for watching items',

etc..

}

英文:

I want to seed a Firestore Database containing a lookup table. We are using Java 8 with Github/Intellij.
We are thinking of using this example https://stackoverflow.com/questions/63004211/how-do-i-seed-data-to-firebase-firestore to seed/update lookup table data every calendar quarter.

Is there any way to seed/update data in Firestore using Java (or any other coding language), while avoiding making changes to Git source control? What is google's recommended way? Changes need to be made automatically across Devops environments, Dev, QA, Staging, Production.
Changes need to be able to change by ProductOwners team in UI, without using code if possible.

https://firebase.google.com/docs/firestore/manage-data/add-data#java

{
	id: 1,
	productName: 'Book',
	description: 'reading books',

	id: 2,
	productName: 'Car',
	description: 'automobile cars used for driving',

	id: 3,
	productName: 'Television',
	description: 'Screen used for watching items',

    etc..
}

答案1

得分: 2

  1. 将种子数据添加到具有读写权限的 Firestore 集合中
  2. 创建一个由 Firestore onWrite 事件触发的 Cloud Function,将给定的更改传播到目标位置

上述链接文档中的示例展示了如何使用 Firebase Admin SDK 在 NodeJS 中创建 Cloud Function。对于 Java,你可以使用 Firestore 文档中的 此示例

import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.logging.Logger;

public class FirebaseFirestore implements RawBackgroundFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  // 使用 GSON (https://github.com/google/gson) 解析 JSON 内容。
  private static final Gson gson = new Gson();

  @Override
  public void accept(String json, Context context) {
    JsonObject body = gson.fromJson(json, JsonObject.class);
    logger.info("由事件触发的函数: " + context.resource());
    logger.info("事件类型: " + context.eventType());

    if (body != null && body.has("oldValue")) {
      logger.info("旧值:");
      logger.info(body.get("oldValue").getAsString());
    }

    if (body != null && body has("value")) {
      logger.info("新值:");
      logger.info(body.get("value").getAsString());
    }
  }
}

或者,您可以使用 Gen 2 Cloud Functions 的 CloudEvent 触发器

package mycloudeventfunction;

import com.google.cloud.functions.CloudEventsFunction;
import io.cloudevents.CloudEvent;

// 定义一个实现 CloudEventsFunction 接口的类
public class MyCloudEventFunction implements CloudEventsFunction {
  // 实现 accept() 方法以处理 CloudEvents
  @Override
  public void accept(CloudEvent event) {
    // 在这里编写您的代码
    // 通过 event.getData() 访问 CloudEvent 数据有效载荷
    // 要将数据有效载荷作为 JSON 字符串获取,使用:
    // new String(event.getData().toBytes())
  }
}

您需要根据所支持的 Cloud Firestore 的适当 EventArc 触发器 来调整此示例,通过解析 CloudEvents 的 JSON 格式。不幸的是,Google 的文档中似乎没有直接的示例,因为 Gen 2 函数仍然相对较新。

英文:
  1. Add the seed data to a Firestore collection where the stakeholders have read/write permission
  2. Create a Cloud Function triggered by Firestore onWrite events that propagates the given change to the targeted locations

The example at the above-linked documentation shows how to create a Cloud Function in NodeJS using the Firebase Admin SDK. For Java you can use this example from the Firestore documentation:

import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.logging.Logger;

public class FirebaseFirestore implements RawBackgroundFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  // Use GSON (https://github.com/google/gson) to parse JSON content.
  private static final Gson gson = new Gson();

  @Override
  public void accept(String json, Context context) {
    JsonObject body = gson.fromJson(json, JsonObject.class);
    logger.info("Function triggered by event on: " + context.resource());
    logger.info("Event type: " + context.eventType());

    if (body != null && body.has("oldValue")) {
      logger.info("Old value:");
      logger.info(body.get("oldValue").getAsString());
    }

    if (body != null && body.has("value")) {
      logger.info("New value:");
      logger.info(body.get("value").getAsString());
    }
  }
}

Alternatively you can use CloudEvent triggers for Gen 2 Cloud Functions:

package mycloudeventfunction;

import com.google.cloud.functions.CloudEventsFunction;
import io.cloudevents.CloudEvent;

// Define a class that implements the CloudEventsFunction interface
public class MyCloudEventFunction implements CloudEventsFunction {
  // Implement the accept() method to handle CloudEvents
  @Override
  public void accept(CloudEvent event) {
    // Your code here
    // Access the CloudEvent data payload via event.getData()
    // To get the data payload as a JSON string, use:
    // new String(event.getData().toBytes())
  }
}

You would need to adapt this example to use the appropriate EventArc trigger for Cloud Firestore by parsing the JSON format for CloudEvents. Unfortunately there don't appear to be any direct examples of this in Google's documentation as Gen 2 functions are still relatively new.

huangapple
  • 本文由 发表于 2023年4月7日 01:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952116.html
匿名

发表评论

匿名网友

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

确定