1 import * as Sequelize from 'sequelize'
2 import * as Promise from 'bluebird'
4 import { addMethodsToModel } from '../utils'
10 } from './tag-interface'
12 let Tag: Sequelize.Model<TagInstance, TagAttributes>
13 let findOrCreateTags: TagMethods.FindOrCreateTags
15 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
16 Tag = sequelize.define<TagInstance, TagAttributes>('Tag',
19 type: DataTypes.STRING,
34 const classMethods = [
39 addMethodsToModel(Tag, classMethods)
44 // ---------------------------------------------------------------------------
46 function associate (models) {
47 Tag.belongsToMany(models.Video, {
49 through: models.VideoTag,
54 findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction) {
55 const tasks: Promise<TagInstance>[] = []
66 if (transaction) query.transaction = transaction
68 const promise = Tag.findOrCreate(query).then(([ tagInstance ]) => tagInstance)
72 return Promise.all(tasks)