cosa
Advanced tools
Comparing version 3.0.5 to 3.0.6
@@ -121,2 +121,50 @@ const assign = require('object-assign'); | ||
const _saveHelper = async function(context, options) { | ||
if (!context.isNew() && !context.isModified()) { | ||
return context; | ||
} | ||
let obj = context.toObject(); | ||
if ('function' === typeof context.beforeSave) { | ||
await context.beforeSave.apply(obj); | ||
} | ||
const value = await context._validate(obj, options); | ||
obj = EJSON.serialize(value); | ||
const original = obj.__original; | ||
delete obj.__modified; | ||
delete obj.__original; | ||
const newEtag = etag(JSON.stringify(obj)); | ||
const collection = definition.collection; | ||
let newOrUpdatedModel; | ||
if (!context.isNew()) { | ||
const query = { _id: obj._id, _etag: obj._etag }; | ||
obj._etag = newEtag; | ||
debug(`updating ${collection}: ${JSON.stringify(obj)}`); | ||
const result = await db.replace(collection, query, obj); | ||
if (result.matchedCount === 0) { | ||
throw errors.Conflict({ message: 'Document update conflict' }); | ||
} | ||
newOrUpdatedModel = context.set({ | ||
_etag: newEtag, | ||
__modified: [], | ||
__original: null | ||
}, { silent: true }); | ||
} else { | ||
obj._etag = newEtag; | ||
if (options.canPassID && options._id) { obj._id = options._id; } | ||
debug(`inserting into ${collection}: ${JSON.stringify(obj)}`); | ||
const result = await db.insert(collection, obj); | ||
debug(`insert into ${collection} successful`, result); | ||
obj = Array.isArray(result.ops) ? result.ops[0] : result.ops; | ||
newOrUpdatedModel = _create(obj, definition); | ||
} | ||
if ('function' === typeof newOrUpdatedModel.afterSave) { | ||
// should not be awaited | ||
const afterSave = newOrUpdatedModel.afterSave(original || null); | ||
if (pathOr(false, ['waitAfterSave'], options)) { | ||
await afterSave; | ||
} | ||
} | ||
return newOrUpdatedModel; | ||
}; | ||
definition.methods.equals = function(obj) { | ||
@@ -237,46 +285,12 @@ if (this === obj) { return true; } | ||
definition.methods.save = async function(options) { | ||
if (!this.isNew() && !this.isModified()) { | ||
return this; | ||
} | ||
let obj = this.toObject(); | ||
if ('function' === typeof this.beforeSave) { | ||
await this.beforeSave.apply(obj); | ||
} | ||
const value = await this._validate(obj, options); | ||
obj = EJSON.serialize(value); | ||
const original = obj.__original; | ||
delete obj.__modified; | ||
delete obj.__original; | ||
const newEtag = etag(JSON.stringify(obj)); | ||
const collection = definition.collection; | ||
let newOrUpdatedModel; | ||
options = Object.assign(options || {}, { canPassID: false }); | ||
return _saveHelper(this, options); | ||
}; | ||
definition.methods.saveWithId = async function(id, options) { | ||
if (!this.isNew()) { | ||
const query = { _id: obj._id, _etag: obj._etag }; | ||
obj._etag = newEtag; | ||
debug(`updating ${collection}: ${JSON.stringify(obj)}`); | ||
const result = await db.replace(collection, query, obj); | ||
if (result.matchedCount === 0) { | ||
throw errors.Conflict({ message: 'Document update conflict' }); | ||
} | ||
newOrUpdatedModel = this.set({ | ||
_etag: newEtag, | ||
__modified: [], | ||
__original: null | ||
}, { silent: true }); | ||
} else { | ||
obj._etag = newEtag; | ||
debug(`inserting into ${collection}: ${JSON.stringify(obj)}`); | ||
const result = await db.insert(collection, obj); | ||
debug(`insert into ${collection} successful`, result); | ||
obj = Array.isArray(result.ops) ? result.ops[0] : result.ops; | ||
newOrUpdatedModel = _create(obj, definition); | ||
throw new Error('saveWithId must receive a newly created object'); | ||
} | ||
if ('function' === typeof newOrUpdatedModel.afterSave) { | ||
// should not be awaited | ||
const afterSave = newOrUpdatedModel.afterSave(original || null); | ||
if (pathOr(false, ['waitAfterSave'], options)) { | ||
await afterSave; | ||
} | ||
} | ||
return newOrUpdatedModel; | ||
options = Object.assign(options || {}, { canPassID: true, _id: id }); | ||
return _saveHelper(this, options); | ||
}; | ||
@@ -283,0 +297,0 @@ |
{ | ||
"name": "cosa", | ||
"version": "3.0.5", | ||
"version": "3.0.6", | ||
"description": "Cosa Models for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
56066
1350
1