Handle models (i.e. docs) Serialization/deserialization Copying Querying, update
* [model](#module_model) * _static_ * [.checkObject(obj)](#module_model.checkObject) * [.serialize(obj)](#module_model.serialize) ⇒string
* [.deserialize(rawData)](#module_model.deserialize) ⇒ [document
](#document)
* [.compareThings(a, b, [_compareStrings])](#module_model.compareThings) ⇒ number
* [.modify(obj, updateQuery)](#module_model.modify) ⇒ [document
](#document)
* [.getDotValue(obj, field)](#module_model.getDotValue) ⇒ \*
* [.areThingsEqual(a, a)](#module_model.areThingsEqual) ⇒ boolean
* [.match(obj, query)](#module_model.match) ⇒ boolean
* _inner_
* [~modifierFunctions](#module_model..modifierFunctions) : enum
* [~comparisonFunctions](#module_model..comparisonFunctions) : enum
* [~logicalOperators](#module_model..logicalOperators)
* [~modifierFunction](#module_model..modifierFunction) : function
* [~comparisonOperator](#module_model..comparisonOperator) ⇒ boolean
* [~whereCallback](#module_model..whereCallback) ⇒ boolean
### model.checkObject(obj)
Check a DB object and throw an error if it's not valid Works by applying the above checkKey function to all fields recursively
**Kind**: static method of [model
](#module_model)
**Params**
- obj [document
](#document) | [Array.<document>
](#document)
### model.serialize(obj) ⇒ string
Serialize an object to be persisted to a one-line string For serialization/deserialization, we use the native JSON parser and not eval or Function That gives us less freedom but data entered in the database may come from users so eval and the like are not safe Accepted primitive types: Number, String, Boolean, Date, null Accepted secondary types: Objects, Arrays
**Kind**: static method of [model
](#module_model)
**Params**
- obj [document
](#document)
### model.deserialize(rawData) ⇒ [document
](#document)
From a one-line representation of an object generate by the serialize function Return the object itself
**Kind**: static method of [model
](#module_model)
**Params**
- rawData string
### model.compareThings(a, b, [_compareStrings]) ⇒ number
Compare { things U undefined } Things are defined as any native types (string, number, boolean, null, date) and objects We need to compare with undefined as it will be used in indexes In the case of objects and arrays, we deep-compare If two objects dont have the same type, the (arbitrary) type hierarchy is: undefined, null, number, strings, boolean, dates, arrays, objects Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!)
**Kind**: static method of [model
](#module_model)
**Params**
- a \*
- b \*
- [_compareStrings] [compareStrings
](#compareStrings) - String comparing function, returning -1, 0 or 1, overriding default string comparison (useful for languages with accented letters)
### model.modify(obj, updateQuery) ⇒ [document
](#document)
Modify a DB object according to an update query
**Kind**: static method of [model
](#module_model)
**Params**
- obj [document
](#document)
- updateQuery [query
](#query)
### model.getDotValue(obj, field) ⇒ \*
Get a value from object with dot notation
**Kind**: static method of [model
](#module_model)
**Params**
- obj object
- field string
### model.areThingsEqual(a, a) ⇒ boolean
Check whether 'things' are equal Things are defined as any native types (string, number, boolean, null, date) and objects In the case of object, we check deep equality Returns true if they are, false otherwise
**Kind**: static method of [model
](#module_model)
**Params**
- a \*
- a \*
### model.match(obj, query) ⇒ boolean
Tell if a given document matches a query
**Kind**: static method of [model
](#module_model)
**Params**
- obj [document
](#document) - Document to check
- query [query
](#query)
### model~modifierFunctions : enum
**Kind**: inner enum of [model
](#module_model)
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| $set | modifierFunction
|
| Set a field to a new value
| | $unset |modifierFunction
|
| Unset a field
| | $min |modifierFunction
|
| Updates the value of the field, only if specified field is smaller than the current value of the field
| | $max |modifierFunction
|
| Updates the value of the field, only if specified field is greater than the current value of the field
| | $inc |modifierFunction
|
| Increment a numeric field's value
| | $pull |modifierFunction
|
| Removes all instances of a value from an existing array
| | $pop |modifierFunction
|
| Remove the first or last element of an array
| | $addToSet |modifierFunction
|
| Add an element to an array field only if it is not already in it No modification if the element is already in the array Note that it doesn't check whether the original array contains duplicates
| | $push |modifierFunction
|
| Push an element to the end of an array field Optional modifier $each instead of value to push several values Optional modifier $slice to slice the resulting array, see https://docs.mongodb.org/manual/reference/operator/update/slice/ Difference with MongoDB: if $slice is specified and not $each, we act as if value is an empty array
| ### model~comparisonFunctions :enum
**Kind**: inner enum of [model
](#module_model)
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| $lt | comparisonOperator
|
| Lower than
| | $lte |comparisonOperator
|
| Lower than or equals
| | $gt |comparisonOperator
|
| Greater than
| | $gte |comparisonOperator
|
| Greater than or equals
| | $ne |comparisonOperator
|
| Does not equal
| | $in |comparisonOperator
|
| Is in Array
| | $nin |comparisonOperator
|
| Is not in Array
| | $regex |comparisonOperator
|
| Matches Regexp
| | $exists |comparisonOperator
|
| Returns true if field exists
| | $size |comparisonOperator
|
| Specific to Arrays, returns true if a length equals b
| | $elemMatch |comparisonOperator
|
| Specific to Arrays, returns true if some elements of a match the query b
| ### model~logicalOperators **Kind**: inner enum of [model
](#module_model)
**Properties**
| Name | Default | Description |
| --- | --- | --- |
| $or |
| Match any of the subqueries
| | $and |
| Match all of the subqueries
| | $not |
| Inverted match of the query
| | $where |
| Use a function to match
| ### model~modifierFunction :function
**Kind**: inner typedef of [model
](#module_model)
**Params**
- obj Object
- The model to modify
- fieldString
- Can contain dots, in that case that means we will set a subfield recursively
- value [document
](#document)
### model~comparisonOperator ⇒ boolean
**Kind**: inner typedef of [model
](#module_model)
**Params**
- a \*
- Value in the object
- b\*
- Value in the query
### model~whereCallback ⇒boolean
**Kind**: inner typedef of [model
](#module_model)
**Params**
- obj [document
](#document)