9.1 KiB
model
Handle models (i.e. docs) Serialization/deserialization Copying Querying, update
- model
- static
- .checkObject(obj)
- .serialize(obj) ⇒
string
- .deserialize(rawData) ⇒
document
- .compareThings(a, b, [_compareStrings]) ⇒
number
- .modify(obj, updateQuery) ⇒
document
- .getDotValue(obj, field) ⇒
*
- .areThingsEqual(a, a) ⇒
boolean
- .match(obj, query) ⇒
boolean
- inner
- ~modifierFunctions :
enum
- ~comparisonFunctions :
enum
- ~logicalOperators
- ~modifierFunction :
function
- ~comparisonOperator ⇒
boolean
- ~whereCallback ⇒
boolean
- ~modifierFunctions :
- static
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
Params
- obj
document
|Array.<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
Params
- obj
document
model.deserialize(rawData) ⇒ document
From a one-line representation of an object generate by the serialize function Return the object itself
Kind: static method of 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
Params
- a
*
- b
*
- [_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
Modify a DB object according to an update query
Kind: static method of model
Params
model.getDotValue(obj, field) ⇒ *
Get a value from object with dot notation
Kind: static method of 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
Params
- a
*
- a
*
model.match(obj, query) ⇒ boolean
Tell if a given document matches a query
Kind: static method of model
Params
model~modifierFunctions : enum
Kind: inner enum of 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
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
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
Params
- obj
Object
-The model to modify
- field
String
-Can contain dots, in that case that means we will set a subfield recursively
- value
document
model~comparisonOperator ⇒ boolean
Kind: inner typedef of model
Params
- a
*
-Value in the object
- b
*
-Value in the query
model~whereCallback ⇒ boolean
Kind: inner typedef of model
Params
- obj
document