]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/author.js
Server: add association between author and user
[github/Chocobozzz/PeerTube.git] / server / models / author.js
1 'use strict'
2
3 const customUsersValidators = require('../helpers/custom-validators').users
4
5 module.exports = function (sequelize, DataTypes) {
6 const Author = sequelize.define('Author',
7 {
8 name: {
9 type: DataTypes.STRING,
10 allowNull: false,
11 validate: {
12 usernameValid: function (value) {
13 const res = customUsersValidators.isUserUsernameValid(value)
14 if (res === false) throw new Error('Username is not valid.')
15 }
16 }
17 }
18 },
19 {
20 indexes: [
21 {
22 fields: [ 'name' ]
23 },
24 {
25 fields: [ 'podId' ]
26 },
27 {
28 fields: [ 'userId' ]
29 }
30 ],
31 classMethods: {
32 associate
33 }
34 }
35 )
36
37 return Author
38 }
39
40 // ---------------------------------------------------------------------------
41
42 function associate (models) {
43 this.belongsTo(models.Pod, {
44 foreignKey: {
45 name: 'podId',
46 allowNull: true
47 },
48 onDelete: 'cascade'
49 })
50
51 this.belongsTo(models.User, {
52 foreignKey: {
53 name: 'userId',
54 allowNull: true
55 },
56 onDelete: 'cascade'
57 })
58 }