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