1 import { each } from 'async'
2 import * as Sequelize from 'sequelize'
4 import { addMethodsToModel } from './utils'
11 } from './tag-interface'
13 let Tag: Sequelize.Model<TagInstance, TagAttributes>
14 let findOrCreateTags: TagMethods.FindOrCreateTags
16 export default function (sequelize, DataTypes) {
17 Tag = sequelize.define('Tag',
20 type: DataTypes.STRING,
35 const classMethods = [
40 addMethodsToModel(Tag, classMethods)
45 // ---------------------------------------------------------------------------
47 function associate (models) {
48 Tag.belongsToMany(models.Video, {
50 through: models.VideoTag,
55 findOrCreateTags = function (tags, transaction, callback) {
57 callback = transaction
61 const tagInstances = []
63 each(tags, function (tag, callbackEach) {
73 if (transaction) query.transaction = transaction
75 Tag.findOrCreate(query).asCallback(function (err, res) {
76 if (err) return callbackEach(err)
78 // res = [ tag, isCreated ]
80 tagInstances.push(tag)
84 return callback(err, tagInstances)