Noomman

Nodejs Object-Oriented MongoDB Manager


Class Instance

Contents

Methods

Methods

constructor(classModel, document)
Description

Creates an instance of Instance for a given ClassModel.

Parameters
Name Type Description
classModel ClassModel A ClassModel that the created Instance will be an instance of.
document Object A mongo document retrieved from a database. This parameter should only be used by internal noomman methods.
Returns

Instance - The Instance created with the given classModel and optional document.

Throws
Error Type Cause
NoommanConstructorError If no ClassModel is provided.
NoommanConstructorError If classModel is not an instance of ClassModel.
NoommanConstructorError If classModel is abstract.
NoommanConstructorError If given document does not have an _id property.
NoommanConstructorError If given document is for an auditable ClassModel but does not have a revision property.
assign(object)
Description

Will assign the values of any properties on the given object which are attributes or relationships defined on the ClassModel of this instance to this Instance. Any other properties on the given object are ignored.

Parameters
Name Type Description
object Object An object containing properties whose values should be assigned to this Instance.
delete(deletePrivilegeMethodParameter)
Description

Deletes this Instance from the database.

Parameters
Name Type Description
deletePrivilegeMethodParameter Object An object containing any parameters needed by a deletePrivilege method.
Returns

Promise<Boolean> - True if Instance is deleted properly.

Throws
Error Type Cause
NoommanDeleteError If this Instance has not yet been saved (i.e. is not in the database).
NoommanDeleteError If deletePrivilege method returns false for this Instance.
deleted()
Description

Use to determine if this instance has been deleted from the database.

Returns

Boolean - True if instance has been deleted using a delete method. False otherwise.

equals(that)
Description

Used to compare two instances. Checks that this Instance and the given Instance have (exactly) the same ClassModel, id, and values for each attribute and relationship.

Parameters
Name Type Description
that Instance An Instance to compare this Instance to.
Returns

Boolean - True if the ClassModel, id, and all attributes and relationships are the same for both Instances, false otherwise.

Throws
Error Type Cause
NoommanArgumentError If 'that' parameter is not an instance of Instance.
isInstanceOf(classModel)
Description

Determines if this Instance is an Instance of the given ClassModel, or any of its sub-ClassModels.

Parameters
Name Type Description
classModel ClassModel A ClassModel to check if this is an Instance of.
Returns

Boolean - True if this Instance is an instance of the given ClassModel, or any of its sub-ClassModels.

propertyIsSet(propertyName)
Description

Defines what it means for a property to be set. Valid values that count as 'set' are as follows: boolean: True or False number: Any value including 0. string: Any thing of type string. Array: Any array with a length greater than 0. Object/Relationship: Any Value

Parameters
Name Type Description
propertyName String The name of an attribute or relationship to check.
Returns

Boolean - True if the value of the attribute or relationship is considered as set.

readPrivilegeFilter(readPrivilegeMethodParameter)
Description

Runs applicable readPrivilege methods for this Instance. If each readPrivilege method returns true for this Instance, then this Instance is returned, otherwise null is returned.

Parameters
Name Type Description
readPrivilegeMethodParameter Object An object containing any parameters that the readPrivilege method(s) may need.
Returns

Promise<Instance> - This Instance if all readPrivilege methods return true, otherwise null.

save(createPrivilegeMethodParameter, updatePrivilegeMethodParameter)
Description

Saves the current state of this Instance to the database in the proper collection according to its ClassModel.

Returns

Promise<Instance> - This Instance, if save is successful.

Throws
Error Type Cause
NoommanSaveError If this Instance has already been deleted.
NoommanSaveError If this Instance has been stripped by stripSensitiveAttributes().
NoommanSaveError If Instance does not pass createPrivilege or updatePrivilege methods.
NoommanValidationError If more than one property in a mutex are set.
NoommanValidationError If a required property is not set.
NoommanValidationError If none of the properties in a required group are set.
Error If a custom validation method from the ClassModel fails.
saved()
Description

Use to determine if this instance currently exists in the database.

Returns

Boolean - True if instance resulted from a database query or if this instance has had a save method complete successfully. False otherwise.

saveWithoutValidation()
Description

Saves the current state of this Instance to the database in the proper collection according to its ClassModel. Does not do any validation or run crudPrivilege functions, recommended not to be used outside internal noomman methods. The purpose is for use in saving multiple instances at once, and any calling method is expected to have already run validations and crudPrivilege functions.

Returns

Promise<Instance> - This Instance, if save is successful.

Throws
Error Type Cause
NoommanSaveError If this Instance has already been deleted.
NoommanSaveError If this Instance has been stripped by stripSensitiveAttributes().
stripped()
Description

Used to determine if this instance has been stripped of sensitive attributes.

Returns

Boolean - True if this instance has been stripped of sensitive attributes by calling stripSensitiveAttributes().

stripSensitiveAttributes()
Description

Will remove any attributes with property sensitive equal to true from this Instance.

validate()
Description

Throws an error if this instance is invalid for any reason, including violations of custom validations, required properties, required groups, or mutexes.

Throws
Error Type Cause
NoommanValidationError If more than one property in a mutex are set.
NoommanValidationError If a required property is not set.
NoommanValidationError If none of the properties in a required group are set.
Error If a custom validation method from the ClassModel fails.
walk(relationshipName, usePreviousState)
Description

Walks a relationship from this Instance, returning the related Instance or Instances.

Parameters
Name Type Description
relationshipName String The name of the relationship to walk.
usePreviousState Boolean If true, will walk the previous value of the given relationship for this Instance. For noomman internal use only.
Returns

Promise<Instance | InstanceSet> - The Instance (if relationship is singular) or InstanceSet (if the relationship is non-singular) related to this Instance through the given relationship. If relationship is empty, then null will be returned for singular relationships or an empty InstanceSet will be returned for non-singular relationships.

Throws
Error Type Cause
NoommanArgumentError If no relationshipName is given.
NoommanArgumentError If relationshipName is not a String.
NoommanArgumentError If relationshipName does not match any relationship for the ClassModel of this Instance.
walkPath(path)
Description

Walks a path of multiple relationships from this Instance, returning an InstanceSet containing all Instances at the end of the path.

Parameters
Name Type Description
path Array<String> An Array containing the names of the relationships to walk.
Returns

Promise<InstanceSet> - The InstanceSet of Instances related to this Instance through the given relationships. If there are no Instances at the end of the path, or path cannot be walked all the way to the end, the returned InstanceSet will be empty.

Throws
Error Type Cause
NoommanArgumentError If path is not an Array.
NoommanArgumentError If path contains any items that are not a string.
NoommanArgumentError If path contains any items that are not a valid relationship for the relavant ClassModel.