Built motion from commit 95b01fa.|0.0.70
[motion.git] / server / models / trigger.js
1 /**
2  * Trigger Model
3  */
4
5 module.exports = function(sequelize, DataTypes) {
6
7   var Trigger = sequelize.define('Trigger', {
8     name: DataTypes.STRING,
9     channel: DataTypes.STRING,
10     description: DataTypes.STRING,
11     status: {
12       type: DataTypes.BOOLEAN,
13       defaultValue: false
14     }
15   }, {
16     tableName: 'triggers',
17     associate: function(models) {
18       Trigger.hasMany(models.ToolsCondition, {
19         as: {
20           plural: 'All',
21           singular: 'OneAll'
22         },
23         foreignKey: 'TriggerAllId',
24         onDelete: 'cascade',
25         hooks: true
26       });
27       Trigger.hasMany(models.ToolsCondition, {
28         as: {
29           plural: 'Any',
30           singular: 'OneAny'
31         },
32         foreignKey: 'TriggerAnyId',
33         onDelete: 'cascade',
34         hooks: true
35       });
36       Trigger.hasMany(models.ToolsAction, {
37         as: 'Actions',
38         onDelete: 'cascade',
39         hooks: true
40       });
41       Trigger.addScope('all', {
42         include: [{
43           model: models.ToolsCondition,
44           as: 'All',
45           attributes: ['field', 'operator', 'value']
46         }, {
47           model: models.ToolsCondition,
48           as: 'Any',
49           attributes: ['field', 'operator', 'value']
50         }, {
51           model: models.ToolsAction,
52           as: 'Actions',
53           attributes: ['action', 'data1', 'data2', 'data3', 'data4', 'data5']
54         }]
55       });
56     }
57   });
58
59   return Trigger;
60 };