Model

flitter-orm/src/model/Model~ Model

The base model class. All model implementations should extend from this.


Constructor

new Model(dataopt, embedded_parentopt)

Create a new instance of this model.

Parameters:
Name Type Attributes Default Description
data object <optional>
{}

data to preload the model with

embedded_parent module:flitter-orm/src/model/Model~Model | boolean <optional>
false

if specified, sets the embedded parent of this model for saving

Extends

Members

(private, static) __name

The collection name for this model. If specified, this will be the value of the collection static member. If not, it will be the name of the class.

(private, static) __schema

The instantiated schema for this model.

(static) schema

User defined schema for the model. This should be implemented by subclasses.

(static) services

The services required by this model. Note that the 'scaffold' service must be provided.

(private) __embedded :boolean

True if the model is embedded within a parent.

Type:
  • boolean

(private) __embedded_parent :module:flitter-orm/src/model/Model~Model|boolean

If the model is embedded within a parent, this is the reference to that parent. Otherwise, false.

(private) __relation_cache :module:flitter-orm/src/model/ResultCache~ResultCache

Cache for results of relationship lookups for the instance.

(private) __schema_instance :module:flitter-orm/src/schema/Schema~Schema|boolean

The holding variable for the instantiated schema.

(private) _di_allow_defer :boolean

If true, the injector will defer the class if the class requests any services that the container is missing. These services are filled in later and added to the prototype and any instances. True by default.

Type:
  • boolean

(private) _di_deferred_instances :Array.<module:flitter-di/src/Injectable~Injectable>

Collection of instances of this class that need to have the deferred service instances injected into them when the deferred services are finally provided.

(private) _di_deferred_services :Array.<string>

List of services that were deferred and not provided at the time of injection.

Type:
  • Array.<string>

collection :string|boolean

Optionally, the name of the collection where this model should have its records stored. If none is provided, the collection will be the name of the class.

Type:
  • string | boolean

id

If defined, will return the string-form ID of this model.

scopes :Array.<module:flitter-orm/src/model/Scope~Scope>

Array of instantiated references to Scopes that should be applied to this model.

Methods

(private, static) __collection() → {Collection}

Get the MongoDB collection instance for this model.

Returns:
Type
Collection

(async, static) count(filteropt, optsopt) → {Promise.<number>}

Count the number of instances of this model with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<number>

(async, static) cursor(filteropt, optsopt) → {mongodb/Cursor}

Get a lookup cursor for this model's collection with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
mongodb/Cursor

(async, static) deleteMany(filteropt, optsopt) → {Promise.<undefined>}

Delete all instances of this model with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<undefined>

(async, static) deleteOne(filteropt, optsopt) → {Promise.<undefined>}

Delete a single instance of this model with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<undefined>

(async, static) filter() → {module:flitter-orm/src/filter/Filter~Filter}

Create a new programmatic filter for this class, pre-loaded with any scopes.

(async, static) find(filteropt, optsopt) → {Promise.<Array.<module:flitter-orm/src/model/Model~Model>>}

Lookup an array of instances of this model with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<Array.<module:flitter-orm/src/model/Model~Model>>

(async, static) findById(id, optsopt) → {Promise.<Array.<module:flitter-orm/src/model/Model~Model>>}

Lookup a single instance of this model by the provided ID.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
id string | ObjectId

the ID of the model to query

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<Array.<module:flitter-orm/src/model/Model~Model>>

(async, static) findOne(filteropt, optsopt) → {Promise.<Array.<module:flitter-orm/src/model/Model~Model>>}

Lookup a single instance of this model with the specified filters.

See: http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#find for information on optional settings.

Parameters:
Name Type Attributes Default Description
filter object <optional>
{}

the query filters for the cursor

opts object <optional>
{}

optional settings

Returns:
Type
Promise.<Array.<module:flitter-orm/src/model/Model~Model>>

(async, static) from_cursor(cursor) → {Promise.<module:flitter-orm/src/model/Model~Model>}

Returns an array of model instances from the specified cursor.

Parameters:
Name Type Description
cursor mongodb/cursor

the cursor

(static) limit(to) → {module:flitter-orm/src/proxy/model/LimitProxy~LimitProxy}

Limit the results to a specific number of records.

Parameters:
Name Type Description
to number

(static) sort() → {module:flitter-orm/src/proxy/model/SortProxy~SortProxy}

Sort the results by the specified key or keys.

Parameters:
Name Type Description
sorts... string

any number of sort specifications

Example
Model.sort('+last_name', '+first_name', '-create_date')

(static) to_object_id(string) → {mongodb/ObjectId}

Returns an instantiated ObjectId for the given string. This is preferred to importing mongodb manually and casting it, because it insures that flitter-orm's instanceof checks are still satisfied.

Parameters:
Name Type Description
string string

the string of the ID

Returns:
  • the equivalent Object ID
Type
mongodb/ObjectId

(private) __collection() → {Collection}

