Parse-SDK-JS icon indicating copy to clipboard operation
Parse-SDK-JS copied to clipboard

Performing batch transactions

Open JeromeDeLeon opened this issue 5 years ago • 20 comments

Is your feature request related to a problem? Please describe. This was already supported in the PServer, but in JS-SDK, it limits us to only perform either save or delete but not both.

Describe the solution you'd like It would be better if we could constructr a batch operation like:

await Parse.Object.performTransaction(
  [
    { method: "PUT", objects: [PObject] },
    { method: "DELETE, object: PObject },
    { method: "POST", object: [PObject, PObject] }
  ],
  { transaction: true, useMasterKey: true }
);

and performs that sequentially.

Describe alternatives you've considered

Additional context Also, using relations when doing batch operation is not allowed because you cannot add something to relation when it is new. I don't know if there's already a solution to this but my current implementation is to use forkJoin([DeleteObjects(), SaveObjects()]) using RxJS.

JeromeDeLeon avatar Sep 13 '19 03:09 JeromeDeLeon

I like the idea of having some way to send a batch from the JS SDK but I am not sure if the proposed API is the best one. I'd probably go with something like this:

const transaction = Parse.Object.createTransaction();
someObject.destroy({ useMasterKey: true, transaction });
someObject.save({ transaction });
try {
  transaction.commit();
} catch (e) {
  transaction.abort();
}

@JeromeDeLeon is this something that you'd be willed to tackle?

davimacedo avatar Sep 13 '19 04:09 davimacedo

That would be nice too. The reason I suggested that is because I want to use what is already existed in the codebase of PS. We could make use of createTransactionalSession from the PS to await on it for session request but how could we incorporate session AKA transactionToken for every write or even read operations?

JeromeDeLeon avatar Sep 13 '19 14:09 JeromeDeLeon

Wait. I just saw that PS already incorporated the transactionalSession. Is there a way for PS to send transactionalSession to SDK? same with commit and abort? something like: GET "/transaction", POST "/transaction/(commit | abort)

JeromeDeLeon avatar Sep 13 '19 15:09 JeromeDeLeon

No.. there is no way to do it. Since the Parse Server can run in multiple processes at the same time, there is no warranty that two requests will go to the same process. In the case they go to different processes, you will not be able to use the same MongoDB session.

So, the solution is actually use the /batch endpoint. I mean... when the JS SDK call transaction.commit();, we will have to put all requests together and send in a single request through the /batch endpoint. Got it?

davimacedo avatar Sep 13 '19 18:09 davimacedo

// create unique ID or something
const transaction = Parse.Obejct.createTransaction();

// These operations will not be run until commit() is called
// maybe save it to array of operations
someObject.destroy({ useMasterKey: true, transaction });
someObject.save({ transaction });
 
try {
// since /batch already have abort operations no need to manually abort
transaction.commit();
} catch(e) {
console.log(e);
}

What I have in mind is that when I call createTransaction, transaction will receive an object containing the token and commit and for each operations, we need to associate that with the transaction and inside, it will not be save but instead put in an array of requests to be collect later when commit is called?

JeromeDeLeon avatar Sep 14 '19 02:09 JeromeDeLeon

Yes. That's the idea. We will probably need the abort in the client side as well to revert the object states.

davimacedo avatar Sep 16 '19 07:09 davimacedo

Currently with the JS SDK (master, 2.11.0), there is no way to pass parameter "transaction" to the Parse Server. (Correct me if I am wrong) so the transactional stuff (which have been done on Parse Server) does not work with the JS SDK.

My plan of attack is to make a PR which includes:

  1. Adding an option in saveAll, destroyAll and pass it to Parse Server

Does this make sense? @davimacedo

mfkenson avatar Feb 15 '20 02:02 mfkenson

// create unique ID or something
const transaction = Parse.Obejct.createTransaction();

// These operations will not be run until commit() is called
// maybe save it to array of operations
someObject.destroy({ useMasterKey: true, transaction });
someObject.save({ transaction });
 
try {
// since /batch already have abort operations no need to manually abort
transaction.commit();
} catch(e) {
console.log(e);
}

What I have in mind is that when I call createTransaction, transaction will receive an object containing the token and commit and for each operations, we need to associate that with the transaction and inside, it will not be save but instead put in an array of requests to be collect later when commit is called?

I am trying to use a similar approach on server-side Parse.Cloud function! Getting the database object in cloud function and manually invoking createTransactionalSession at the beginning of my cloud function. And then invoking

  • commitTransactionalSession
  • abortTransactionalSession after some CRUD operations.

I am stuck now and I have no idea why this does not work as expected...Perhaps the cloud function and the CRUD handlers they are not pointing to the same database object..?

mfkenson avatar Feb 15 '20 03:02 mfkenson

I think that your original idea of changing saveAll, destroyAll makes sense, will be a lot easier and a very good first step. Why don't you start with them?

davimacedo avatar Feb 17 '20 08:02 davimacedo

@davimacedo Yes I did give a try (#1090 ). Hope this is a good start to contribute.

'changing saveAll, destroyAll' in the JS SDK should be the top priority to support what have been done on Parse Server batch endpoint.

I might get back to the cloud function approach later.

mfkenson avatar Feb 19 '20 21:02 mfkenson

Great job. It is a good start for sure. I've just left a comment over there.

davimacedo avatar Feb 21 '20 22:02 davimacedo

Any updates on this issue?

itsnitigya avatar Nov 08 '20 11:11 itsnitigya

Is there any update on this issue or a workaround? We have several use cases where transactions would be necessary (eg update 2 objects at the same time or rollback)

jonas-db avatar May 01 '21 12:05 jonas-db

I am also interested in an update🙂

danielmalmros avatar Jun 28 '21 17:06 danielmalmros

interested.

Kingtous avatar Aug 19 '21 01:08 Kingtous

If anyone interested in picking up @mfkenson's https://github.com/parse-community/Parse-SDK-JS/pull/1090?

mtrezza avatar Aug 19 '21 08:08 mtrezza

I'm interested in this too.

JoeProgram avatar Sep 30 '21 17:09 JoeProgram

Hello everyone, do you have any updates on this? By the way, it sounds like an amazing feature!

gtzinos avatar May 02 '23 08:05 gtzinos

I'm interested in this too.

alljinx avatar May 05 '23 08:05 alljinx

Added a bounty to this feature due to high demand.

mtrezza avatar May 07 '23 15:05 mtrezza