Class: GenericMongoDBObject

GenericMongoDBObject

Common logic for Mongo/Mongoose objects, abstraction layer to allow a simple switch from one DB to another.


<abstract> new GenericMongoDBObject(linkedObjects: [, object])

Class variables are :

 - {mongoose.Schema} $document
 - {mongoose.Model} $model: used for db access functions
 - {String} $name: flag the property which should be used to find documents by name (as in findByName() method)
Parameters:
Name Type Argument Description
linkedObjects: Array

objects to be linked to this one i.e. removed when this object is removed

object Object <optional>

to init this GenericMongoDBObject with

Source:

Extends

Methods


<protected> $convertToObjectID()

Source:

<protected> $dbPopulateCheckReference(obj, path)

Check 'obj' to see if any of its paths need to be reset and return an array of path to set to null

Parameters:
Name Type Description
obj Object
path String
Source:
Returns:
Type
Array.<String>

<protected> $getDocumentReferenced(path)

Tests a path to see if it references another Document.
If so returned the model name(s) of the referenced Document(s); otherwise return null

Parameters:
Name Type Description
path String
Source:
Returns:
Type
Object | String | null

<protected> $getMongoCollection()

Get the Mongo collection associated with this DBObject

Source:
Returns:
Type
mongo.Collection

<protected> $updateOCC(query, update [, options] [, returnedObj])

Perform an Optimistic Consistency Controled update operation

Parameters:
Name Type Argument Description
query Object
update Object
options Object <optional>

(see http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOneAndUpdate for complete options list)

Properties
Name Type Argument Default Description
returnOriginal boolean <optional>
true

When false, returns the updated document rather than the original. The default is true.

upsert boolean <optional>
false

Upsert the document if it does not exist.

projection Object <optional>
null

Limits the fields to return for all matching documents.

sort Object <optional>
null

Determines which document the operation modifies if the query selects multiple documents.

returnedObj any <optional>

object to wrap in the Promise if fulfilled; if null return update's value (i.e. updated document or original document depending on 'options.returnOriginal')

Source:
Returns:

wrapping 'returnedObj' / update's result

Type
Promise

<protected> _convert(document [, options], $recursiveCall:)

Convert an Object into a proper DBObject

Parameters:
Name Type Argument Description
document any
options Object <optional>
$recursiveCall: Boolean

don't use

Inherited From:
Source:
Returns:
Type
DBObject

addToSet(property:, value, options)

Adds a value to an array unless the value is already present, in which case it does nothing to that array. The underlying DBObject is saved

Parameters:
Name Type Description
property: String

name of the array property

value any
options Object
Properties
Name Type Argument Default Description
upsert boolean <optional>
true

set to false to only add 'value' to set at path 'property' if it exists (and is an Array)

each boolean <optional>
true

set to true to add all elements of 'value' separately. If set to false and 'value' is an array adds the Array

occ boolean <optional>
true

set to false to disable concurrency check (using Optimistic Concurrency Control) => addToSet will return a rejected Promise

Inherited From:
Source:
Returns:

wrapping the value which were added

Type
Promise

dbAddToSet(property:, value, options)

Adds a value to an array unless the value is already present, in which case it does nothing to that array. The underlying DBObject is saved

Parameters:
Name Type Description
property: String

name of the array property

value any
options Object
Properties
Name Type Argument Default Description
each boolean <optional>
true

set to true to add all elements of 'value' separately. If set to false and 'value' is an array adds the Array

slice boolean <optional>

same as $slice operator in Mongo see https://docs.mongodb.com/manual/reference/operator/update/slice/

Overrides:
Source:
Returns:

wrapping the value which were added

Type
Promise

<protected> dbFind(query [, options])

Encapsulate 'this.$model.find(query)'

Parameters:
Name Type Argument Description
query string

: id to lookup

options Object <optional>
Overrides:
Source:
Returns:
Type
Promise

<protected> dbFindById(id [, options])

Encapsulate 'this.$model.findById(id)'

Parameters:
Name Type Argument Description
id string

: id to lookup

options Object <optional>
Overrides:
Source:
Returns:
Type
Promise

<protected> dbFindByIdAndRemove(id [, options])

Encapsulate 'this.$model.findByIdAndRemove(id)'

Parameters:
Name Type Argument Description
id Object | String | Number

: id to lookup, if object structure as a Query

options Object <optional>
Overrides:
Source:
Returns:
Type
Promise

<protected> dbFindOneAndUpdate(query [, options])

Encapsulate '$model.findByIdAndUpdate(id, query, options)'

Parameters:
Name Type Argument Description
id.id string

: id to lookup

query string
options object <optional>
Overrides:
Source:
Returns:
Type
Promise

<protected> dbPopulate( [path])

Replaces all document references by the actual DBObject

Parameters:
Name Type Argument Description
path Array.<String> | String | Object | Array.<Object> <optional>

path or array of path to populate; if nully populate all available references; if Object or [Object] uses Mongoose deep populate (see http://mongoosejs.com/docs/populate.html#deep-populate)

Overrides:
Source:
Returns:

wrapping the populated DBObject

Type
Promise

dbPush(property:, value, options)

Appends a specified value to an array. The underlying DBObject is saved

Parameters:
Name Type Description
property: String

name of the array property

value any
options Object
Properties
Name Type Argument Default Description
each boolean <optional>
true

set to true to add all elements of 'value' separately. If set to false and 'value' is an array adds the Array

Source:
Returns:

wrapping the value which were added

Type
Promise

<protected> dbRemove( [query] [, options])

Encapsulate 'this.$model.remove(query)'

Parameters:
Name Type Argument Description
query Object <optional>

if empty remove the current object (this._id must then be set)

options Object <optional>
Overrides:
Source:
Returns:
Type
Promise

<protected> dbSave(options)

Async save of 'this'

Parameters:
Name Type Description
options Object
Properties
Name Type Argument Default Description
occ boolean <optional>
false

set to true to force a concurrency check using Optimistic Concurrency Control

Overrides:
Source:
Returns:

wrapping the updated object

Type
Promise

fill(object, ignoreKeys)

Copy 'object' into the current 'DBObject', excluding 'ignoreKeys'

Parameters:
Name Type Description
object Object

: object to clone

ignoreKeys Array.<String>

: array of String containing the keys to ignore

Inherited From:
Source:

find(query, options)

Method to find an Object using a query

Parameters:
Name Type Description
query Object
options Object

supports lean, populateFields, limit, sort

Inherited From:
Source:
Returns:

wrapping an array of Objects

Type
Promise

findById(id:, options)

Method to find a Object based on its ID

Parameters:
Name Type Description
id: String

id of the object to find

options Object

supports lean, populateFields, limit, sort

Inherited From:
Source:
Returns:

wrapping an Object (or null)

Type
Promise

findByName(name, options)

Method to find an Object based on its name

Parameters:
Name Type Description
name String
options Object

supports lean, populateFields, limit, sort

Inherited From:
Source:
Returns:

wrapping an array of Objects

Type
Promise

<protected> getDocument(object)

Generic method to create an instance of Mongoose Document (new MongooseModel()) from
this.$model. It expects 'this.$model' to have been set in the subclass's construtor

Parameters:
Name Type Description
object any
Source:
Returns:
Type
mongoose.Document

getName()

Returns the name associated with this object.

Inherited From:
Source:
Returns:
Type
String

isEmpty()

Check if the associated DBObject is empty

Inherited From:
Source:
Returns:
Type
boolean

lean()

Create a lean (and cloned) version of this object i.e. keeping the data but without the DBObject magic
Goes recursively into nested Objects

Inherited From:
Source:
Returns:
Type
Object

populate( [path])

Replaces all document references by the actual DBObject

Parameters:
Name Type Argument Description
path Array | String <optional>

path or array of path to populate, is nully populate all available references

Inherited From:
Source:
Returns:

wrapping the populated object

Type
Promise

push(property:, value, options)

Appends a specified value to an array. The underlying DBObject is saved

Parameters:
Name Type Description
property: String

name of the array property

value any
options Object
Properties
Name Type Argument Default Description
each boolean <optional>
true

set to true to add all elements of 'value' separately. If set to false and 'value' is an array adds the Array

occ boolean <optional>
true

set to false to disable concurrency check (using Optimistic Concurrency Control) => push will return a rejected Promise

upsert boolean <optional>
true

set to false to only add 'value' to set at path 'property' if it exists (and is an Array)

Inherited From:
Source:
Returns:

wrapping the updated object

Type
Promise

remove( [options])

Method to remove an object. This._id must be set (at least)

Since 1.3 Added an option to remove() to temporarily add nested objects to linkedObjects list

Since 1.2 remove() method is now called recursively on nested DBObject / arrays of DBObject (instead of dbRemove())

Parameters:
Name Type Argument Description
options Object <optional>
Properties
Name Type Argument Default Description
delete Object <optional>
null

if null, execute a hard delete (i.e. actually remove DBObject from the DB)

Properties
Name Type Argument Description
method Object <optional>

'hard' or 'soft'

user Object <optional>

used only if options.delete.method == 'soft'

unlink Array.<String> <optional>

additional objects to be removed (temporary add the content of the list to the object's linkedObjects' property; method == 'hard' only)

Inherited From:
Source:
Returns:

wrapping a fully populated removed object

Type
Promise

save( [options])

Method to save an Object into DB.

Since v1.2 save() don't automatically populate saved Object. Use options.populate = true if needed

Parameters:
Name Type Argument Description
options Object <optional>
Properties
Name Type Argument Default Description
populate boolean | String | Array.<String> <optional>
false

set to populate saved object (true => populate all fields, String/String[] will be passed as argument to populate())

occ boolean <optional>
false

set to true to force a concurrency check using Optimistic Concurrency Control

Inherited From:
Source:
Returns:

wrapping the saved Object

Type
Promise