Noomman

Nodejs Object-Oriented MongoDB Manager


Class ClassModel

Contents

Methods Objects

Methods

constructor(schema)
Description

Parameters
Name Type Description
schema Object<schema> A schema describing the properties of this ClassModel.
Returns

ClassModel - The ClassModel created according to the given schema.

Throws
Error Type Cause
NoommanConstructorError If property className is omitted.
NoommanConstructorError If property attributes is provided and is not an Array.
NoommanConstructorError If property relationships is provided and is not an Array.
NoommanConstructorError If property superClasses is provided and is not an Array.
NoommanConstructorError If property superClasses is provided and is an empty Array.
NoommanConstructorError If property useSuperClassCollection is true and superClasses is omitted or contains more than one ClassModel.
NoommanConstructorError If property auditable is provided and is not a Boolean.
NoommanConstructorError If property indices is provided and is not an Array.
NoommanConstructorError If property validations is provided and is not an Array.
NoommanConstructorError If property createPrivilege of property crudPrivileges is not a Function.
NoommanConstructorError If property readPrivilege of property crudPrivileges is not a Function.
NoommanConstructorError If property updatePrivilege of property crudPrivileges is not a Function.
NoommanConstructorError If property deletePrivilege of property crudPrivileges is not a Function.
NoommanConstructorError If property sensitivePrivilege of property crudPrivileges is not a Function.
NoommanConstructorError If ClassModel has a sensitive attribute but no sensitivePrivilege method.
NoommanConstructorError If ClassModel has a sensitivePrivilege method but no sensitive attribute.
NoommanConstructorError If property staticMethods is provided but is not an Object.
NoommanConstructorError If property staticMethods is provided, and any of its properties is not a Function.
NoommanConstructorError If property staticMethods is provided, and any of its properties has the same name as a built in noomman method on class ClassModel.
NoommanConstructorError If property nonStaticMethods is provided but is not an Object.
NoommanConstructorError If property nonStaticMethods is provided, and any of its properties is not a Function.
NoommanConstructorError If property nonStaticMethods is provided, and any of its properties has the same name as a built in noomman method on class Instance.
NoommanConstructorError If properties useSuperClassCollection and abstract are both true.
NoommanConstructorError If property superClasses is provided, and any super ClassModel has an attribute with the same name as a attribute defined in the attributes property of the schema.
NoommanConstructorError If property superClasses is provided, and any super ClassModel has an relationship with the same name as a relationship defined in the relationships property of the schema.
NoommanConstructorError If any ClassModel in the superClasses property has useSuperClassModel set to true.
NoommanConstructorError If property auditable is false, but a ClassModel in the superClasses property has auditable set to true.
NoommanConstructorError If properties abstract and useSuperClassCollection are both true.
allSuperClasses()
Description

Finds all the ClassModels which are a parent ClassModel to this ClassModel, all the way up the inheritance tree.

Returns

Array<ClassModel> - An array containing all the ClassModels that are a super ClassModel to this ClassModel.

clear()
Description

Deletes every document in the collection for this ClassModel. This is for testing purposes only, never run in production.

Returns

Promise<undefined> - A promise which resolves to undefined if no Errors are thrown.

finalize()
Description

For each defined ClassModel, runs post-constructor validations and applies indices. Run only after ALL class models have been created.

Returns

Promise<undefined> - A Promise which resolves to undefined if successful.

Throws
Error Type Cause
NoommanClassModelError If no ClassModel exists with the value of a relationship's toClass property.
NoommanClassModelError If a two-way relationship is defined and the related ClassModel does not have a relationship a name matching the mirrorRelationship property of the relationship.
NoommanClassModelError If a two-way relationship is defined but the mirrorRelationship properties of the two relationships are not the name of the other relationship.
find(queryFilter, readPrivilegeMethodParameter, sensitivePrivilegeMethodParameter)
Description

Finds Instances of this ClassModel using the given query filter in the database. If called on a super-ClassModel, will recursively check this ClassModel's collection, and then it's sub-ClassModels' collections. This method respects readPrivilege and sensitivePrivilege methods. If this ClassModel has read privileges set, Instances found during query will be filtered down to those which pass the readPrivilege method(s) for this ClassModel. If this ClassModel has sensitive privileges set, all Instances which do not pass the sensitivePrivilege method(s) for this ClassModel will be stripped of any sensitive attributes.

Parameters
Name Type Description
queryFilter Object A mongo query object (required).
readPrivilegeMethodParameter Object An object containing parameters that will be passed to the readPrivilege method(s) for this ClassModel.
sensitivePrivilegeMethodParameter Object An object containing parameters that will be passed to the sensitivePrivilege method(s) for this ClassModel.
Returns

Promise<InstanceSet> - An InstanceSet of this ClassModel containing all instances of this ClassModel or its children which match the given query and pass the readPrivilege methods if applicable.

findById(id, readPrivilegeMethodParameter, sensitivePrivilegeMethodParameter)
Description