Get the MongoDB collection instance for this model.

Returns:
Type
Collection

(async, private) __scope_limit_save(db_object) → {Promise.<object>}

Allow all of the model's registerd scopes to modify the schema-cast database object before it is persisted.

Parameters:
Name Type Description
db_object object
Returns:
  • modified db_object
Type
Promise.<object>

(private) __set_values(data, current_objectopt, current_schemaopt)

Shallow copy the values from the specified object to this model.

Parameters:
Name Type Attributes Default Description
data object
current_object object <optional>
false

for recursion. The current object scope.

current_schema object <optional>
false

for recursion. The current schema level.

belongs_to_many(OtherModel, local_key, foreign_keyopt) → {Promise.<Array.<module:flitter-orm/src/model/Model~Model>>|Array.<module:flitter-orm/src/model/Model~Model>}

Associates many records of another model with this model based on a local/foreign key relationship.

Normally, this would be automatically returned by a named method on the sub-class that implements the relationship.

This returns a cached result. So, it will only return a promise on the first call. Subsequent calls will return the results from the cache and are, therefore, synchronous.

Parameters:
Name Type Attributes Default Description
OtherModel module:flitter-orm/src/model/Model~Model

static class of the other model

local_key string

local key of the field to match

foreign_key string <optional>
''

foreign key of the field to match (if none provided, assume the same as local_key)

Returns:
  • the matching model instances
Type
Promise.<Array.<module:flitter-orm/src/model/Model~Model>> | Array.<module:flitter-orm/src/model/Model~Model>

belongs_to_one(OtherModel, local_key, foreign_keyopt) → {Promise.<(module:flitter-orm/src/model/Model~Model|undefined)>|module:flitter-orm/src/model/Model~Model}

Associates a single record of another model with this model based on a local/foreign key relationship.

Normally, this would be automatically returned by a named method on the sub-class that implements the relationship.

This returns a cached result. So, it will only return a promise on the first call. Subsequent calls will return the results from the cache and are, therefore, synchronous.

Parameters:
Name Type Attributes Default Description
OtherModel module:flitter-orm/src/model/Model~Model

static class of the other model

local_key string

local key of the field to match

foreign_key string <optional>
''

foreign key of the field to match (if none provided, assume the same as local_key)

Returns:
  • the matching model instance
Type
Promise.<(module:flitter-orm/src/model/Model~Model|undefined)> | module:flitter-orm/src/model/Model~Model

(async) delete() → {Promise.<module:flitter-orm/src/model/Model~Model>}

Delete the current instance of this model from the database. This will remove the model's ID from this instance. Other properties will remain unchanged.

has_many(OtherModel, local_key, foreign_keyopt) → {Promise.<Array.<module:flitter-orm/src/model/Model~Model>>|Array.<module:flitter-orm/src/model/Model~Model>}

Associates many records of another model with this model based on a local/foreign key relationship.

Normally, this would be automatically returned by a named method on the sub-class that implements the relationship.

This returns a cached result. So, it will only return a promise on the first call. Subsequent calls will return the results from the cache and are, therefore, synchronous.

Parameters:
Name Type Attributes Default Description
OtherModel module:flitter-orm/src/model/Model~Model

static class of the other model

local_key string

local key of the field to match

foreign_key string <optional>
''

foreign key of the field to match (if none provided, assume the same as local_key)

Returns:
  • the matching model instances
Type
Promise.<Array.<module:flitter-orm/src/model/Model~Model>> | Array.<module:flitter-orm/src/model/Model~Model>

has_one(OtherModel, local_key, foreign_keyopt) → {Promise.<module:flitter-orm/src/model/Model~Model>|module:flitter-orm/src/model/Model~Model}

Associates a single record of another model with this model based on a local/foreign key relationship.

Normally, this would be automatically returned by a named method on the sub-class that implements the relationship.

This returns a cached result. So, it will only return a promise on the first call. Subsequent calls will return the results from the cache and are, therefore, synchronous.

Parameters:
Name Type Attributes Default Description
OtherModel module:flitter-orm/src/model/Model~Model

static class of the other model

local_key string

local key of the field to match

foreign_key string <optional>
''

foreign key of the field to match (if none provided, assume the same as local_key)

Returns:
  • the matching model instance
Type
Promise.<module:flitter-orm/src/model/Model~Model> | module:flitter-orm/src/model/Model~Model

(async) save() → {Promise.<module:flitter-orm/src/model/Model~Model>}

Persist this model instance to the database. This will store only values defined in the schema for this model, and, in so doing, will cast those values and fill in the specified defaults. These changes will be added to this instance after the save.

Returns:
  • the current instance with updated properties
Type
Promise.<module:flitter-orm/src/model/Model~Model>

(async) set(field, value) → {Promise.<module:flitter-orm/src/model/Model~Model>}

A convenience method. Set the specified field on this model equal to the specified property and immediately save the record.

Parameters:
Name Type Description
field string
value *