Subscribe to our list to receive the latest updates on Node on Fire

Class: Models

Models

new Models()

The models module which is Node on Fire's ORM backed by PostgreSQL. It is designed to have a similar API on the front-end and the back-end. This allows you to do most things in the front-end, but move performance-critical and sensitive code to the back-end.

Using the Models module you can define models. A model contains a list of properties and associations. One-to-one, one-to-many and many-to-many associations are supported.

Models are persisted to the database. This happens in the release phase in the Migrations module. See Migrations.

To create a model. For example, when creating a User model:

function User() {
    this.name = [this.String];
}
app.model(User);

A UserModel is added to the dependency injection so you can access the User model via:

app.service(function TestService(UserModel) {
    UserModel.findOne();
});

You can also add methods to your models:

function User() {
    this.name = [this.String];
    this.getTest = function() {
        return 123;
    };
}
app.model(User);

Now the User#getTest is available in, for example, your service:

app.service(function TestService(UserModel) {
    UserModel.getTest();
});

The method is also available as instance method on your model instances.

Methods

_addModel(modelConstructor, modelName)

Allocates an object with the modelConstructor and inherits with the Model constructor. Sets the model on the models object so it's available as this.models.ModelName (where ModelName is the name of the model).

Parameters:
Name Type Description
modelConstructor Constructor

The constructor of the model.

modelName String

The name of the model to add.

Returns:
Type
Model

addModelConstructor(modelConstructor)

Adds a model constructor.

If the Models#setup has already been called, immediately loads the model for the given constructor.

Parameters:
Name Type Description
modelConstructor Constructor

The model's constructor function.

createModel(modelName, properties)

Creates a modelConstructor, and creates the associated model.

This method is only invoked from migrations.

Parameters:
Name Type Description
modelName String

The name of the model to create.

properties Dictionary

All the properties in key-value pairs. Where value is an array of PropertyTypes items.

To Do:
  • Move this to migrations.

destroyModel(modelName)

Destroys a model and removes all it's associations.

This destroys the in-memory model and doens't actually drop the backing table or any model instances.

Parameters:
Name Type Description
modelName String

The name of the model to destroy.

execute(query, parameters)

Executes a raw query.

Parameters:
Name Type Description
query String

The SQL statement to execute.

parameters Array

Any parameters to pass to the statement.

Returns:

Resolves with a result set, see the pg module for more info.

Type
Promise

findModel(modelName)

Returns Model with the name.

Parameters:
Name Type Description
modelName String

The name of the model to return.

forEach(callback)

Convenience model to loop over all the models.

Parameters:
Name Type Description
callback function

The callback function to invoke for every model.

getAuthenticator()

Returns the authenticator model.

The authenticator model is, for example, a User or Account model.

To declare an authenticator model, set any of the model's properties to PropertyTypes#Authenticate.

getModel(modelName)

Returns the model. If the model does not exists, this method throws an error.

Parameters:
Name Type Description
modelName String

The name of the model.

loadModelConstructor(modelConstructor)

Stores the modelConstructor so that the model gets loaded when Model#setup is invoked.

Parameters:
Name Type Description
modelConstructor Constructor

The model's constructor.

model(modelConstructor)

Sets up a model with a model constructor.

A model constructor defines a model with the name of the constructor. Please note that this constructor is invoked to set up a model, and not when a model instance is created.

Every property set in the constructor will be considered a model property. The value should be an array of Models~PropertyType.

It is not allowed to start a property name with _ or $ as these prefixes are currently reserved.

An id property is automatically created, if it does not exist, with [this.UUID, this.CanUpdate(false)] property type, and is considered the model's primary key.

For more information on all property types, see PropertyTypes.

Set any of the model constructor's prototype methods after invoking this method as this method inherits the constructor from Model thus overwriting the constructor's prototype.

Parameters:
Name Type Description
modelConstructor Constructor

The named model constructor.

Throws:
A model name "Model" is currently reserved and this method will throw an error when trying to add Model. ```js function User() { this.name = [this.String]; } app.model(User); User.prototype.test = function() { ... }; ```
Type
Error

setActiveMigration(migration)

Parameters:
Name Type Description
migration Migration

The currently running migration.

To Do:
  • Move this to Migrations. Sets the active migration on all models. This is used to proxy all calls on models during a migration to the migration.

setup(basePath)

Sets up the datastore connection and loads all models from models/. The models are only loaded if basePath is provided.

Parameters:
Name Type Description
basePath String

The app's base path.

Returns:
Type
Promise

sql(upQuery, downQuery)

Adds a query which gets executed in the release stage. Through this method, you can add a query to the migration up and down method. This makes it possible to manage indices in your code or create triggers.

For example, the following would ensure an index on your users table when querying on the name column.

app.sql(
    'CREATE INDEX test_index ON users (name)',
    'DROP INDEX test_index'
);

When running fire build a new migration will be created with the upQuery in the up method of the migration and the downQuery in the down method of the migration. To actually apply the index you have to run fire release.

Parameters:
Name Type Description
upQuery String

The query to be invoked in the migration up method.

downQuery String

The query to be invoked in the migration down method.

stop()

Stops the models module. This closes the connection pool to the database.

Do you have any questions or comments on this page? Start a discussion below.

comments powered by Disqus
Subscribe to our list to receive the latest updates on Node on Fire