Finds a single instance of this ClassModel with the given id. If called on a superclass, will recursively check this ClassModel's collection, and then it's sub-ClassModels collections. This method respects readPrivilege and sensitivePrivilege methods. If this ClassModel has read privileges set, the Instance found during query will not be returned if it does not pass the readPrivilege method(s) for this ClassModel. If this ClassModel has sensitive privileges set, an instance which does not pass the sensitivePrivilege method(s) for this ClassModel will be stripped of any sensitive attributes.

Parameters
Name Type Description
id ObjectId | String A mongo ObjectId (or hex string representation thereof) of the Instance you which to find.
readPrivilegeMethodParameter Object An object containing parameters that will be passed to the readPrivilege method(s) for this ClassModel.
sensitivePrivilegeMethodParameter Object An object containing parameters that will be passed to the sensitivePrivilege method(s) for this ClassModel.
Returns

Promise<Instance> - The first Instance of this ClassModel or its children which has the given id and passes the readPrivilege methods if applicable. Returns null if no Instance with the given id is found, or if matching instance does not pass readPrivilege method (if applicable).

Throws
Error Type Cause
NoommanArgumentError If the given id is a string and is not a valid hex string for an ObjectId.
findOne(queryFilter, readPrivilegeMethodParameter, sensitivePrivilegeMethodParameter)
Description

Finds a single Instance of this ClassModel using the given query filter in the database. If called on a super-ClassModel, will recursively check this ClassModel's collection, and then it's sub-ClassModels collections. This method respects readPrivilege and sensitivePrivilege methods. If this ClassModel has read privileges set, the Instance found during query will not be returned if it does not pass the readPrivilege method(s) for this ClassModel. If this ClassModel has sensitive privileges set, an Instance which does not pass the sensitivePrivilege method(s) for this ClassModel will be stripped of any sensitive attributes.

Parameters
Name Type Description
queryFilter Object A mongo query object (required).
readPrivilegeMethodParameter Object An object containing parameters that will be passed to the readPrivilege method(s) for this ClassModel.
sensitivePrivilegeMethodParameter Object An object containing parameters that will be passed to the sensitivePrivilegeMethodParameter method(s) for this ClassModel.
Returns

Promise<Instance> - The first Instance of this ClassModel or its children which matches the given query and passes the readPrivilege methods if applicable. Returns null if no Instance matches query or if matching Instance does not pass readPrivilege method if applicable.

isSuperClass()
Description

Use to determine if this ClassModel is a super ClassModel.

Returns

Boolean - True if this ClassModel has any sub ClassModels, false otherwise.

pureFind(queryFilter)
Description

Finds instances of this ClassModel using the given query filter in the database. If called on a super-ClassModel, will recursively check this ClassModel's collection, and then it's sub-ClassModels collections. This method DOES NOT do any readPrivilege or sensitivePrivilege filtering.

Parameters
Name Type Description
queryFilter Object A mongo query object (required).
Returns

Promise<InstanceSet> - An InstanceSet of this ClassModel containing all instances of this ClassModel or its children which match the given query.

Throws
Error Type Cause
NoommanClassModelError If this ClassModel is abstract and has no sub-ClassModels.
pureFindById(id)
Description

Finds a single instance of this ClassModel with the given id. If called on a superclass, will recursively check this ClassModel's collection, and then it's sub-ClassModels collections. This method does not respect readPrivilege and sensitivePrivilege methods.

Parameters
Name Type Description
id ObjectId A mongo ObjectId of the Instance you which to find.
Returns

Promise<Instance> - The Instance of this ClassModel or its children with the given id.

pureFindOne(queryFilter)
Description

Finds a single instance of this ClassModel using the given query filter in the database. If called on a superclass, will recursively check this ClassModel's collection, and then it's sub-ClassModels collections. This method does not respect readPrivilege and sensitivePrivilege methods.

Parameters
Name Type Description
queryFilter Object A mongo query object (required).
Returns

Promise<Instance> - The first Instance of this ClassModel or its children which matches the given query.

Throws
Error Type Cause
NoommanClassModelError If this ClassModel is abstract and has no sub-ClassModels.

Object Definitions

schema

Schema object passed to ClassModel.constructor(). This defines the name, attributes, relationships, and other properties of a ClassModel.

Shape
                
{
    className: String (required),
    superClasses: [ ClassModel ],
    useSuperClassCollection: Boolean,
    abstract: Boolean,
    auditable: Boolean,
    attributes: [
        {
            name: String (required), 
            type: String (required), 
            list: Boolean,
            required: Boolean,
            unique: Boolean,
            sensitive: Boolean,
            mutex: String,
            requiredGroup: String,
        },
    ],
    relationships: [
        {
            name: String (required),
            toClass: String (required),
            singular: Boolean (required),
            required: Boolean,
            owns: Boolean,
            mirrorRelationship: String,
            mutex: String,
            requiredGroup: String,
        }
    ],
    privileges: {
        create: Function,
        read: Function,
        update: Function,
        delete: Function,
        sensitive: Function,
    },
    validations: [ Function ],
    indices: [ fieldOrSpec ], 
    staticMethods: {
        String: Function,
    },
    nonStaticMethods: {
        String: Function,
    },
}