npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rodrigogs/dynamoose-plugin-optimistic-locking

v0.0.1

Published

A Dynamoose plugin that implements optimistic locking for db writes

Downloads

3

Readme

Dynamoose Plugin - Optimistic Locking

NPM Version NPM Downloads Build Status Coverage Status License: MIT

Dynamoose is a modeling tool for Amazon's DynamoDB (https://github.com/dynamoosejs/dynamoose). Optimistic Locking is a technique that protects data integrity in a concurrent enviroment, ensuring multiple write operations on the same data do not overwrite each other (by default last operation wins)

Getting Started

Installation

$ npm i @rodrigogs/dynamoose-plugin-optimistic-locking

Example

const {
  OptimisticLockingPlugin,
  OptimisticLockException,
} = require('@rodrigogs/dynamoose-plugin-optimistic-locking');

const BookCollectionSchema = new dynamoose.Schema({
  author: {
    type: String,
    hashKey: true,
  },
  books: [String],
  derivedWorks: [String],
});
const BookCollection = dynamo.model("BookCollection", BookCollectionSchema)
BookCollection.plugin(OptimisticLockingPlugin, { fetchItemOnWriteError: true });

const asoiaf = await BookCollection.create({
  author: 'George R. R. Martin',
  books: ['A Game of Thrones', 'A Clash of Kings', 'A Storm of Swords'],
  derivedWorks: ['Game of Thrones (TV Series)'],
});
asoiaf.getVersion(); // Returns 1.
...
let updatedCollection;
while (!updatedCollection) {
  asoiaf.push('A Feast For Crows');
  try {
    updatedCollection = await BookCollection.put(asoiaf);
  } catch (err) {
    if (!(err instanceof OptimisticLockException)) throw err;
    asoiaf = err.itemInDb;
  }
}

API

Plugin Options

BookCollection.plugin(OptimisticLockingPlugin, {/* Plugin Options */});

fetchItemOnWriteError: boolean (default: false)

If set to true, Optimistic Locking Plugin will perform a database read automatically if conditional update fails. If the version of the item in the database is newer than the one being saved, it will throw an instance of OptimisticLockException. The item from the database is stored as itemInDb property on the exception object. If set to false, the original ConditionalCheckFailedException is thrown, and there is no guarantee that optimistic locking is the cause of the received exception. Setting to true is recommended to get the most out of the plugin.

attributeName: string (default: '__version')

DynamoDB attribute name for storing version number.

attributeSymbol: symbol

Can be used to override default symbol for accessing version prop on an item. Using getVersion() and setVersion() methods is recommended instead.

Static methods

Model.putNextVersion(item, updateFunction[, options]) Sends put request in a loop fetching newest item version every iteration, until request succeeds or max attempts reached (if specified). Requires fetchItemOnWriteError plugin option to be set to true.

BookCollection.plugin(OptimisticLockingPlugin, { fetchItemOnWriteError: true });

const updatedAsoiaf = await BookCollection.putNextVersion(
  asoiaf,
  (item) => { item.derivedWorks.push('Game of Thrones: A Telltale Games Series'); },
  { maxAttempts: 3 }
);

| Parameter Name | Type | Description | | | ---- | ---- | ----------- | -------- | | item | object | item to update and save to the database | required | | updateFunction | function | applies updates to the item, will be called every iteration | required | | options | object | extra options |   | | options.maxAttempts | number | by default operation will run until the item is saved, but will fail after max attempts have been reached if this option is specified |   |

Instance methods

item.getVersion() Returns current item version

asoiaf.getVersion();

item.setVersion(number) Overwrites item version with custom value. Note that this value will be incremented before the item is saved.

Roadmap

  • Better TypeScript support
  • Provide option to disable optimistic locking for invidual put/batchPut/update calls

License

MIT