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:
```js
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});

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:

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

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

Property name of foreign key field.

model

Object

Model object

RelationMixin.hasMany(modelTo, parameters)

Define a "one to many" relationship by specifying the model name

Examples:

User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});

Book.hasMany(Chapter);

Or, equivalently:

Book.hasMany('chapters', {model: Chapter});

Query and create related models:

Book.create(function(err, book) {
// Create a chapter instance ready to be saved in the data source.var chapter = book.chapters.build({name: 'Chapter 1'});
// Save the new chapter
chapter.save();
// you can also call the Chapter.create method with the `chapters` property which will build a chapter// instance and save the it in the data source.
book.chapters.create({name: 'Chapter 2'}, function(err, savedChapter) {
// this callback is optional
});
// Query chapters for the book
book.chapters(function(err, chapters) { // all chapters with bookId = book.id console.log(chapters);
});
book.chapters({where: {name: 'test'}, function(err, chapters) {
// All chapters with bookId = book.id and name = 'test'console.log(chapters);
});
});

Arguments

Name

Type

Description

modelTo

Object or String

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

parameters

Object

Configuration parameters; see below.

parameters

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

Property name of foreign key field.

model

Object

Model object

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.