Skip to content

Conversation

@laurenzlong
Copy link
Contributor

Description

Add support for create/update/delete event types for database functions.

Code sample

const functions = require('firebase-functions'); exports.createEvent = functions.database.ref('/path').onCreate(event => { console.log('Data created.'); }); exports.updateEvent = functions.database.ref('/path').onUpdate(event => { console.log('Data updated.'); }); exports.deleteEvent = functions.database.ref('/path').onDelete(event => { console.log('Data at path is deleted or its only child is deleted.'); }); // onWrite currently exists and will be kept: exports.writeEvent = functions.database.ref('/path').onWrite(event => { console.log('Data created, updated, or deleted'); }); 
@piuccio
Copy link

piuccio commented Jun 12, 2017

this would be amazing!

@laurenzlong
Copy link
Contributor Author

Thanks for the enthusiasm @piuccio and everyone else! We're still waiting on some backend changes, but hope to have this in the next SDK release.

Copy link
Member

@inlined inlined left a comment

Choose a reason for hiding this comment

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

LGTM, though we may want to merge this to a launch branch since the backend changes are still pending (there's been some testing infra issues holding up GCF right now)

} as any);
});

it('should interpolate params until the server does it', () => {
Copy link
Member

Choose a reason for hiding this comment

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

The server already does this; we should be able to yank this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@inlined inlined assigned laurenzlong and unassigned inlined Jun 16, 2017
@laurenzlong
Copy link
Contributor Author

@inlined I'll just leave this PR here then, and you can merge when the backend is ready. Since the other PR is in the other repo, using a launch branch will make things more complicated.

@laurenzlong laurenzlong assigned inlined and unassigned laurenzlong Jun 17, 2017
@inlined
Copy link
Member

inlined commented Jun 19, 2017

I meant that this could go to a launch since it needs to be held back and the other can go to master. NBD either way. The backend change has been merged into GCF so it should go live this week.

@laurenzlong laurenzlong merged commit 3533644 into master Jun 27, 2017
@laurenzlong laurenzlong deleted the ll-crud branch June 27, 2017 15:00
@laurenzlong laurenzlong restored the ll-crud branch June 28, 2017 17:56
laurenzlong added a commit that referenced this pull request Jun 28, 2017
laurenzlong added a commit that referenced this pull request Jun 28, 2017
@laurenzlong laurenzlong deleted the ll-crud branch July 7, 2017 18:09
@1hakr
Copy link

1hakr commented Jul 11, 2017

@laurenzlong , the deltasnapshot return null in onDelete() trigger. so the previous will not work A user has reported this and i have also faced this link.

I didnt want to create bug so commenting it here

@laurenzlong
Copy link
Contributor Author

@1hakr This is definitely a bug, thanks for reporting!

@laurenzlong
Copy link
Contributor Author

hey @1hakr we aren't able to reproduce this. Can you file an actual Github issue, and include repro instructions? Including exactly how you deleted the node (if it's via code, then add the client side code, if it's via CLI, include the command you used, if it's via console, then exactly what you clicked)

@CodingDoug
Copy link

@1hakr I was unable to reproduce in the most simple case of an onDelete that does nothing but log the previous value of the data that was deleted:

exports.onDelete = functions.database.ref("/messages/{messageId}").onDelete(event => { console.log("Stuff got deleted ", event.data.previous.val()) }) 

The correctly logs the deleted values for me. It sounds like you're seeing something different?

@1hakr
Copy link

1hakr commented Jul 11, 2017

Found the culprit. I was using exists() method to check if data exists or not before doing any operation. I removed the code and it works now. I thought exists() was supposed to tell you if data exists or not. My bad. 😐

Here is the code i was using.

exports.deletePriceAlert = functions.database.ref('/user_alerts/price/{uid}/{alertId}').onDelete(event => { const snapshot = event.data; const uid = event.params.uid; if (!snapshot.exists()) { return; } const comboKey = snapshot.previous.val().name; return admin.database().ref(`/alerts/price/${comboKey}/${uid}`).remove().then(result => { logInfo("Delete alert", {user: uid, key : comboKey}); }) .catch(error => { return reportError(error, {user: uid, type: 'database_write', context: 'delete alert'}); }); }); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants