|
|
@ -214,91 +214,63 @@ function compareThings (a, b) { |
|
|
|
// Updating documents
|
|
|
|
// Updating documents
|
|
|
|
// ==============================================================
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastStepModifierFunctions.$set = function (obj, field, value) { |
|
|
|
|
|
|
|
obj[field] = value; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findLastModificationStep (modifier, obj, field, value) { |
|
|
|
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
|
|
|
|
lastStepModifierFunctions[modifier](obj, field, value); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
obj[fieldParts[0]] = obj[fieldParts[0]] || {}; |
|
|
|
|
|
|
|
modifierFunctions[modifier](obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set field to value in a model |
|
|
|
* The signature of modifier functions is as follows |
|
|
|
* Create it if it doesn't exist |
|
|
|
* Their structure is always the same: recursively follow the dot notation while creating |
|
|
|
* @param {Object} obj The model to set a field for |
|
|
|
* the nested documents if needed, then apply the "last step modifier" |
|
|
|
|
|
|
|
* @param {Object} obj The model to modify |
|
|
|
* @param {String} field Can contain dots, in that case that means we will set a subfield recursively |
|
|
|
* @param {String} field Can contain dots, in that case that means we will set a subfield recursively |
|
|
|
* @param {Model} value |
|
|
|
* @param {Model} value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
modifierFunctions.$set = function (obj, field, value) { |
|
|
|
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
// Set a field to a new value
|
|
|
|
obj[fieldParts[0]] = value; |
|
|
|
lastStepModifierFunctions.$set = function (obj, field, value) { |
|
|
|
} else { |
|
|
|
obj[field] = value; |
|
|
|
obj[fieldParts[0]] = obj[fieldParts[0]] || {}; |
|
|
|
|
|
|
|
modifierFunctions.$set(obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Push an element to the end of an array field
|
|
|
|
|
|
|
|
lastStepModifierFunctions.$push = function (obj, field, value) { |
|
|
|
|
|
|
|
// Create the array if it doesn't exist
|
|
|
|
|
|
|
|
if (!obj.hasOwnProperty(field)) { obj[field] = []; } |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
if (!util.isArray(obj[field])) { throw "Can't $push an element on non-array values"; } |
|
|
|
* Push an element at the last place of an array |
|
|
|
obj[field].push(value); |
|
|
|
* @param {Object} obj The model to set a field for |
|
|
|
|
|
|
|
* @param {String} field Can contain dots, in that case that means we will set a subfield recursively |
|
|
|
|
|
|
|
* @param {Model} value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
modifierFunctions.$push = function (obj, field, value) { |
|
|
|
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
|
|
|
|
// Create the array if it doesn't exist
|
|
|
|
|
|
|
|
if (!obj.hasOwnProperty(fieldParts[0])) { obj[fieldParts[0]] = []; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!util.isArray(obj[fieldParts[0]])) { throw "Can't $push an element on non-array values"; } |
|
|
|
|
|
|
|
obj[fieldParts[0]].push(value); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
obj[fieldParts[0]] = obj[fieldParts[0]] || {}; |
|
|
|
|
|
|
|
modifierFunctions.$push(obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Increment a numeric field's value
|
|
|
|
/** |
|
|
|
lastStepModifierFunctions.$inc = function (obj, field, value) { |
|
|
|
* Increase (or decrease) a 'number' field |
|
|
|
|
|
|
|
* Create and initialize it if needed |
|
|
|
|
|
|
|
* @param {Object} obj The model to set a field for |
|
|
|
|
|
|
|
* @param {String} field Can contain dots, in that case that means we will set a subfield recursively |
|
|
|
|
|
|
|
* @param {Model} value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
modifierFunctions.$inc = function (obj, field, value) { |
|
|
|
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof value !== 'number') { throw value + " must be a number"; } |
|
|
|
if (typeof value !== 'number') { throw value + " must be a number"; } |
|
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
if (typeof obj[field] !== 'number') { |
|
|
|
if (typeof obj[fieldParts[0]] !== 'number') { |
|
|
|
if (!_.has(obj, field)) { |
|
|
|
if (!_.has(obj, fieldParts[0])) { |
|
|
|
obj[field] = value; |
|
|
|
obj[fieldParts[0]] = value; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw "Don't use the $inc modifier on non-number fields"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
obj[fieldParts[0]] += value; |
|
|
|
throw "Don't use the $inc modifier on non-number fields"; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
obj[fieldParts[0]] = obj[fieldParts[0]] || {}; |
|
|
|
obj[field] += value; |
|
|
|
modifierFunctions.$inc(obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Given its name, create the complete modifier function
|
|
|
|
|
|
|
|
function createModifierFunction (modifier) { |
|
|
|
|
|
|
|
return function (obj, field, value) { |
|
|
|
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
|
|
|
|
lastStepModifierFunctions[modifier](obj, field, value); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
obj[fieldParts[0]] = obj[fieldParts[0]] || {}; |
|
|
|
|
|
|
|
modifierFunctions[modifier](obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Actually create all modifier functions
|
|
|
|
|
|
|
|
Object.keys(lastStepModifierFunctions).forEach(function (modifier) { |
|
|
|
|
|
|
|
modifierFunctions[modifier] = createModifierFunction(modifier); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Modify a DB object according to an update query |
|
|
|
* Modify a DB object according to an update query |
|
|
|