aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/models/video/author.ts
blob: 3222c483494f82774b82128a41b5dd578ddc64d9 (plain) (tree)
1
2
3
4
5
6

                                      
                                                   
 
                                            
        








                                                             
                                                                                                       
                                                                       

             



                                           
                                                  


                                                                        


       





                             

          





                                      
         
       


     


                                                        





                                                                              
                                





                       
 
                                 





                       
 
 
                                                                                                                 





                  
                                                                      
                  

                     

   
                                                                                
 
import * as Sequelize from 'sequelize'

import { isUserUsernameValid } from '../../helpers'

import { addMethodsToModel } from '../utils'
import {
  AuthorInstance,
  AuthorAttributes,

  AuthorMethods
} from './author-interface'

let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor

export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          usernameValid: function (value) {
            const res = isUserUsernameValid(value)
            if (res === false) throw new Error('Username is not valid.')
          }
        }
      }
    },
    {
      indexes: [
        {
          fields: [ 'name' ]
        },
        {
          fields: [ 'podId' ]
        },
        {
          fields: [ 'userId' ],
          unique: true
        },
        {
          fields: [ 'name', 'podId' ],
          unique: true
        }
      ]
    }
  )

  const classMethods = [ associate, findOrCreateAuthor ]
  addMethodsToModel(Author, classMethods)

  return Author
}

// ---------------------------------------------------------------------------

function associate (models) {
  Author.belongsTo(models.Pod, {
    foreignKey: {
      name: 'podId',
      allowNull: true
    },
    onDelete: 'cascade'
  })

  Author.belongsTo(models.User, {
    foreignKey: {
      name: 'userId',
      allowNull: true
    },
    onDelete: 'cascade'
  })
}

findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) {
  const author = {
    name,
    podId,
    userId
  }

  const query: Sequelize.FindOrInitializeOptions<AuthorAttributes> = {
    where: author,
    defaults: author,
    transaction
  }

  return Author.findOrCreate(query).then(([ authorInstance ]) => authorInstance)
}