Modella Cheat Sheet

From WikiOD

Here is the cheatsheat of Modella and basic guide on how to use Modella

Defining models[edit | edit source]

User = Modella('User')
  .attr('name')
  .attr('email', { required: true })
  .use(require('modella-validators'))
  .validator (u) ->
    u.error('username', 'is required')  unless u.has('username')

Instances[edit | edit source]

user
  .name()
  .name('John')
  .set(name: 'John')
  .has('name')   # → true
  .isNew()
  .isValid()
  .save (err) ->
  .remove (err) ->
  .removed
  .model         # === User

Events[edit | edit source]

Emitting[edit | edit source]

Model.emit('event', [data...])
record.emit('event', [data...])

List of events[edit | edit source]

user
  .on 'save', ->
  .on 'create', ->
  .on 'saving', (data, done) -> done()
  .on 'remove', ->
  .on 'removing', (data, done) -> done()
  .on 'valid', ->
  .on 'invalid', ->
  .on 'change', ->
  .on 'change email', ->
  .on 'initializing', (instance, attrs) ->
  .on 'initialize', ->
  .on 'error', -> failed to save model
  .on 'setting', (instance, attrs) ->  # on Model#set()
  .on 'attr', -> # new attr via Model.attr()

Misc[edit | edit source]

Plugins[edit | edit source]

MyPlugin = ->
  return (Model) ->

    Model.method = ...
    Model.prototype.method = ...
    Model.attr(...)

    Model

A plugin is a function that returns a model decorator (ie, a function that takes in a model and returns a model).

Memory[edit | edit source]

User
  .all (err, users) ->
  .find id, (err, user) ->
  .remove ->
  .save ->
  .update ->

Credit:rstacruz