/
tag.js
71 lines (65 loc) · 2.15 KB
/
tag.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const tagState = require("../controllers/_common/constants")["TAGS"];
let enumValues = Object.values(tagState);
module.exports = (sequelize, DataTypes) => {
let Tag = sequelize.define("Tag", {
text: {
type: DataTypes.STRING,
allowNull: false
},
state: {
type: DataTypes.ENUM(enumValues),
allowNull: false,
defaultValue: tagState.NOT_VALIDATED
},
}, {
// https://sequelize.org/master/manual/models-definition.html#configuration
// Enable optimistic locking. When enabled, sequelize will add a version count attribute
// to the model and throw an OptimisticLockingError error when stale instances are saved.
// Set to true or a string with the attribute name you want to use to enable.
version: true,
// scopes : reuse more easily some common parts
// https://sequelize.org/master/manual/scopes.html
scopes: {
common_attributes: {
attributes: [
["id", "tag_id"],
["text", "tag_text"],
"category_id",
"state",
"version"
]
}
}
});
Tag.associate = function (models) {
// a Tag must have a Tag Category
Tag.belongsTo(models.Tag_Category, {
as: "category",
foreignKey: {
name: "category_id",
allowNull: false
},
onDelete: "CASCADE"
});
// a Tag can be used in multiple configurations
Tag.belongsToMany(models.Configuration, {
through: models.Configuration_Tag,
foreignKey: {
name: "tag_id",
allowNull: false
}
});
// a Tag can be used in multiple exercises
Tag.belongsToMany(models.Exercise, {
through: models.Exercise_Tag,
timestamps: false,
as: "exercises",
foreignKey: {
name: "tag_id",
allowNull: false
}
});
};
return Tag;
};