Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
/ mersion Public archive

Retry Mongoose saves that suffer from conflicts via VersionErrors

Notifications You must be signed in to change notification settings

digit-co/mersion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mersion

Retry Mongoose saves that suffer from conflicts via VersionErrors

const mongoose = require('mongoose')
const Mersion = require('mersion')

const Book = mongoose.model('Book', new Schema({
  title: String,
  tags: [String]
}))

(async () => {
  const mersion = new Mersion({
    loadFn: () => Book.findById(bookId),
    saveFn: (book) => {
      // mutating array has the possibility of causing a VersionError
      book.tags = ['fiction']
      return book.save()
    },
    retries: 2
  })

  // Calls `loadFn`, followed by `saveFn()`.
  // If a VersionError occurs, will retry `retries` times
  const savedBook = await mersion.save()
})()

About

Retry Mongoose saves that suffer from conflicts via VersionErrors

Resources

Stars

Watchers

Forks

Packages

No packages published