Skip to content Skip to sidebar Skip to footer

Parse Server Atomic Increment Result

In my Parse backend I have an array that contains unique number codes, so users must not be able to get the same code twice. For that reason somewhere in a column of some table I a

Solution 1:

Increment does return the final value that was atomically incremented:

  1. First the unit test to show how it is used:

    fit('increment', (done) => {
        new Parse.Object('Counter')
          .set('value', 1)
          .save()
          .then(result => {
            console.log('just saved', JSON.stringify(result));
            return result
              .increment('value')
              .save();
          })
          .then(result => {
            console.log('incremented', JSON.stringify(result))
            expect(result.get('value')).toBe(2);
            done();
          })
          .catch(done.fail);
      });
    
  2. Behind the scenes, here's what's happening (if you're using mongo, there's similar for postgress too):

    • MongoAdapter turns into a mongo $inc operation which is returned
    • Mongo documentation explaining $inc which includes "$inc is an atomic operation within a single document."

Post a Comment for "Parse Server Atomic Increment Result"