]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { QueryTypes, Sequelize } from 'sequelize' |
df66d815 | 2 | |
1735c825 | 3 | let sequelizes: { [ id: number ]: Sequelize } = {} |
c8000975 | 4 | |
df66d815 | 5 | function getSequelize (serverNumber: number) { |
c8000975 C |
6 | if (sequelizes[serverNumber]) return sequelizes[serverNumber] |
7 | ||
df66d815 C |
8 | const dbname = 'peertube_test' + serverNumber |
9 | const username = 'peertube' | |
10 | const password = 'peertube' | |
11 | const host = 'localhost' | |
12 | const port = 5432 | |
13 | ||
c8000975 | 14 | const seq = new Sequelize(dbname, username, password, { |
df66d815 C |
15 | dialect: 'postgres', |
16 | host, | |
17 | port, | |
df66d815 C |
18 | logging: false |
19 | }) | |
c8000975 C |
20 | |
21 | sequelizes[serverNumber] = seq | |
22 | ||
23 | return seq | |
df66d815 C |
24 | } |
25 | ||
26 | function setActorField (serverNumber: number, to: string, field: string, value: string) { | |
27 | const seq = getSequelize(serverNumber) | |
28 | ||
1735c825 | 29 | const options = { type: QueryTypes.UPDATE } |
df66d815 C |
30 | |
31 | return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options) | |
32 | } | |
33 | ||
a130f33c C |
34 | function setVideoField (serverNumber: number, uuid: string, field: string, value: string) { |
35 | const seq = getSequelize(serverNumber) | |
36 | ||
1735c825 | 37 | const options = { type: QueryTypes.UPDATE } |
a130f33c C |
38 | |
39 | return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
40 | } | |
41 | ||
9f79ade6 C |
42 | function setPlaylistField (serverNumber: number, uuid: string, field: string, value: string) { |
43 | const seq = getSequelize(serverNumber) | |
44 | ||
1735c825 | 45 | const options = { type: QueryTypes.UPDATE } |
9f79ade6 C |
46 | |
47 | return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
48 | } | |
49 | ||
cda03765 C |
50 | async function countVideoViewsOf (serverNumber: number, uuid: string) { |
51 | const seq = getSequelize(serverNumber) | |
52 | ||
53 | // tslint:disable | |
54 | const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` | |
55 | ||
1735c825 C |
56 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } |
57 | const [ { total } ] = await seq.query<{ total: number }>(query, options) | |
cda03765 C |
58 | |
59 | if (!total) return 0 | |
60 | ||
1735c825 C |
61 | // FIXME: check if we really need parseInt |
62 | return parseInt(total + '', 10) | |
cda03765 C |
63 | } |
64 | ||
c8000975 C |
65 | async function closeAllSequelize (servers: any[]) { |
66 | for (let i = 1; i <= servers.length; i++) { | |
67 | if (sequelizes[ i ]) { | |
68 | await sequelizes[ i ].close() | |
69 | delete sequelizes[ i ] | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
df66d815 | 74 | export { |
a130f33c | 75 | setVideoField, |
9f79ade6 | 76 | setPlaylistField, |
c8000975 | 77 | setActorField, |
cda03765 | 78 | countVideoViewsOf, |
c8000975 | 79 | closeAllSequelize |
df66d815 | 80 | } |