Loopback: Atomic read and update

2019-07-13 03:17发布

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条回答
迷人小祖宗
2楼-- · 2019-07-13 04:14

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.

查看更多
登录 后发表回答