Show:

Properties

Model.attributes
(
  • attr
)
static public

Model attributes definition. This needs to be overridden to define the various attributes the model will have.

Gets passed a function attr which should be called multiple times (once for each attribute)

Example

attr('title', 'string')
  

To specify a default value, pass an options object with an appropriate default value

Example

attr('isAccepted', 'boolean', {
    defaultValue: true
  })
  

The default value can also be specified via a function

Example

attr('title', 'string', {
    defaultValue: function () {
      return 'default title'
    }
  })
  

Parameters:

  • attr Function
    • function used to define model attributes
constructor
(
  • props
)
public

Inherited from Base but overwritten in src/classes/model.js:16

Parameters:

  • props Object
    • properties hash that the model instance should be created with
delete () Promise public

Delete the model instance

Returns:

Model.relationships
(
  • relation
)
static public

Model relationship definition. This should be overriden to define any relationships to other models that are needed.

Gets passed a function relation which should be called multiple times (once for each relationship) relation has the following signature

relation(type, modelName, options)
  

type can be either belongsTo or hasMany modelName is a models Model.modelName property options is an object which may contain any or all of the keys name, keyFrom or keyTo

Example:

relationships (relation) {
    relation('hasMany', 'comment')
  }
  

Example:

relationships (relation) {
    relation('hasMany', 'comment', {name: 'comments', keyFrom: 'id', keyTo: 'postId'})
  }
  

If not specified, name is a pluralized version of the models name for hasMany or singular for belongsTo If not specified, keyFrom is the models idField for hasMany or a concatenation of modelName and the string 'Id' for belongsTo If not specified, keyTo is a concatenation of the related model's modelName and the string 'Id' for for hasMany or the related models idField for belongsTo

Parameters:

save () Promise public

Save's the model instance

Returns:

toJSON () Object public

Returns:

Object:
  • plain javascript representation of object (without relationship state)

adapter

Unknown public static

attributes

Unknown public

Provides a copy of the models internal state as a plain javascript object Does not provide relationship data as models. For this you need to use individual relationship getters.

Example:

const model = new PostModel({
    title: 'My title',
    comments: [
      {comment: 'This is my comment'},
      {comment: 'This is another comment'}
    ]
  })
  
  model.comments.then(comments => {
    // comments are CommentModel instances
  })
  
  // comments are plain javascript objects in an array
  const comments = model.attributes.comments
  

attributes

Unknown public

Setter for model instances internal state. Called in model constructor with passed in state data

Performs validation of incoming data and guards access to internal state. All individual getters and setters go through this setter thereby providing a single entry point for data validation and cleanup

Internal state is stored as a plain object and can contain nested relationship data Example: For a model

class PostModel extends Model {
    static attributes (attr) {
      attr('title', 'string')
    }
    static relationships (relation) {
      relation('hasMany', 'comment')
    }
  }
  

The following is valid though unspecifiedProp will be ignored:

new PostModel({
    title: 'My title',
    unspecifiedProp: true,
    comments: [
      {comment: 'This is my comment'},
      {comment: 'This is another comment'}
    ]
  })
  

The internal state of the model will be:

{
    title: 'My title',
    comments: [
      {comment: 'This is my comment'},
      {comment: 'This is another comment'}
    ]
  }
  

Sub-properties:

  • props Object
    • a hash of keys and values to set as the models internal data

idField

Unknown public static

Specifies the name of the models id field.

By default the name of the models id field is 'id'

This method can be overridden to specify a different name to use for the models id field in child classes.

Example:

static get idField () {
   return 'modelId'
  }
  

By default id fields are of type 'number'.

If another type is desired then a matching named field should be provided in the model attributes hash.

Example:

static get idField () {
    return 'customIdField'
  }
  
  static attributes (attr) {
    attr('customIdField', 'string')
  }
  

isNew

Unknown public

Assumes the model to be in a new state if the models id is not present in the attributes hash

modelName

Unknown public static

Model name matching filename (not classname) Used throughout Ash to reference the model

Example: For a model class named MyPostModel or MyPostsModel, modelName will be my-post

serializer

Unknown public static

tableName

Unknown public static

Table name for model of the form lowercase, pluralized with underscores separating words Calculated from models class name with Model stripped off the end

Example: For model MyPostModel, tablename would be my_posts

type

Unknown public static

Exposes a pluralised version of the models name Matches up with jsonapi specs type attribute

Example: for a model post, type would be posts