1 import * as Bluebird from 'bluebird'
2 import { Transaction } from 'sequelize'
3 import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4 import { isVideoTagValid } from '../../helpers/custom-validators/videos'
5 import { throwIfNotValid } from '../utils'
6 import { VideoModel } from './video'
7 import { VideoTagModel } from './video-tag'
19 export class TagModel extends Model<TagModel> {
22 @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
32 @BelongsToMany(() => VideoModel, {
34 through: () => VideoTagModel,
39 static findOrCreateTags (tags: string[], transaction: Transaction) {
40 const tasks: Bluebird<TagModel>[] = []
51 if (transaction) query['transaction'] = transaction
53 const promise = TagModel.findOrCreate(query)
54 .then(([ tagInstance ]) => tagInstance)
58 return Promise.all(tasks)