Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add firestore 2nd gen triggers #1358

Merged
merged 17 commits into from Mar 22, 2023
Merged

Add firestore 2nd gen triggers #1358

merged 17 commits into from Mar 22, 2023

Conversation

colerogers
Copy link
Contributor

@colerogers colerogers commented Mar 10, 2023

Add support for 2nd gen firestore triggers!

import {
  onDocumentWritten,
  onDocumentCreated,
  onDocumentUpdated,
  onDocumentDeleted,
  Change
  FirestoreEvent
} from "firebase-functions/v2/firestore";

export const a = onDocumentWritten("foo/{bar}", (event: FirestoreEvent<Change<DocumentSnapshot>, { bar: string }>) => {});

export const b = onDocumentCreated("foo/{bar}", (event: FirestoreEvent<QueryDocumentSnapshot, { bar: string }>) => {});

export const c = onDocumentUpdated("foo/{bar}", (event: FirestoreEvent<Change<QueryDocumentSnapshot>, { bar: string }>) => {});

export const d = onDocumentDeleted("foo/{bar}", (event: FirestoreEvent<QueryDocumentSnapshot, { bar: string }>) => {});

package.json Outdated Show resolved Hide resolved
}
try {
const dataBuffer = Buffer.from(data);
const anyDecoded = Any.decode(dataBuffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh strange that this middle step is needed. Do you know why this is required? (kinda reminds me of TS val as unknown as string trick)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firestore is sending the payload as an encoded Any.value, not sure why they chose this, but it was in their original dd

src/common/providers/firestore.ts Show resolved Hide resolved
}

/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS Q: I'm not sure why we need Document extends string here. Isn't Document always going to be string anyway?

I see similar pattern in our v1 code (Path extends string) and I'm not sure why that's there too 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used so we can enforce strong typing in our code - https://stackoverflow.com/questions/57342018/extending-a-string-in-typescript

I think Thomas was the one that actually put the Path stuff in during a refactor.

src/v2/providers/firestore.ts Show resolved Hide resolved
@colerogers colerogers merged commit 3efab1d into master Mar 22, 2023
@patrikjuvonen
Copy link

This fixed issue #1213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants