【问题标题】:How to re-write this batch.delete function to work for realtime database?如何重新编写这个 batch.delete 函数来为实时数据库工作?
【发布时间】:2021-06-24 11:56:56
【问题描述】:

我有一个谷歌云功能,目前适用于 firestore。我能够将第一个变量转换为实时数据库,但不完全有知识 () 对其余部分做同样的事情。

如何在实时数据库中执行此操作?

  exports.cleanupUser = functions.auth.user().onDelete(async (user) => {
   const dbRef = admin.database().ref('stripe_customers');//admin.firestore().collection('stripe_customers');
   const customer = (await dbRef.doc(user.uid).get()).data();
   await stripe.customers.del(customer.customer_id);
   // Delete the customers payments & payment methods in firestore.
   const batch = admin.firestore().batch();
   const paymetsMethodsSnapshot = await dbRef
     .doc(user.uid)
     .collection('payment_methods')
     .get();
   paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
   const paymentsSnapshot = await dbRef
     .doc(user.uid)
     .collection('payments')
     .get();
   paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));
 
   await batch.commit();
 
   await dbRef.doc(user.uid).delete();
   return;
 });

我尝试了以下方法,但它似乎不起作用:

 exports.cleanupUser = functions.auth.user().onDelete(async (user) => {
   const dbRef = admin.database().ref('stripe_customers');//admin.firestore().collection('stripe_customers');
   const customer = (await dbRef.child(user.uid).get()).data();
   await stripe.customers.del(customer.customer_id);
   // Delete the customers payments & payment methods in firestore.
   const batch = admin.database().batch();
   const paymetsMethodsSnapshot = await dbRef
    .child(user.uid).child('payment_methods').get();
   paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
   const paymentsSnapshot = await dbRef
    .child(user.uid).child('payments').get();
   paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));
 
   await batch.commit();
 
   await dbRef.child(user.uid).delete();
   return;
 });

【问题讨论】:

  • Firestore 在实时数据库上的批量写入等价物是多路径更新,您将路径和值传递给写入update() 函数。如果您将null 指定为路径的值,则该路径将被删除。转换整个函数有点超出 Stack Overflow 的范围,但这应该是一个很好的起点。

标签: javascript firebase firebase-realtime-database google-cloud-firestore google-cloud-functions


【解决方案1】:

Realtime Database 的数据结构比 Firestore 简单得多,两者各有利弊。

对于实时数据库,如果您删除某个位置的数据,则该位置下的所有数据都会被删除。这与 Firestore 不同,在 Firestore 中,您需要删除要删除的文档的每个子集合中的每个文档。

简而言之就是这个firestore代码

const dbRef = admin.firestore().collection('stripe_customers');
const batch = admin.firestore().batch();
const paymetsMethodsSnapshot = await dbRef
  .doc(user.uid)
  .collection('payment_methods')
  .get();
paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
const paymentsSnapshot = await dbRef
  .doc(user.uid)
  .collection('payments')
  .get();
paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));

await batch.commit();

await dbRef.doc(user.uid).delete();

被压扁

const dbRef = admin.database().ref('stripe_customers');
await dbRef.child(user.uid).remove();

你的功能是什么:

exports.cleanupUser = functions.auth.user()
  .onDelete(async (user) => {
    const dbRef = admin.database().ref('stripe_customers');

    // Firestore's .get() is the same as RTDB's .once('value')
    // Firestore's .data() is the same as RTDB's .val()
    const customer = (await dbRef.child(user.uid).once('value')).val();

    // user data already deleted? do nothing rather than throw errors.
    if (customer === null) {
      return;
    }

    await stripe.customers.del(customer.customer_id);

    await dbRef.child(user.uid).remove();
  });

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2011-05-06
    • 2021-02-24
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2012-02-03
    • 2017-03-26
    • 2016-08-13
    相关资源
    最近更新 更多