Loopback: Atomic read and update

2019-07-13 03:27发布

问题:

is there a way to implement something like this in loopback?

LOCK
 READ
 INCREMENT
UNLOCK

I would like to keep counters as database values, each key is a counter (or a setting), and they shouldn't accessed my multiple requests at the same time.

Also this should work for local requests too (no remoteHooks)

Thanks

回答1:

If you are using the mongoDB connector, this is supported by extended operators.

MyModel.updateAll(
  { id: 123' },
  { '$inc': { myproperty: 1 }}, // increment myproperty by 1
  { allowExtendedOperators: true }
);

Otherwise, you can use transactions as a workaround for some connectors.