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

69 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-11-15 17:56:21 +01:00
import * as Sequelize from 'sequelize'
import { addMethodsToModel } from '../utils'
2017-11-16 15:55:01 +01:00
import { VideoShareAttributes, VideoShareInstance, VideoShareMethods } from './video-share-interface'
2017-11-15 17:56:21 +01:00
let VideoShare: Sequelize.Model<VideoShareInstance, VideoShareAttributes>
2017-11-16 15:55:01 +01:00
let loadAccountsByShare: VideoShareMethods.LoadAccountsByShare
2017-11-15 17:56:21 +01:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoShare = sequelize.define<VideoShareInstance, VideoShareAttributes>('VideoShare',
{ },
{
indexes: [
{
fields: [ 'accountId' ]
},
{
fields: [ 'videoId' ]
}
]
}
)
const classMethods = [
2017-11-16 15:55:01 +01:00
associate,
loadAccountsByShare
2017-11-15 17:56:21 +01:00
]
addMethodsToModel(VideoShare, classMethods)
return VideoShare
}
// ------------------------------ METHODS ------------------------------
function associate (models) {
VideoShare.belongsTo(models.Account, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade'
})
VideoShare.belongsTo(models.Video, {
foreignKey: {
name: 'videoId',
allowNull: true
},
onDelete: 'cascade'
})
}
2017-11-16 15:55:01 +01:00
loadAccountsByShare = function (videoId: number) {
const query = {
where: {
videoId
},
include: [
{
model: VideoShare['sequelize'].models.Account,
required: true
}
]
}
return VideoShare.findAll(query)
.then(res => res.map(r => r.Account))
}