dataSource.autoupdate(model, [cb])

Model to migrate. If not present, apply to all models. Can also be an array of Strings.

[cb]

Function

The callback function

dataSource.buildModelFromInstance(name, json, options)

Introspect a JSON object and build a model class

Arguments

Name

Type

Description

name

String

Name of the model

json

Object

The json object representing a model instance

options

Object

Options

Returns

Name

Type

Description

result

dataSource.columnMetadata(modelName, propertyName)

Return column metadata for specified modelName and propertyName

Arguments

Name

Type

Description

modelName

String

The model name

propertyName

String

The property name

Returns

Name

Type

Description

result

Object

column metadata

dataSource.columnName(modelName, propertyName)

Return column name for specified modelName and propertyName

Arguments

Name

Type

Description

modelName

String

The model name

propertyName

String

The property name

Returns

Name

Type

Description

result

String

columnName The column name.

dataSource.columnNames(modelName)

Return column names for specified modelName

Arguments

Name

Type

Description

modelName

String

The model name

Returns

Name

Type

Description

result

Array.<String>

column names

dataSource.createModel(className, properties, properties)

Define a model class. Returns newly created model object.
The first (String) argument specifying the model name is required.
You can provide one or two JSON object arguments, to provide configuration options.
See Model definition reference for details.

includeEmbeds(callback)

Handle Inclusion of EmbedsMany/EmbedsManyWithBelongsTo/EmbedsOne
Relations. Since Embedded docs are part of parents, no need to make
db calls. Let the related function be called for each object to fetch
the results from cache.

modelBuilder.getModel(name, forceCreate)

Whether the create a stub for the given name if a model doesn't exist.

Returns

Name

Type

Description

result

The model class

modelBuilder.getModelDefinition(name)

Get the model definition by name

Arguments

Name

Type

Description

name

String

The model name

Returns

Name

Type

Description

result

ModelDefinition

The model definition

modelBuilder.getSchemaName()

Get the schema name

modelBuilder.resolveType(type)

Resolve the type string to be a function, for example, 'String' to String.
Returns {Function} if the type is resolved

Arguments

Name

Type

Description

type

String

The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive

Class: RelationMixin

RelationMixin

RelationMixin class. Use to define relationships between models.

Class: RelationMixin Static Methods

RelationMixin.belongsTo(modelTo, params)

Declare "belongsTo" relation that sets up a one-to-one connection with another model, such that each
instance of the declaring model "belongs to" one instance of the other model.

For example, if an application includes users and posts, and each post can be written by exactly one user.
The following code specifies that Post has a reference called author to the User model via the userId property of Post
as the foreign key.

Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});

You can then access the author in one of the following styles.
Get the User object for the post author asynchronously:

post.author(callback);

Get the User object for the post author synchronously:

post.author();

Set the author to be the given user:

post.author(user)

Examples:

Suppose the model Post has a belongsTo relationship with User (the author of the post). You could declare it this way:

The related object is cached, so if later you try to get again the author, no additional request will be made.
But there is an optional boolean parameter in first position that set whether or not you want to reload the cache:

post.author(true, function(err, user) {
// The user is reloaded, even if it was already cached.
});

This optional parameter default value is false, so the related object will be loaded from cache if available.

Arguments

Name

Type

Description

modelTo

Class or String

Model object (or String name of model) to which you are creating the relationship.

params

Object

Configuration parameters; see below.

params

Name

Type

Description

as

String

Name of the property in the referring model that corresponds to the foreign key field in the related model.

foreignKey

String

Name of foreign key property.

RelationMixin.hasAndBelongsToMany(modelTo, params)

A hasAndBelongsToMany relation creates a direct many-to-many connection with another model, with no intervening model.
For example, if your application includes users and groups, with each group having many users and each user appearing
in many groups, you could declare the models this way:

The transaction can be committed or rolled back. If timeout happens, the
transaction will be rolled back. Please note a transaction is typically
associated with a pooled connection. Committing or rolling back a transaction
will release the connection back to the pool.

Once the transaction is committed or rolled back, the connection property
will be set to null to mark the transaction to be inactive. Trying to commit
or rollback an inactive transaction will receive an error from the callback.

Please also note that the transaction is only honored with the same data
source/connector instance. CRUD methods will not join the current transaction
if its model is not attached the same data source.

Class: TransactionMixin Instance Methods

transaction.commit(cb)

Commit a transaction and release it back to the pool

Arguments

Name

Type

Description

cb

Function

Callback function

Returns

Name

Type

Description

result

Promise

transaction.rollback(cb)

Rollback a transaction and release it back to the pool

Arguments

Name

Type

Description

cb

Function

Callback function

Returns

Name

Type

Description

result

Promise

Class: Validatable

Validatable

This class provides methods that add validation cababilities to models.
Each of the validations runs when the obj.isValid() method is called.

All of the methods have an options object parameter that has a
message property. When there is only a single error message, this property is just a string;
for example: Post.validatesPresenceOf('title', { message: 'can not be blank' });

In more complicated cases it can be a set of messages, for each possible error condition; for example:
User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});

Validatable.validatesPresenceOf(propertyName, errMsg)

Validate presence of one or more specified properties.
Requires a model to include a property to be considered valid; fails when validated field is blank.

For example, validate presence of title

Post.validatesPresenceOf('title');

Validate that model has first, last, and age properties:

User.validatesPresenceOf('first', 'last', 'age');

Example with custom message

Post.validatesPresenceOf('title', {message:'Cannot be blank'});

Arguments

Name

Type

Description

propertyName

String

One or more property names.

errMsg

Object

Optional custom error message. Default is "can't be blank"

errMsg

Name

Type

Description

message

String

Error message to use instead of default.

Validatable.validatesUniquenessOf(propertyName, Options)

Validate uniqueness. Ensure the value for property is unique in the collection of models.
Not available for all connectors. Currently supported with these connectors:

In Memory

Oracle

MongoDB

// The login must be unique across all User instances.User.validatesUniquenessOf('login');
// Assuming SiteUser.belongsTo(Site)// The login must be unique within each Site.SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });

Class: Validatable Instance Methods

validatable.isValid(callback)

This method performs validation and triggers validation hooks.
Before validation the obj.errors collection is cleaned.
Each validation can add errors to obj.errors collection.
If collection is not blank, validation failed.

NOTE: This method can be called as synchronous only when no asynchronous validation is
configured. It's strongly recommended to run all validations as asyncronous.