Class: DBObject

DBObject

Abstraction layer to encapsulate DB accesses and isolate our backend code from DB choice


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

Subclasses should set the following property in the constructor:

 - '$name' (string): it will be used to lookup the property which will be used as the "name" of the objects (e.g., in findByName())
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 DBObject with

Source:

Members


<static, constant> OCC_MAX_RETRIES

Source:

<static, constant> STATUS

Source:

Methods


<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

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

Source:
Returns:

wrapping the value which were added

Type
Promise

<protected> dbAddToSet()

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
Subclasses must override this method

Source:
Returns:
Type
Promise

<protected> dbFind(query [, options])

Find object matching 'query'
Subclasses must override this method

Parameters:
Name Type Argument Description
query object
options Object <optional>
Source:
Returns:
Type
Promise

<protected> dbFindById(id [, options])

Find object which id is "id"
Subclasses must override this method

Parameters:
Name Type Argument Description
id object
options Object <optional>
Source:
Returns:
Type
Promise

<protected> dbFindByIdAndRemove(id [, options])

Finds a matching document, removes it, returning the found document (if any)
Subclasses must override this method

Parameters:
Name Type Argument Description
id Object | Number | String
options Object <optional>
Source:
Returns:
Type
Promise

<protected> dbFindOneAndUpdate(query [, options])

Find object with id 'id' and update its values based on 'query'.
Subclasses must override this method

Parameters:
Name Type Argument Description
id.id string

: id to lookup

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

<protected> dbPopulate( [path])

Replaces all document references by the actual DBObject
Subclasses must override this method

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

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

Source:
Returns:
Type
Promise

<protected> dbRemove(query [, options])

Remove object matching 'query'
Subclasses must override this method

Parameters:
Name Type Argument Description
query Object
options Object <optional>
Source:
Returns:
Type
Promise

<protected> dbSave()

Save object
Subclasses must override this method

Source:
Returns:
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

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

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

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

Source:
Returns:

wrapping an array of Objects

Type
Promise

getName()

Returns the name associated with this object.

Source:
Returns:
Type
String

isEmpty()

Check if the associated DBObject is empty

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

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

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)

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)

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

Source:
Returns:

wrapping the saved Object

Type
Promise