最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google cloud firestore - How to use Expression in Firebase Trigger Functions - Stack Overflow

programmeradmin5浏览0评论

I want to create only one function to be triggered by some of my Collections, it looks like it's possible with the use of CEL braced expression or REGEX, however I failed to figure out the correct form to properly trigger the function.

For example if I have 6 collections "Col1 through Col6", how can modify this function to be triggered by Col3 and Col5 only

export const writeTrigger = onDocumentWritten({document:"{collectionName}/{docId}"}, triggerFunctionHandler)

Reference Firebase Functions Type Definition

Edit

Line 183 of This Document

/**
 * Event handler that triggers when a document is created, updated, or deleted in Firestore.
 *
 * @param documentOrOpts - Options or a string document path.
 * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
 */
export function onDocumentWritten<Document extends string>(
  documentOrOpts: Document | DocumentOptions<Document>,
  handler: (
    event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>
  ) => any | Promise<any>
): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>> {
  return onChangedOperation(writtenEventType, documentOrOpts, handler);
}

Line 147

/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace.  */
export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
  /** The document path */
  document: Document | Expression<string>;
  /** The Firestore database */
  database?: string | Expression<string>;
  /** The Firestore namespace */
  namespace?: string | Expression<string>;
}

which points to Firebase Functions Type Definitions line 25

/*
 * A CEL expression which can be evaluated during function deployment, and
 * resolved to a value of the generic type parameter: i.e, you can pass
 * an Expression<number> as the value of an option that normally accepts numbers.
 */
export abstract class Expression<T extends string | number | boolean | string[]>

And it looks to me that this can use CEL to trigger selective collections, and I may be very much reading this wrong.

I want to create only one function to be triggered by some of my Collections, it looks like it's possible with the use of CEL braced expression or REGEX, however I failed to figure out the correct form to properly trigger the function.

For example if I have 6 collections "Col1 through Col6", how can modify this function to be triggered by Col3 and Col5 only

export const writeTrigger = onDocumentWritten({document:"{collectionName}/{docId}"}, triggerFunctionHandler)

Reference Firebase Functions Type Definition

Edit

Line 183 of This Document

/**
 * Event handler that triggers when a document is created, updated, or deleted in Firestore.
 *
 * @param documentOrOpts - Options or a string document path.
 * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
 */
export function onDocumentWritten<Document extends string>(
  documentOrOpts: Document | DocumentOptions<Document>,
  handler: (
    event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>
  ) => any | Promise<any>
): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>> {
  return onChangedOperation(writtenEventType, documentOrOpts, handler);
}

Line 147

/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace.  */
export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
  /** The document path */
  document: Document | Expression<string>;
  /** The Firestore database */
  database?: string | Expression<string>;
  /** The Firestore namespace */
  namespace?: string | Expression<string>;
}

which points to Firebase Functions Type Definitions line 25

/*
 * A CEL expression which can be evaluated during function deployment, and
 * resolved to a value of the generic type parameter: i.e, you can pass
 * an Expression<number> as the value of an option that normally accepts numbers.
 */
export abstract class Expression<T extends string | number | boolean | string[]>

And it looks to me that this can use CEL to trigger selective collections, and I may be very much reading this wrong.

Share Improve this question edited Mar 26 at 18:08 Wisam Jbori asked Mar 26 at 7:54 Wisam JboriWisam Jbori 1131 silver badge8 bronze badges 2
  • You say you have 6 top-level collections (Col1 through Col6), and you need to trigger a function each time a new document within Col3 and Col5 is written, right? – Alex Mamo Commented Mar 26 at 8:57
  • @AlexMamo correct! – Wisam Jbori Commented Mar 26 at 18:08
Add a comment  | 

1 Answer 1

Reset to default 1

The wildcards used in functions definitions don't accept CEL expressions or regular expressions. All you can do is use a named wildcard on the entire part of a document path (collection or document) using a placeholder in curly braces. The code you're citing isn't relevant to the matching of these named wildcard paths.

The limitation on these wildcards isn't part of the Firebase CLI at all - it's a limitation in Google Cloud Platform internally. You're bound to this limitation even if you're using GCP Firestore functions directly (read the part of that doc that discusses wildcards and paramters) and not using the Firebase tools that wrap them.

If you want a function to trigger on only two specifically named collections, you will need to declare two different triggers, each using that name coded into the path string. They can both call into the same JS implementation function if you want, but you can't avoid declaring and exporting the triggers separately.

See also:

  • Cloud function trigger for multiple collections which start with a same text

  • Cloud Functions for Firebase "Create" Trigger - multiple collections

发布评论

评论列表(0)

  1. 暂无评论