Last active
July 18, 2017 04:54
-
-
Save teebu/8fb6f4f19ae1db4f27e1305b6c5d421c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const configuration = { | |
db: { | |
name: 's3-db', | |
environment: 'dev', | |
pageSize : 10, | |
allowDrop: false | |
}, | |
provider: { | |
name: 's3', | |
region: 'us-east-1' | |
}, | |
collections: { | |
testdb: { | |
collideOnMissmatch: true, | |
onlyUpdateOnMD5Change: false, | |
id: { | |
propertyName: '_id', | |
}, | |
validator: document => apps_validator(document) ? Promise.resolve(document) : Promise.reject("validator fail"), | |
collisionValidator: (thisMetadata, thatMetadata) => { | |
const isValid = !thatMetadata || !thatMetadata._rev || thatMetadata._rev == thisMetadata._rev; | |
console.log('rev: target rev: %s, this rev: %s', thatMetadata._rev, thisMetadata._rev) | |
if (isValid) | |
return isValid; | |
}, | |
metadataUpdate: metadata => { | |
metadata._rev = (metadata._rev) ? (parseInt(metadata._rev) + 1).toString() : "1"; | |
//console.log('setting metadata', metadata) | |
return metadata; | |
} | |
} | |
}, | |
serializer: { | |
serialize: data => { | |
//console.log('serializing') | |
data._rev = data._rev ? (parseInt(data._rev) + 1).toString() : "1"; | |
//console.log('new data:', data) | |
return JSON.stringify(data) | |
}, | |
deserialize: data => JSON.parse(data) | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isCollided = (document, argMetatada={}) => { | |
if(document.getId || document[idPropertyName]){ | |
const id = document.getId ? document.getId() : document[idPropertyName]; | |
const metadata = document.getId ? Utils.getMetaData(document) : argMetatada; | |
return collectionProvider.getDocumentHead(fqn,id) | |
.then( head => { | |
const targetMetaData = document.getId ? provider.document.buildDocumentMetaData(head) : head.Metadata; | |
let hasChanged = false; | |
let isValid = false | |
/* | |
* Extension point that allows additional checking on the metadata of the document | |
* and the corresponding documentHeader. | |
**/ | |
if(collisionValidator){ | |
isValid = collisionValidator(metadata,targetMetaData,document); | |
if (isValid) return Promise.resolve(document); | |
else return Promise.reject('collisionValidator, failed to validate.'); | |
} | |
/* | |
* The targetMetaData comes from a headCheck on the object being | |
* overwritten. So if the MD5 on the __meta of the current record | |
* matches the target MD5, the underlying object is likely not | |
* modified. | |
* | |
* Md5 does not always get returned. | |
*/ | |
if(!hasChanged && targetMetaData.md5 && targetMetaData.md5 !== metadata.md5){ | |
hasChanged = true; | |
} | |
if(!hasChanged && targetMetaData.eTag !== metadata.eTag){ | |
hasChanged = true; | |
} | |
if(hasChanged){ | |
return Promise.reject('Collision, the document has been modified.'); | |
} | |
return Promise.resolve(document); | |
}) | |
.catch( error => 'NotFound' === error.code ? Promise.resolve(document) : handleError(error) ) | |
} | |
return Promise.resolve(document); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment