diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/video/video-file.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/models/video/video-file.ts')
-rw-r--r-- | server/models/video/video-file.ts | 109 |
1 files changed, 42 insertions, 67 deletions
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 600141994..df4067a4e 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts | |||
@@ -1,81 +1,56 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { isVideoFileInfoHashValid, isVideoFileResolutionValid, isVideoFileSizeValid } from '../../helpers/custom-validators/videos' | 3 | import { isVideoFileInfoHashValid, isVideoFileResolutionValid, isVideoFileSizeValid } from '../../helpers/custom-validators/videos' |
4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 4 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
5 | import { throwIfNotValid } from '../utils' | ||
6 | import { VideoModel } from './video' | ||
5 | 7 | ||
6 | import { addMethodsToModel } from '../utils' | 8 | @Table({ |
7 | import { VideoFileAttributes, VideoFileInstance } from './video-file-interface' | 9 | tableName: 'videoFile', |
8 | 10 | indexes: [ | |
9 | let VideoFile: Sequelize.Model<VideoFileInstance, VideoFileAttributes> | ||
10 | |||
11 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
12 | VideoFile = sequelize.define<VideoFileInstance, VideoFileAttributes>('VideoFile', | ||
13 | { | 11 | { |
14 | resolution: { | 12 | fields: [ 'videoId' ] |
15 | type: DataTypes.INTEGER, | ||
16 | allowNull: false, | ||
17 | validate: { | ||
18 | resolutionValid: value => { | ||
19 | const res = isVideoFileResolutionValid(value) | ||
20 | if (res === false) throw new Error('Video file resolution is not valid.') | ||
21 | } | ||
22 | } | ||
23 | }, | ||
24 | size: { | ||
25 | type: DataTypes.BIGINT, | ||
26 | allowNull: false, | ||
27 | validate: { | ||
28 | sizeValid: value => { | ||
29 | const res = isVideoFileSizeValid(value) | ||
30 | if (res === false) throw new Error('Video file size is not valid.') | ||
31 | } | ||
32 | } | ||
33 | }, | ||
34 | extname: { | ||
35 | type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), | ||
36 | allowNull: false | ||
37 | }, | ||
38 | infoHash: { | ||
39 | type: DataTypes.STRING, | ||
40 | allowNull: false, | ||
41 | validate: { | ||
42 | infoHashValid: value => { | ||
43 | const res = isVideoFileInfoHashValid(value) | ||
44 | if (res === false) throw new Error('Video file info hash is not valid.') | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | }, | 13 | }, |
49 | { | 14 | { |
50 | indexes: [ | 15 | fields: [ 'infoHash' ] |
51 | { | ||
52 | fields: [ 'videoId' ] | ||
53 | }, | ||
54 | { | ||
55 | fields: [ 'infoHash' ] | ||
56 | } | ||
57 | ] | ||
58 | } | 16 | } |
59 | ) | ||
60 | |||
61 | const classMethods = [ | ||
62 | associate | ||
63 | ] | 17 | ] |
64 | addMethodsToModel(VideoFile, classMethods) | 18 | }) |
65 | 19 | export class VideoFileModel extends Model<VideoFileModel> { | |
66 | return VideoFile | 20 | @CreatedAt |
67 | } | 21 | createdAt: Date |
68 | 22 | ||
69 | // ------------------------------ STATICS ------------------------------ | 23 | @UpdatedAt |
70 | 24 | updatedAt: Date | |
71 | function associate (models) { | 25 | |
72 | VideoFile.belongsTo(models.Video, { | 26 | @AllowNull(false) |
27 | @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution')) | ||
28 | @Column | ||
29 | resolution: number | ||
30 | |||
31 | @AllowNull(false) | ||
32 | @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size')) | ||
33 | @Column(DataType.BIGINT) | ||
34 | size: number | ||
35 | |||
36 | @AllowNull(false) | ||
37 | @Column(DataType.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME))) | ||
38 | extname: string | ||
39 | |||
40 | @AllowNull(false) | ||
41 | @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash')) | ||
42 | @Column | ||
43 | infoHash: string | ||
44 | |||
45 | @ForeignKey(() => VideoModel) | ||
46 | @Column | ||
47 | videoId: number | ||
48 | |||
49 | @BelongsTo(() => VideoModel, { | ||
73 | foreignKey: { | 50 | foreignKey: { |
74 | name: 'videoId', | ||
75 | allowNull: false | 51 | allowNull: false |
76 | }, | 52 | }, |
77 | onDelete: 'CASCADE' | 53 | onDelete: 'CASCADE' |
78 | }) | 54 | }) |
55 | Video: VideoModel | ||
79 | } | 56 | } |
80 | |||
81 | // ------------------------------ METHODS ------------------------------ | ||