e1c6d7977cbc0e542bdec6aa4be14cb166ba5f20
[motion.git] / server / models / channel.js
1 /**
2  * Channel Model
3  */
4
5 module.exports = function(sequelize, DataTypes) {
6
7   var Channel = sequelize.define('Channel', {
8     name: {
9       type: DataTypes.STRING,
10       validate: {
11         isUnique: function(name, next) {
12           Channel
13             .find({
14               where: {
15                 name: name
16               }
17             })
18             .done(function(error, channel) {
19               if (error)
20                 next(error);
21
22               if (channel)
23                 next({
24                   message: 'name already used'
25                 });
26
27               next();
28             });
29         }
30       }
31     },
32     title: DataTypes.STRING,
33     link: DataTypes.STRING,
34     icon: DataTypes.STRING,
35     position: DataTypes.STRING
36   }, {
37     tableName: 'channels',
38     associate: function(models) {
39       Channel.belongsToMany(models.User, {
40         through: 'user_has_channels'
41       });
42     }
43   });
44
45   return Channel;
46 };