diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
commit | 99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch) | |
tree | c9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/models/author.js | |
parent | fcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff) | |
parent | 91cc839af88730ba55f84997c56b85ea100070a7 (diff) | |
download | PeerTube-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.js | 85 |
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 | |||
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 | findOrCreateAuthor | ||
35 | } | ||
36 | } | ||
37 | ) | ||
38 | |||
39 | return Author | ||
40 | } | ||
41 | |||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | function 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 | |||
62 | function 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 | } | ||