Giter VIP home page Giter VIP logo

Comments (16)

hiranya911 avatar hiranya911 commented on July 24, 2024

Just to make sure I understood correct, how would it be different from the existing transaction() or set_if_unchanged() methods? By performing an update instead of a full overwrite (HTTP PATCH as opposed to PUT)?

Not sure if the server API supports that kind of an operation. @caseycrogers, does this sound like something we can do?

from firebase-admin-python.

alexanderwhatley avatar alexanderwhatley commented on July 24, 2024

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

I'm not convinced that this can be implemented without server-side support. For instance suppose you do:

data, etag = ref.get(etag=True)
make_changes_to(data)
if etag ==  ref.etag():
  # Foo
  ref.update(data)

If another client updates data in the server at #Foo, we will overwrite the change leading to the lost-update problem. We need an atomic, server-supported update-if-unchanged operation for this.

@caseycrogers @rockwotj Does the existing conditional requests support provide something like that?

from firebase-admin-python.

alexanderwhatley avatar alexanderwhatley commented on July 24, 2024

@hiranya911, any updates on this? Also, is there documentation about how transactions are done in the new Firestore database? I wasn't able to find anything. Thanks.

from firebase-admin-python.

rockwotj avatar rockwotj commented on July 24, 2024

Sorry I missed this. What you do you by update-if-unchanged? You want to two 2 writes and make sure no data is changed inbetween them you just need to set the same 'X-Firebase-ETag: true' header in the write and you'll get the ETag back just if you did a read there right after the operation is done. You can use that etag in the next write.

As for Firestore transactions, here are the python docs (which I guess that's what you want): https://googlecloudplatform.github.io/google-cloud-python/latest/firestore/transaction.html

Firestore transactions are much more powerful and let you do full read/write operations atomically.

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

Hi @rockwotj. The SDK currently supports a set_if_unchanged() method which handles this. I guess the question is can we support a similar update_if_unchanged() method. In REST terms can we do a conditional PATCH request?

from firebase-admin-python.

rockwotj avatar rockwotj commented on July 24, 2024

Yeah you can do it with PATCH too

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

Thanks @rockwotj.

@alexanderwhatley Would you be interested in providing us a PR? I assume it will be very similar to the existing set_if_unchanged(), except using HTTP patch method instead of put.

from firebase-admin-python.

alexanderwhatley avatar alexanderwhatley commented on July 24, 2024

Sure @hiranya911, I'll get something up by tomorrow hopefully.

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

And on a tangential, Firestore support for Python admin SDK is being developed at #79.

from firebase-admin-python.

caseycrogers avatar caseycrogers commented on July 24, 2024

A quick correction, none of the conditional request features (ie ETags, if-none-match) are supported with the PATCH method.
This is because, with PATCH, it's unclear what should be used to compute the ETag.

I'm not entirely sure what you mean by "set" vs. "update". Do you mean PUT vs. PATCH? If so, any PATCH requests can be converted into one or more PUT requests and you can use the set_if_unchanged method.

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

Oh! I was afraid of this.

And just to clarify @caseycrogers, you're right.

  • set = PUT = total overwrite
  • update = PATCH = selective update of children

The problem with implementing update_if_unchanged() as a series of PUT operations is, then it is no longer atomic. I think @alexanderwhatley is trying to read a parent object along with its etag, and then atomically update some of its children. Something like:

data, etag = ref.get(etag=True)
update = {'foo' : 'bar', 'baz' : data.get('baz') + 1}
ref.update_if_unchanged(update, etag) # This should ideally be a single REST call

from firebase-admin-python.

caseycrogers avatar caseycrogers commented on July 24, 2024

@alexanderwhatley can we get some more context There might be some easy ways to rewrite your code not to depend on patch depending on how your data is structured and what exactly you are trying to achieve.

from firebase-admin-python.

alexanderwhatley avatar alexanderwhatley commented on July 24, 2024

@caseycrogers, I am trying to do what hiranya is saying, which doesn't appear possible right now. I most likely will just switch to using Firestore anyways, which appears to support this kind of transaction, so this should be a nonissue.

from firebase-admin-python.

caseycrogers avatar caseycrogers commented on July 24, 2024

What solution will work best for you will depend heavily on your data structure. It may be that the Database's transactions implementation will suit your needs well without the need for an update/PATCH.

However, as you said, this may be a non-issue given that Firestore's transactions support update operations. Let me know if you decide to proceed with this in the Database.

from firebase-admin-python.

hiranya911 avatar hiranya911 commented on July 24, 2024

I'm closing this since the backend API doesn't seem to support this. We can revisit this if this turns out to be a critical use case.

from firebase-admin-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.