aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/tag.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/tag.ts')
-rw-r--r--server/models/tag.ts38
1 files changed, 25 insertions, 13 deletions
diff --git a/server/models/tag.ts b/server/models/tag.ts
index 85a0442d2..b2a9c9f81 100644
--- a/server/models/tag.ts
+++ b/server/models/tag.ts
@@ -1,9 +1,20 @@
1import { each } from 'async' 1import { each } from 'async'
2import * as Sequelize from 'sequelize'
2 3
3// --------------------------------------------------------------------------- 4import { addMethodsToModel } from './utils'
5import {
6 TagClass,
7 TagInstance,
8 TagAttributes,
9
10 TagMethods
11} from './tag-interface'
4 12
5module.exports = function (sequelize, DataTypes) { 13let Tag: Sequelize.Model<TagInstance, TagAttributes>
6 const Tag = sequelize.define('Tag', 14let findOrCreateTags: TagMethods.FindOrCreateTags
15
16export default function (sequelize, DataTypes) {
17 Tag = sequelize.define('Tag',
7 { 18 {
8 name: { 19 name: {
9 type: DataTypes.STRING, 20 type: DataTypes.STRING,
@@ -17,35 +28,36 @@ module.exports = function (sequelize, DataTypes) {
17 fields: [ 'name' ], 28 fields: [ 'name' ],
18 unique: true 29 unique: true
19 } 30 }
20 ], 31 ]
21 classMethods: {
22 associate,
23
24 findOrCreateTags
25 }
26 } 32 }
27 ) 33 )
28 34
35 const classMethods = [
36 associate,
37
38 findOrCreateTags
39 ]
40 addMethodsToModel(Tag, classMethods)
41
29 return Tag 42 return Tag
30} 43}
31 44
32// --------------------------------------------------------------------------- 45// ---------------------------------------------------------------------------
33 46
34function associate (models) { 47function associate (models) {
35 this.belongsToMany(models.Video, { 48 Tag.belongsToMany(models.Video, {
36 foreignKey: 'tagId', 49 foreignKey: 'tagId',
37 through: models.VideoTag, 50 through: models.VideoTag,
38 onDelete: 'cascade' 51 onDelete: 'cascade'
39 }) 52 })
40} 53}
41 54
42function findOrCreateTags (tags, transaction, callback) { 55findOrCreateTags = function (tags, transaction, callback) {
43 if (!callback) { 56 if (!callback) {
44 callback = transaction 57 callback = transaction
45 transaction = null 58 transaction = null
46 } 59 }
47 60
48 const self = this
49 const tagInstances = [] 61 const tagInstances = []
50 62
51 each(tags, function (tag, callbackEach) { 63 each(tags, function (tag, callbackEach) {
@@ -60,7 +72,7 @@ function findOrCreateTags (tags, transaction, callback) {
60 72
61 if (transaction) query.transaction = transaction 73 if (transaction) query.transaction = transaction
62 74
63 self.findOrCreate(query).asCallback(function (err, res) { 75 Tag.findOrCreate(query).asCallback(function (err, res) {
64 if (err) return callbackEach(err) 76 if (err) return callbackEach(err)
65 77
66 // res = [ tag, isCreated ] 78 // res = [ tag, isCreated ]