aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/miscs/sql.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/miscs/sql.ts')
-rw-r--r--shared/extra-utils/miscs/sql.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/shared/extra-utils/miscs/sql.ts b/shared/extra-utils/miscs/sql.ts
new file mode 100644
index 000000000..b281471ce
--- /dev/null
+++ b/shared/extra-utils/miscs/sql.ts
@@ -0,0 +1,80 @@
1import * as Sequelize from 'sequelize'
2
3let sequelizes: { [ id: number ]: Sequelize.Sequelize } = {}
4
5function getSequelize (serverNumber: number) {
6 if (sequelizes[serverNumber]) return sequelizes[serverNumber]
7
8 const dbname = 'peertube_test' + serverNumber
9 const username = 'peertube'
10 const password = 'peertube'
11 const host = 'localhost'
12 const port = 5432
13
14 const seq = new Sequelize(dbname, username, password, {
15 dialect: 'postgres',
16 host,
17 port,
18 operatorsAliases: false,
19 logging: false
20 })
21
22 sequelizes[serverNumber] = seq
23
24 return seq
25}
26
27function setActorField (serverNumber: number, to: string, field: string, value: string) {
28 const seq = getSequelize(serverNumber)
29
30 const options = { type: Sequelize.QueryTypes.UPDATE }
31
32 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
33}
34
35function setVideoField (serverNumber: number, uuid: string, field: string, value: string) {
36 const seq = getSequelize(serverNumber)
37
38 const options = { type: Sequelize.QueryTypes.UPDATE }
39
40 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
41}
42
43function setPlaylistField (serverNumber: number, uuid: string, field: string, value: string) {
44 const seq = getSequelize(serverNumber)
45
46 const options = { type: Sequelize.QueryTypes.UPDATE }
47
48 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
49}
50
51async function countVideoViewsOf (serverNumber: number, uuid: string) {
52 const seq = getSequelize(serverNumber)
53
54 // tslint:disable
55 const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
56
57 const options = { type: Sequelize.QueryTypes.SELECT }
58 const [ { total } ] = await seq.query(query, options)
59
60 if (!total) return 0
61
62 return parseInt(total, 10)
63}
64
65async 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
74export {
75 setVideoField,
76 setPlaylistField,
77 setActorField,
78 countVideoViewsOf,
79 closeAllSequelize
80}