PeerTube/server/models/video/video-channel-share.ts

86 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-11-15 10:56:21 -06:00
import * as Sequelize from 'sequelize'
import { addMethodsToModel } from '../utils'
2017-11-16 08:55:01 -06:00
import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface'
2017-11-15 10:56:21 -06:00
let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes>
2017-11-16 08:55:01 -06:00
let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare
let load: VideoChannelShareMethods.Load
2017-11-15 10:56:21 -06:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare',
{ },
{
indexes: [
{
fields: [ 'accountId' ]
},
{
fields: [ 'videoChannelId' ]
}
]
}
)
const classMethods = [
2017-11-16 08:55:01 -06:00
associate,
load,
2017-11-16 08:55:01 -06:00
loadAccountsByShare
2017-11-15 10:56:21 -06:00
]
addMethodsToModel(VideoChannelShare, classMethods)
return VideoChannelShare
}
// ------------------------------ METHODS ------------------------------
function associate (models) {
VideoChannelShare.belongsTo(models.Account, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade'
})
VideoChannelShare.belongsTo(models.VideoChannel, {
foreignKey: {
name: 'videoChannelId',
allowNull: true
},
onDelete: 'cascade'
})
}
2017-11-16 08:55:01 -06:00
load = function (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
return VideoChannelShare.findOne({
where: {
accountId,
videoChannelId
},
include: [
VideoChannelShare['sequelize'].models.Account,
VideoChannelShare['sequelize'].models.VideoChannel
],
transaction: t
})
}
loadAccountsByShare = function (videoChannelId: number, t: Sequelize.Transaction) {
2017-11-16 08:55:01 -06:00
const query = {
where: {
videoChannelId
},
include: [
{
model: VideoChannelShare['sequelize'].models.Account,
required: true
}
],
transaction: t
2017-11-16 08:55:01 -06:00
}
return VideoChannelShare.findAll(query)
.then(res => res.map(r => r.Account))
}