2019-04-18 11:28:17 +02:00
import { QueryTypes , Sequelize } from 'sequelize'
2018-10-23 11:38:48 +02:00
2019-04-18 11:28:17 +02:00
let sequelizes : { [ id : number ] : Sequelize } = { }
2019-02-21 15:44:12 +01:00
2018-10-23 11:38:48 +02:00
function getSequelize ( serverNumber : number ) {
2019-02-21 15:44:12 +01:00
if ( sequelizes [ serverNumber ] ) return sequelizes [ serverNumber ]
2018-10-23 11:38:48 +02:00
const dbname = 'peertube_test' + serverNumber
const username = 'peertube'
const password = 'peertube'
const host = 'localhost'
const port = 5432
2019-02-21 15:44:12 +01:00
const seq = new Sequelize ( dbname , username , password , {
2018-10-23 11:38:48 +02:00
dialect : 'postgres' ,
host ,
port ,
logging : false
} )
2019-02-21 15:44:12 +01:00
sequelizes [ serverNumber ] = seq
return seq
2018-10-23 11:38:48 +02:00
}
function setActorField ( serverNumber : number , to : string , field : string , value : string ) {
const seq = getSequelize ( serverNumber )
2019-04-18 11:28:17 +02:00
const options = { type : QueryTypes . UPDATE }
2018-10-23 11:38:48 +02:00
return seq . query ( ` UPDATE actor SET " ${ field } " = ' ${ value } ' WHERE url = ' ${ to } ' ` , options )
}
2018-11-14 16:32:12 +01:00
function setVideoField ( serverNumber : number , uuid : string , field : string , value : string ) {
const seq = getSequelize ( serverNumber )
2019-04-18 11:28:17 +02:00
const options = { type : QueryTypes . UPDATE }
2018-11-14 16:32:12 +01:00
return seq . query ( ` UPDATE video SET " ${ field } " = ' ${ value } ' WHERE uuid = ' ${ uuid } ' ` , options )
}
2019-03-19 14:13:53 +01:00
function setPlaylistField ( serverNumber : number , uuid : string , field : string , value : string ) {
const seq = getSequelize ( serverNumber )
2019-04-18 11:28:17 +02:00
const options = { type : QueryTypes . UPDATE }
2019-03-19 14:13:53 +01:00
return seq . query ( ` UPDATE "videoPlaylist" SET " ${ field } " = ' ${ value } ' WHERE uuid = ' ${ uuid } ' ` , options )
}
2019-04-11 17:33:36 +02:00
async function countVideoViewsOf ( serverNumber : number , uuid : string ) {
const seq = getSequelize ( serverNumber )
// tslint:disable
const query = ` SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = ' ${ uuid } ' `
2019-04-18 11:28:17 +02:00
const options = { type : QueryTypes . SELECT as QueryTypes . SELECT }
const [ { total } ] = await seq . query < { total : number } > ( query , options )
2019-04-11 17:33:36 +02:00
if ( ! total ) return 0
2019-04-18 11:28:17 +02:00
// FIXME: check if we really need parseInt
return parseInt ( total + '' , 10 )
2019-04-11 17:33:36 +02:00
}
2019-02-21 15:44:12 +01:00
async function closeAllSequelize ( servers : any [ ] ) {
for ( let i = 1 ; i <= servers . length ; i ++ ) {
if ( sequelizes [ i ] ) {
await sequelizes [ i ] . close ( )
delete sequelizes [ i ]
}
}
}
2018-10-23 11:38:48 +02:00
export {
2018-11-14 16:32:12 +01:00
setVideoField ,
2019-03-19 14:13:53 +01:00
setPlaylistField ,
2019-02-21 15:44:12 +01:00
setActorField ,
2019-04-11 17:33:36 +02:00
countVideoViewsOf ,
2019-02-21 15:44:12 +01:00
closeAllSequelize
2018-10-23 11:38:48 +02:00
}