aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/author.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/models/author.js
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'server/models/author.js')
-rw-r--r--server/models/author.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/server/models/author.js b/server/models/author.js
new file mode 100644
index 000000000..7d15fb6ec
--- /dev/null
+++ b/server/models/author.js
@@ -0,0 +1,85 @@
1'use strict'
2
3const customUsersValidators = require('../helpers/custom-validators').users
4
5module.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 findOrCreateAuthor
35 }
36 }
37 )
38
39 return Author
40}
41
42// ---------------------------------------------------------------------------
43
44function associate (models) {
45 this.belongsTo(models.Pod, {
46 foreignKey: {
47 name: 'podId',
48 allowNull: true
49 },
50 onDelete: 'cascade'
51 })
52
53 this.belongsTo(models.User, {
54 foreignKey: {
55 name: 'userId',
56 allowNull: true
57 },
58 onDelete: 'cascade'
59 })
60}
61
62function findOrCreateAuthor (name, podId, userId, transaction, callback) {
63 if (!callback) {
64 callback = transaction
65 transaction = null
66 }
67
68 const author = {
69 name,
70 podId,
71 userId
72 }
73
74 const query = {
75 where: author,
76 defaults: author
77 }
78
79 if (transaction) query.transaction = transaction
80
81 this.findOrCreate(query).asCallback(function (err, result) {
82 // [ instance, wasCreated ]
83 return callback(err, result[0])
84 })
85}