X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Ftag.ts;h=ed8df8b48aa84367003161035dda9256eac13c76;hb=3d527ba173a37bd61ec8ad742642bb320d12995c;hp=e39a418cdfb0199965bc94ff39c5eb970c45f09a;hpb=2d3741d6d92e9bd1f41694c7442a6d1da434e1f2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index e39a418cd..ed8df8b48 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts @@ -1,11 +1,12 @@ import * as Bluebird from 'bluebird' -import * as Sequelize from 'sequelize' +import { fn, QueryTypes, Transaction, col } from 'sequelize' import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { isVideoTagValid } from '../../helpers/custom-validators/videos' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoTagModel } from './video-tag' import { VideoPrivacy, VideoState } from '../../../shared/models/videos' +import { MTag } from '@server/typings/models' @Table({ tableName: 'tag', @@ -14,6 +15,10 @@ import { VideoPrivacy, VideoState } from '../../../shared/models/videos' { fields: [ 'name' ], unique: true + }, + { + name: 'tag_lower_name', + fields: [ fn('lower', col('name')) ] as any // FIXME: typings } ] }) @@ -37,10 +42,10 @@ export class TagModel extends Model { }) Videos: VideoModel[] - static findOrCreateTags (tags: string[], transaction: Sequelize.Transaction) { - if (tags === null) return [] + static findOrCreateTags (tags: string[], transaction: Transaction): Promise { + if (tags === null) return Promise.resolve([]) - const tasks: Bluebird[] = [] + const tasks: Bluebird[] = [] tags.forEach(tag => { const query = { where: { @@ -48,12 +53,11 @@ export class TagModel extends Model { }, defaults: { name: tag - } + }, + transaction } - if (transaction) query['transaction'] = transaction - - const promise = TagModel.findOrCreate(query) + const promise = TagModel.findOrCreate(query) .then(([ tagInstance ]) => tagInstance) tasks.push(promise) }) @@ -73,10 +77,10 @@ export class TagModel extends Model { const options = { bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED }, - type: Sequelize.QueryTypes.SELECT + type: QueryTypes.SELECT as QueryTypes.SELECT } - return TagModel.sequelize.query(query, options) + return TagModel.sequelize.query<{ name: string }>(query, options) .then(data => data.map(d => d.name)) } }