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