const 集合 = ['潜在客户', '客户','分支机构','用户'];
//Function to sync Firestore collections to typesense schema
async function syncFirestoreToTypesense(companyId, collections) {
console.log(`Syncing ${collections} for company ${companyId}`);
admin.initializeApp();
const firestore = admin.firestore();
for (const collection of collections) {
const firestoreCollection = firestore.collection(`Company/${companyId}/${collection}`);
const typesenseCollection = typesenseClient.collections(`${companyId}.${collection}`);
console.log(`Syncing ${collection} for company ${companyId}`);
// Subscribe to Firestore events using onWrite
exports[`syncFirestoreToTypesense_${companyId}_${collection}`] = functions.firestore
.document(`Company/${companyId}/${collection}/{documentId}`)
.onWrite(async (change, context) => {
const documentId = context.params.documentId;
const documentData = change.after.data();
console.log(documentData);
console.log(`got id - ${documentId}`);
try {
if (change.after.exists) {
// Upsert document to Typesense
await typesenseCollection.documents().upsert({
...documentData,
id: documentId,
});
console.log(`Synced ${collection} document - ${documentId} for company ${companyId}`);
} else {
// Document removed, delete from Typesense
await typesenseCollection.documents(documentId).delete();
console.log(`Deleted ${collection} document ${documentId} for company ${companyId}`);
}
} catch (error) {
console.error(`Error syncing ${collection} document ${documentId} for company ${companyId}`, error);
}
});
console.log(`Subscribed to ${collection} events for company ${companyId}`);
}
}
它应该在谷歌控制台中为多个集合创建一个云函数,每当我在 firestore 中进行更改时,它应该触发该函数,并且 console.log 应该运行并同时将数据添加到 typesense 数据库中...
请帮帮我!
在
for (const collection of collections)
循环中声明您的云函数不起作用。
据我所知有两种可能的方法:
exports syncFirestoreToTypesenseLeads = functions.firestore
.document(`Company/{companyId}/Leads/{documentId}`)
.onWrite(async (change, context) => {...});
exports syncFirestoreToTypesenseCustomers = functions.firestore
.document(`Company/{companyId}/Customers/{documentId}`)
.onWrite(async (change, context) => {...});
// ... And so on
请注意,您可以在函数中包含内部 Cloud Function 业务逻辑,如下所示:
async function typesenseCFBusinessLogic(...) {...};
exports syncFirestoreToTypesenseLeads = functions.firestore
.document(`Company/{companyId}/Leads/{documentId}`)
.onWrite(async (change, context) => {
await typesenseCFBusinessLogic(...);
return null;
});
exports syncFirestoreToTypesenseCustomers = functions.firestore
.document(`Company/{companyId}/Customers/{documentId}`)
.onWrite(async (change, context) => {
await typesenseCFBusinessLogic(...);
return null;
});
// ... And so on
这里的想法是声明一个独特的云函数,该函数会针对所有集合中的所有更改而触发,并检查触发它的文档是否位于所需的集合之一内。
大致如下:
exports syncFirestoreToTypesense = functions.firestore
.document(`Company/{companyId}/{collId}/{documentId}`)
.onWrite(async (change, context) => {
const collections = ['Leads', 'Customers','Branches','users'];
if (collections.includes(context.params.collId)) {
// ... Business logic
}
return ...;
});