aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/author.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-22 20:58:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-25 17:32:16 +0200
commite02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch)
treeb7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/author.ts
parent65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff)
downloadPeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz
PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst
PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip
Type models
Diffstat (limited to 'server/models/author.ts')
-rw-r--r--server/models/author.ts36
1 files changed, 24 insertions, 12 deletions
diff --git a/server/models/author.ts b/server/models/author.ts
index 4a7396929..b543d17a0 100644
--- a/server/models/author.ts
+++ b/server/models/author.ts
@@ -1,7 +1,21 @@
1import * as Sequelize from 'sequelize'
2
1import { isUserUsernameValid } from '../helpers' 3import { isUserUsernameValid } from '../helpers'
2 4
3module.exports = function (sequelize, DataTypes) { 5import { addMethodsToModel } from './utils'
4 const Author = sequelize.define('Author', 6import {
7 AuthorClass,
8 AuthorInstance,
9 AuthorAttributes,
10
11 AuthorMethods
12} from './author-interface'
13
14let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
15let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor
16
17export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes) {
18 Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
5 { 19 {
6 name: { 20 name: {
7 type: DataTypes.STRING, 21 type: DataTypes.STRING,
@@ -30,22 +44,20 @@ module.exports = function (sequelize, DataTypes) {
30 fields: [ 'name', 'podId' ], 44 fields: [ 'name', 'podId' ],
31 unique: true 45 unique: true
32 } 46 }
33 ], 47 ]
34 classMethods: {
35 associate,
36
37 findOrCreateAuthor
38 }
39 } 48 }
40 ) 49 )
41 50
51 const classMethods = [ associate, findOrCreateAuthor ]
52 addMethodsToModel(Author, classMethods)
53
42 return Author 54 return Author
43} 55}
44 56
45// --------------------------------------------------------------------------- 57// ---------------------------------------------------------------------------
46 58
47function associate (models) { 59function associate (models) {
48 this.belongsTo(models.Pod, { 60 Author.belongsTo(models.Pod, {
49 foreignKey: { 61 foreignKey: {
50 name: 'podId', 62 name: 'podId',
51 allowNull: true 63 allowNull: true
@@ -53,7 +65,7 @@ function associate (models) {
53 onDelete: 'cascade' 65 onDelete: 'cascade'
54 }) 66 })
55 67
56 this.belongsTo(models.User, { 68 Author.belongsTo(models.User, {
57 foreignKey: { 69 foreignKey: {
58 name: 'userId', 70 name: 'userId',
59 allowNull: true 71 allowNull: true
@@ -62,7 +74,7 @@ function associate (models) {
62 }) 74 })
63} 75}
64 76
65function findOrCreateAuthor (name, podId, userId, transaction, callback) { 77findOrCreateAuthor = function (name, podId, userId, transaction, callback) {
66 if (!callback) { 78 if (!callback) {
67 callback = transaction 79 callback = transaction
68 transaction = null 80 transaction = null
@@ -81,7 +93,7 @@ function findOrCreateAuthor (name, podId, userId, transaction, callback) {
81 93
82 if (transaction) query.transaction = transaction 94 if (transaction) query.transaction = transaction
83 95
84 this.findOrCreate(query).asCallback(function (err, result) { 96 Author.findOrCreate(query).asCallback(function (err, result) {
85 if (err) return callback(err) 97 if (err) return callback(err)
86 98
87 // [ instance, wasCreated ] 99 // [ instance, wasCreated ]