diff options
Diffstat (limited to 'server/models/video/tag.ts')
-rw-r--r-- | server/models/video/tag.ts | 105 |
1 files changed, 46 insertions, 59 deletions
diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index 0c0757fc8..0ae74d808 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts | |||
@@ -1,73 +1,60 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as Promise from 'bluebird' | 2 | import { Transaction } from 'sequelize' |
3 | 3 | import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
4 | import { addMethodsToModel } from '../utils' | 4 | import { isVideoTagValid } from '../../helpers/custom-validators/videos' |
5 | import { | 5 | import { throwIfNotValid } from '../utils' |
6 | TagInstance, | 6 | import { VideoModel } from './video' |
7 | TagAttributes, | 7 | import { VideoTagModel } from './video-tag' |
8 | 8 | ||
9 | TagMethods | 9 | @Table({ |
10 | } from './tag-interface' | 10 | tableName: 'tag', |
11 | 11 | timestamps: false, | |
12 | let Tag: Sequelize.Model<TagInstance, TagAttributes> | 12 | indexes: [ |
13 | let findOrCreateTags: TagMethods.FindOrCreateTags | ||
14 | |||
15 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
16 | Tag = sequelize.define<TagInstance, TagAttributes>('Tag', | ||
17 | { | 13 | { |
18 | name: { | 14 | fields: [ 'name' ], |
19 | type: DataTypes.STRING, | 15 | unique: true |
20 | allowNull: false | ||
21 | } | ||
22 | }, | ||
23 | { | ||
24 | timestamps: false, | ||
25 | indexes: [ | ||
26 | { | ||
27 | fields: [ 'name' ], | ||
28 | unique: true | ||
29 | } | ||
30 | ] | ||
31 | } | 16 | } |
32 | ) | ||
33 | |||
34 | const classMethods = [ | ||
35 | associate, | ||
36 | |||
37 | findOrCreateTags | ||
38 | ] | 17 | ] |
39 | addMethodsToModel(Tag, classMethods) | 18 | }) |
19 | export class TagModel extends Model<TagModel> { | ||
40 | 20 | ||
41 | return Tag | 21 | @AllowNull(false) |
42 | } | 22 | @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) |
23 | @Column | ||
24 | name: string | ||
43 | 25 | ||
44 | // --------------------------------------------------------------------------- | 26 | @CreatedAt |
27 | createdAt: Date | ||
45 | 28 | ||
46 | function associate (models) { | 29 | @UpdatedAt |
47 | Tag.belongsToMany(models.Video, { | 30 | updatedAt: Date |
31 | |||
32 | @BelongsToMany(() => VideoModel, { | ||
48 | foreignKey: 'tagId', | 33 | foreignKey: 'tagId', |
49 | through: models.VideoTag, | 34 | through: () => VideoTagModel, |
50 | onDelete: 'CASCADE' | 35 | onDelete: 'CASCADE' |
51 | }) | 36 | }) |
52 | } | 37 | Videos: VideoModel[] |
53 | 38 | ||
54 | findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction) { | 39 | static findOrCreateTags (tags: string[], transaction: Transaction) { |
55 | const tasks: Promise<TagInstance>[] = [] | 40 | const tasks: Bluebird<TagModel>[] = [] |
56 | tags.forEach(tag => { | 41 | tags.forEach(tag => { |
57 | const query: Sequelize.FindOrInitializeOptions<TagAttributes> = { | 42 | const query = { |
58 | where: { | 43 | where: { |
59 | name: tag | 44 | name: tag |
60 | }, | 45 | }, |
61 | defaults: { | 46 | defaults: { |
62 | name: tag | 47 | name: tag |
48 | } | ||
63 | } | 49 | } |
64 | } | ||
65 | 50 | ||
66 | if (transaction) query.transaction = transaction | 51 | if (transaction) query['transaction'] = transaction |
67 | 52 | ||
68 | const promise = Tag.findOrCreate(query).then(([ tagInstance ]) => tagInstance) | 53 | const promise = TagModel.findOrCreate(query) |
69 | tasks.push(promise) | 54 | .then(([ tagInstance ]) => tagInstance) |
70 | }) | 55 | tasks.push(promise) |
56 | }) | ||
71 | 57 | ||
72 | return Promise.all(tasks) | 58 | return Promise.all(tasks) |
59 | } | ||
73 | } | 60 | } |