Skip to content

Commit

Permalink
set version if modified path and optCon is array
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Jun 13, 2023
1 parent 848f8b7 commit 40528ce
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions lib/model.js
Expand Up @@ -356,31 +356,12 @@ Model.prototype.$__handleSave = function(options, callback) {
const optionsWithCustomValues = Object.assign({}, options, saveOptions);
const where = this.$__where();
const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
console.log('what is optimisticConcurrency', optimisticConcurrency)
if (optimisticConcurrency) {
const key = this.$__schema.options.versionKey;
const val = this.$__getValue(key);
if (val != null) {
where[key] = val;
}
if (Array.isArray(optimisticConcurrency) && optimisticConcurrency.every(x => typeof x === 'string')) {
const modPaths = this.modifiedPaths();
console.log('what is modPaths', modPaths)
for (let i = 0; i < optimisticConcurrency.length; i ++) {
if (modPaths.includes(optimisticConcurrency[i])) {
// apply optimistic concurrency?
this.constructor.collection.findOne(where, optionsWithCustomValues)
.then(documentExists => {
const matchedCount = !documentExists ? 0 : 1;
callback(null, { $where: where, matchedCount });
})
.catch(callback);
} else {
continue;
}
}
return;
}
}
this.constructor.collection.findOne(where, optionsWithCustomValues)
.then(documentExists => {
Expand Down Expand Up @@ -737,7 +718,17 @@ Model.prototype.$__delta = function() {

const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
if (optimisticConcurrency) {
this.$__.version = dirty.length ? VERSION_ALL : VERSION_WHERE;
if (Array.isArray(optimisticConcurrency)) {
const modPaths = this.modifiedPaths();
for(let i = 0; i < modPaths.length; i++) {

Check failure on line 723 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected space(s) after "for"
if (modPaths.includes(optimisticConcurrency[i])) {
this.$__.version = dirty.length ? VERSION_ALL : VERSION_WHERE;
break;
}
}
} else {
this.$__.version = dirty.length ? VERSION_ALL : VERSION_WHERE;
}
}

if (!dirty.length && VERSION_ALL !== this.$__.version) {
Expand Down

0 comments on commit 40528ce

Please sign in to comment.