Class: ModelBuilder

ModelBuilder()

new ModelBuilder()

ModelBuilder - A builder to define data models.
Properties:
Name Type Description
definitions Object Definitions of the models.
models Object Model constructors
Source:

Methods

buildModelFromInstance(name, json, options) → {ModelClass}

Introspect the JSON document to build a corresponding model.
Parameters:
Name Type Description
name String The model name
json Object The JSON object
options Object The options
Source:
Returns:
The generated model class constructor.
Type
ModelClass

buildModels(schemas) → {Object.<string, ModelClass>}

Build models from schema definitions `schemas` can be one of the following: 1. An array of named schema definition JSON objects 2. A schema definition JSON object 3. A list of property definitions (anonymous)
Parameters:
Name Type Description
schemas * The schemas
Source:
Returns:
A map of model constructors keyed by model name.
Type
Object.<string, ModelClass>

define(className, properties, settings, parent) → {ModelClass}

Define a model class. Simple example: ``` var User = modelBuilder.define('User', { email: String, password: String, birthDate: Date, activated: Boolean }); ``` More advanced example: ``` var User = modelBuilder.define('User', { email: { type: String, limit: 150, index: true }, password: { type: String, limit: 50 }, birthDate: Date, registrationDate: {type: Date, default: function () { return new Date }}, activated: { type: Boolean, default: false } }); ```
Parameters:
Name Type Description
className String Name of class
properties Object Hash of class properties in format `{property: Type, property2: Type2, ...}` or `{property: {type: Type}, property2: {type: Type2}, ...}`
settings Object Other configuration of class
parent function Parent model
Source:
Returns:
The class constructor.
Type
ModelClass

defineProperty(model, propertyName, propertyDefinition)

Define single property named `propertyName` on `model`
Parameters:
Name Type Description
model String Name of model
propertyName String Name of property
propertyDefinition Object Property settings
Source:

defineValueType(type, aliases)

Define a new value type that can be used in model schemas as a property type.
Parameters:
Name Type Description
type function Type constructor.
aliases
Source:

deleteModelByName(modelName)

Remove a model from the registry.
Parameters:
Name Type Description
modelName String
Source:

extendModel(model)

Extend existing model with specified properties Example: Instead of extending a model with attributes like this (for example): ```js db.defineProperty('Content', 'competitionType', { type: String }); db.defineProperty('Content', 'expiryDate', { type: Date, index: true }); db.defineProperty('Content', 'isExpired', { type: Boolean, index: true }); ``` This method enables you to extend a model as follows (for example): ```js db.extendModel('Content', { competitionType: String, expiryDate: { type: Date, index: true }, isExpired: { type: Boolean, index: true } }); ```
Parameters:
Name Type Description
model String Name of model
Properties:
Name Type Description
type String Datatype of property: Must be an [LDL type](http://docs.strongloop.com/display/LB/LoopBack+types).
index Boolean True if the property is an index; false otherwise.
Source:

getModel(name, forceCreate) → {ModelClass}

Get a model by name.
Parameters:
Name Type Description
name String The model name
forceCreate Boolean Whether the create a stub for the given name if a model doesn't exist.
Source:
Returns:
The model class
Type
ModelClass

getModelDefinition(name) → {ModelDefinition}

Get the model definition by name
Parameters:
Name Type Description
name String The model name
Source:
Returns:
The model definition
Type
ModelDefinition

getSchemaName(nameopt) → {string}

Get the schema name. If no parameter is given, then an anonymous model name is generated and returned.
Parameters:
Name Type Attributes Description
name string <optional>
The optional name parameter.
Source:
Returns:
The schema name.
Type
string

resolveType(prop)

Resolve the type string to be a function, for example, 'String' to String. Returns {Function} if the type is resolved
Parameters:
Name Type Description
prop String | Object | Array The object whose type is to be resolved
Source: