diff options
Diffstat (limited to 'server/models/view/video-view.ts')
-rw-r--r-- | server/models/view/video-view.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/server/models/view/video-view.ts b/server/models/view/video-view.ts new file mode 100644 index 000000000..df462e631 --- /dev/null +++ b/server/models/view/video-view.ts | |||
@@ -0,0 +1,60 @@ | |||
1 | import { literal, Op } from 'sequelize' | ||
2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript' | ||
3 | import { AttributesOnly } from '@shared/typescript-utils' | ||
4 | import { VideoModel } from '../video/video' | ||
5 | |||
6 | @Table({ | ||
7 | tableName: 'videoView', | ||
8 | updatedAt: false, | ||
9 | indexes: [ | ||
10 | { | ||
11 | fields: [ 'videoId' ] | ||
12 | }, | ||
13 | { | ||
14 | fields: [ 'startDate' ] | ||
15 | } | ||
16 | ] | ||
17 | }) | ||
18 | export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> { | ||
19 | @CreatedAt | ||
20 | createdAt: Date | ||
21 | |||
22 | @AllowNull(false) | ||
23 | @Column(DataType.DATE) | ||
24 | startDate: Date | ||
25 | |||
26 | @AllowNull(false) | ||
27 | @Column(DataType.DATE) | ||
28 | endDate: Date | ||
29 | |||
30 | @AllowNull(false) | ||
31 | @Column | ||
32 | views: number | ||
33 | |||
34 | @ForeignKey(() => VideoModel) | ||
35 | @Column | ||
36 | videoId: number | ||
37 | |||
38 | @BelongsTo(() => VideoModel, { | ||
39 | foreignKey: { | ||
40 | allowNull: false | ||
41 | }, | ||
42 | onDelete: 'CASCADE' | ||
43 | }) | ||
44 | Video: VideoModel | ||
45 | |||
46 | static removeOldRemoteViewsHistory (beforeDate: string) { | ||
47 | const query = { | ||
48 | where: { | ||
49 | startDate: { | ||
50 | [Op.lt]: beforeDate | ||
51 | }, | ||
52 | videoId: { | ||
53 | [Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)') | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | return VideoViewModel.destroy(query) | ||
59 | } | ||
60 | } | ||