aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/miscs/sql.ts
blob: 1ce3d801afb4b39f48a2698e3aa129a667d7b540 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as Sequelize from 'sequelize'

let sequelizes: { [ id: number ]: Sequelize.Sequelize } = {}

function getSequelize (serverNumber: number) {
  if (sequelizes[serverNumber]) return sequelizes[serverNumber]

  const dbname = 'peertube_test' + serverNumber
  const username = 'peertube'
  const password = 'peertube'
  const host = 'localhost'
  const port = 5432

  const seq = new Sequelize(dbname, username, password, {
    dialect: 'postgres',
    host,
    port,
    operatorsAliases: false,
    logging: false
  })

  sequelizes[serverNumber] = seq

  return seq
}

function setActorField (serverNumber: number, to: string, field: string, value: string) {
  const seq = getSequelize(serverNumber)

  const options = { type: Sequelize.QueryTypes.UPDATE }

  return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
}

function setVideoField (serverNumber: number, uuid: string, field: string, value: string) {
  const seq = getSequelize(serverNumber)

  const options = { type: Sequelize.QueryTypes.UPDATE }

  return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
}

function setPlaylistField (serverNumber: number, uuid: string, field: string, value: string) {
  const seq = getSequelize(serverNumber)

  const options = { type: Sequelize.QueryTypes.UPDATE }

  return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
}

async function closeAllSequelize (servers: any[]) {
  for (let i = 1; i <= servers.length; i++) {
    if (sequelizes[ i ]) {
      await sequelizes[ i ].close()
      delete sequelizes[ i ]
    }
  }
}

export {
  setVideoField,
  setPlaylistField,
  setActorField,
  closeAllSequelize
}