@shieldsbetter/sb-optimistic-entities

0.0.13 • Public • Published

sb-optimistic-entities

Optimistic locking library for MongoDb. Perform updates with plain Javascript.

TL;DR

const assert = require('assert');
const OptEntCollection = require('@shieldsbetter/sb-optimistic-entities');
const { MongoClient } = require('mongodb');

async main() {
    const dbClient = await MongoClient
            .connect(process.env.MONGOD_URI)
            .then(mc => mc.db('TestDb'));
    const petsCollection = new OptEntCollection(dbClient.collection('Pets'));

    await petsCollection.insertOne({
        _id: 'Ellie',
        breed: 'Azawakh',
        age: 6
    });

    // If there is a conflicting update, this will retry automatically
    // with the new value.
    await petsCollection.updateOne({ _id: 'Ellie' }, ellie => {
        ellie.age++;
        return ellie;
    });

    const entity = await petsCollection.findOne({ _id: 'Ellie' });
    assert.deepEqual(entity, {
        _id: 'Ellie',
        breed: 'Azawakh',
        age: 7;
    });
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.13
    0
    • latest

Version History

Package Sidebar

Install

npm i @shieldsbetter/sb-optimistic-entities

Weekly Downloads

0

Version

0.0.13

License

ISC

Unpacked Size

15.7 kB

Total Files

12

Last publish

Collaborators

  • shieldsbetter