summaryrefslogtreecommitdiff
path: root/lib/models/author.js
blob: 03f832a49aad0625a628a1d25d2200c92d44c76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
// external modules
var Sequelize = require('sequelize')

module.exports = function (sequelize, DataTypes) {
  var Author = sequelize.define('Author', {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    color: {
      type: DataTypes.STRING
    }
  }, {
    indexes: [
      {
        unique: true,
        fields: ['noteId', 'userId']
      }
    ],
    classMethods: {
      associate: function (models) {
        Author.belongsTo(models.Note, {
          foreignKey: 'noteId',
          as: 'note',
          constraints: false,
          onDelete: 'CASCADE',
          hooks: true
        })
        Author.belongsTo(models.User, {
          foreignKey: 'userId',
          as: 'user',
          constraints: false,
          onDelete: 'CASCADE',
          hooks: true
        })
      }
    }
  })
  return Author
}