Skip to main content


We.js model is a wrapper of sequelize models and allows plugin changes with hooks in model definition before set it in sequelize.

Related documentation in sequelize docs:

How to create a model?

With we.js yeoman generator!

Create one empty model:

yo wejs:model post

With model attribute in generation:

# create the post model with title, bodu and published attributes
yo wejs:model post title:string:allowNull=false body:text published:boolean

With associations:

# create the post model with title attibute and creator association, associated with user
yo wejs:model post title:string creator:belongsTo:user

Model Hooks

Model hooks are simple functions how run in sequelize hooks

Generate with yo wejs:modelHook:

yo wejs:modelHook modelHookName

folder: server/models/hooks/


 * model Hook
 * @param  {Object}   record  sequelize record
 * @param  {Object}   options sequelize Model options
 * @param  {Function} done    callback
module.exports = function (record, options, done) {
  // do something with record or options,
  // we is avaible in this.we
  // for errors run done(err)

Model instance methods

Model instance are avaible in records

Generate with yo wejs:modelInstanceMethod:

yo wejs:modelInstanceMethod modelInstanceMethodName

folder: server/models/instanceMethods/


 * Instance methods
module.exports = function () {
  // record is: this
  // do something ...

Model class methods

Model class are avaible in model class in we.db.model[modelName].function

Generate with yo wejs:modelClassMethod:

yo wejs:modelClassMethod modelClassMethodName

folder: server/models/classMethods/


 * Class methods
module.exports = function () {
  // model class is: this
  // do something ...

Model example

Example below is created with command:

yo wejs:model post title:string:allowNull=false body:text published:boolean creator:belongsTo:user

Example file: server/models/post.json

  "attributes": {
    "title": {
      "type": "STRING",
      "allowNull": false
    "body": {
      "type": "TEXT",
      "skipSanitizer": true // skip model attribute sanitizer, use if you want store things like json data
    "published": {
      "type": "BOOLEAN"

  "associations": {
    "creator": {
      "type": "belongsTo",
      "model": "user"

  "classMethods": {
    "anyName": "modelClassMethodName"
  "instanceMethods": {
    "anyName": "modelInstanceMethodName"
  "hooks": {
    "afterCreate": ["registerLog"]

Widget: Comments 13: