diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-09 15:37:43 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:18 +0200 |
commit | 9293139fde7091e9badcafa9b570b83cea9a10ad (patch) | |
tree | 9728412f02af4aa59d2c016db47624251e8f6be6 /shared/extra-utils/miscs/sql-command.ts | |
parent | 078f17e6d90376050f43ce639e88e11869b49ee7 (diff) | |
download | PeerTube-9293139fde7091e9badcafa9b570b83cea9a10ad.tar.gz PeerTube-9293139fde7091e9badcafa9b570b83cea9a10ad.tar.zst PeerTube-9293139fde7091e9badcafa9b570b83cea9a10ad.zip |
Introduce sql command
Diffstat (limited to 'shared/extra-utils/miscs/sql-command.ts')
-rw-r--r-- | shared/extra-utils/miscs/sql-command.ts | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/shared/extra-utils/miscs/sql-command.ts b/shared/extra-utils/miscs/sql-command.ts new file mode 100644 index 000000000..2a3e9e607 --- /dev/null +++ b/shared/extra-utils/miscs/sql-command.ts | |||
@@ -0,0 +1,142 @@ | |||
1 | import { QueryTypes, Sequelize } from 'sequelize' | ||
2 | import { AbstractCommand } from '../shared' | ||
3 | |||
4 | export 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 | // tslint:disable | ||
54 | const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' + | ||
55 | `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` | ||
56 | |||
57 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } | ||
58 | const [ { total } ] = await seq.query<{ total: number }>(query, options) | ||
59 | |||
60 | if (!total) return 0 | ||
61 | |||
62 | return parseInt(total + '', 10) | ||
63 | } | ||
64 | |||
65 | getActorImage (filename: string) { | ||
66 | return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`) | ||
67 | .then(rows => rows[0]) | ||
68 | } | ||
69 | |||
70 | selectQuery (query: string) { | ||
71 | const seq = this.getSequelize() | ||
72 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } | ||
73 | |||
74 | return seq.query<any>(query, options) | ||
75 | } | ||
76 | |||
77 | updateQuery (query: string) { | ||
78 | const seq = this.getSequelize() | ||
79 | const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE } | ||
80 | |||
81 | return seq.query(query, options) | ||
82 | } | ||
83 | |||
84 | setPluginField (pluginName: string, field: string, value: string) { | ||
85 | const seq = this.getSequelize() | ||
86 | |||
87 | const options = { type: QueryTypes.UPDATE } | ||
88 | |||
89 | return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options) | ||
90 | } | ||
91 | |||
92 | setPluginVersion (pluginName: string, newVersion: string) { | ||
93 | return this.setPluginField(pluginName, 'version', newVersion) | ||
94 | } | ||
95 | |||
96 | setPluginLatestVersion (pluginName: string, newVersion: string) { | ||
97 | return this.setPluginField(pluginName, 'latestVersion', newVersion) | ||
98 | } | ||
99 | |||
100 | setActorFollowScores (newScore: number) { | ||
101 | const seq = this.getSequelize() | ||
102 | |||
103 | const options = { type: QueryTypes.UPDATE } | ||
104 | |||
105 | return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options) | ||
106 | } | ||
107 | |||
108 | setTokenField (accessToken: string, field: string, value: string) { | ||
109 | const seq = this.getSequelize() | ||
110 | |||
111 | const options = { type: QueryTypes.UPDATE } | ||
112 | |||
113 | return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options) | ||
114 | } | ||
115 | |||
116 | async cleanup () { | ||
117 | if (!this.sequelize) return | ||
118 | |||
119 | await this.sequelize.close() | ||
120 | this.sequelize = undefined | ||
121 | } | ||
122 | |||
123 | private getSequelize () { | ||
124 | if (this.sequelize) return this.sequelize | ||
125 | |||
126 | const dbname = 'peertube_test' + this.server.internalServerNumber | ||
127 | const username = 'peertube' | ||
128 | const password = 'peertube' | ||
129 | const host = 'localhost' | ||
130 | const port = 5432 | ||
131 | |||
132 | this.sequelize = new Sequelize(dbname, username, password, { | ||
133 | dialect: 'postgres', | ||
134 | host, | ||
135 | port, | ||
136 | logging: false | ||
137 | }) | ||
138 | |||
139 | return this.sequelize | ||
140 | } | ||
141 | |||
142 | } | ||