aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/miscs/sql-command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/server-commands/miscs/sql-command.ts')
-rw-r--r--shared/server-commands/miscs/sql-command.ts141
1 files changed, 141 insertions, 0 deletions
diff --git a/shared/server-commands/miscs/sql-command.ts b/shared/server-commands/miscs/sql-command.ts
new file mode 100644
index 000000000..bedb3349b
--- /dev/null
+++ b/shared/server-commands/miscs/sql-command.ts
@@ -0,0 +1,141 @@
1import { QueryTypes, Sequelize } from 'sequelize'
2import { AbstractCommand } from '../shared/abstract-command'
3
4export class SQLCommand extends AbstractCommand {
5 private sequelize: Sequelize
6
7 deleteAll (table: string) {
8 const seq = this.getSequelize()
9
10 const options = { type: QueryTypes.DELETE }
11
12 return seq.query(`DELETE FROM "${table}"`, options)
13 }
14
15 async getCount (table: string) {
16 const seq = this.getSequelize()
17
18 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
19
20 const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options)
21 if (total === null) return 0
22
23 return parseInt(total, 10)
24 }
25
26 setActorField (to: string, field: string, value: string) {
27 const seq = this.getSequelize()
28
29 const options = { type: QueryTypes.UPDATE }
30
31 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
32 }
33
34 setVideoField (uuid: string, field: string, value: string) {
35 const seq = this.getSequelize()
36
37 const options = { type: QueryTypes.UPDATE }
38
39 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
40 }
41
42 setPlaylistField (uuid: string, field: string, value: string) {
43 const seq = this.getSequelize()
44
45 const options = { type: QueryTypes.UPDATE }
46
47 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
48 }
49
50 async countVideoViewsOf (uuid: string) {
51 const seq = this.getSequelize()
52
53 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
54 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
55
56 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
57 const [ { total } ] = await seq.query<{ total: number }>(query, options)
58
59 if (!total) return 0
60
61 return parseInt(total + '', 10)
62 }
63
64 getActorImage (filename: string) {
65 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
66 .then(rows => rows[0])
67 }
68
69 selectQuery (query: string) {
70 const seq = this.getSequelize()
71 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
72
73 return seq.query<any>(query, options)
74 }
75
76 updateQuery (query: string) {
77 const seq = this.getSequelize()
78 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
79
80 return seq.query(query, options)
81 }
82
83 setPluginField (pluginName: string, field: string, value: string) {
84 const seq = this.getSequelize()
85
86 const options = { type: QueryTypes.UPDATE }
87
88 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
89 }
90
91 setPluginVersion (pluginName: string, newVersion: string) {
92 return this.setPluginField(pluginName, 'version', newVersion)
93 }
94
95 setPluginLatestVersion (pluginName: string, newVersion: string) {
96 return this.setPluginField(pluginName, 'latestVersion', newVersion)
97 }
98
99 setActorFollowScores (newScore: number) {
100 const seq = this.getSequelize()
101
102 const options = { type: QueryTypes.UPDATE }
103
104 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
105 }
106
107 setTokenField (accessToken: string, field: string, value: string) {
108 const seq = this.getSequelize()
109
110 const options = { type: QueryTypes.UPDATE }
111
112 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
113 }
114
115 async cleanup () {
116 if (!this.sequelize) return
117
118 await this.sequelize.close()
119 this.sequelize = undefined
120 }
121
122 private getSequelize () {
123 if (this.sequelize) return this.sequelize
124
125 const dbname = 'peertube_test' + this.server.internalServerNumber
126 const username = 'peertube'
127 const password = 'peertube'
128 const host = 'localhost'
129 const port = 5432
130
131 this.sequelize = new Sequelize(dbname, username, password, {
132 dialect: 'postgres',
133 host,
134 port,
135 logging: false
136 })
137
138 return this.sequelize
139 }
140
141}