Models
The models are the entities responsible for accessing the database(s) and getting data, so the controllers (or the REST API) can manage to send it to the user!
RhapsodyJS models follow a generic template, so both server-side and client-side model (e.g. Backbone model) can be generated without code duplication.
All the models of an app must be in the app/models
folder.
Accessing a model
When programming some action view, you can require a model using the method: Rhapsody.requireModel(modelName), and then use it as a JugglingDB model normally. To see how to use the models, check the JugglingDB documentation.
Spec
var ModelName = {
attributes: {
//Model attributes (see below)
},
sharedMethods: {
//Methods that will be both in server-side and client-side model
},
clientMethods: {
//Methods that will be only in the client-side model
},
serverMethods: {
//Methods that will be only in the server-side model
},
hooks: {
//Hooks (see below)
}
options: {
//Model options (see below)
},
relationships: {
//Model relationships (see below)
}
}
module.exports = ModelName;
Attributes
A model attribute can be writen by two ways:
attributeWithoutOptions: attributeType,
attributeWithOptions: {
option: optionValue (see the options below)
}
Attribute types
As RhapsodyJS uses JugglingDB for database access, the attribute types dependes on the adapter you're using. But most of adapters accept the following attribute types:
String
Number
Date
Boolean
Object
(You must pass the object, not writeObject
in the type)
Attribute options
A model attribute can have the following options:
type
The type of the attribute. For available types, see above. (required)serverValidations
Array of method names (that must be in the serverMethods) that validates the attributedefault
Default value of the attributerequired
If the attribute is requiredrestricted
If the attribute should be omited when the model is sent via RESTful API (see REST API)
Methods
Shared methods
These methods will be both in the server-side models (returned from a database query) and the client-side models (e.g. Backbone models), so shouldn't contain specific server-side or client-side code.
Client and Server Methods
Each on them, as the name says, will be attached to theirs respective sides (client-side and server-side) models.
Model options
A model can have the following options:
allowREST
Allow the creation of a RESTful API for this model (see REST API) (defaults to true)middlewares
Middlewares names (see the Middlewares session) tha the request to the RESTful API will pass before have access to the data.adapter
Name of the adapter this model uses (see the Database adapters session). If not specified, will use the defaultAdapter (see defaultAdapater)generateClientModel
If, when building the app, RhapsodyJS should create a client model for this model, based in theclientModelAdapter
you've set on the environment config file of your app (defaults to true).
Hooks
In this option you'll set the hooks of your model. Each hook accepts a single argument next
, that you'll call after the hook
is complete (i.e. the hooks are asynchronous). To access the properties of the model, use this
(e.g. this.name
to access the name
property).
The supported hooks are:
afterInitialize
beforeCreate
afterCreate
beforeSave
afterSave
beforeUpdate
afterUpdate
beforeDestroy
afterDestroy
beforeValidate
afterValidate
Relationships
Here you'll set the relationships between your model and other models. Each relationship is in the form:
relationshipName: {
type: relationshipType,
with: relatedModel/*,
through: relationshipModel (see below),
foreignKey: relationshipForeignKey (see below)
*/
}
Relationship types
Each relationship can have one of the following types:
hasMany
belongsTo
hasAndBelongsToMany
Relationship with
Should be the name (not the require) of the model that it's related to.
Relationship through
If it's a hasAndBelongsToMany
, you can choose to it happen through one of your models.
You can set it by this property, setting is as the name (no the require) of the model of the relationship.
Relationship foreignKey
If it's a hasMany
or belongsTo
relationship, you can set here what's the foreign key that "links" the two models.