diff options
Diffstat (limited to 'server/models/author.ts')
-rw-r--r-- | server/models/author.ts | 36 |
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { isUserUsernameValid } from '../helpers' | 3 | import { isUserUsernameValid } from '../helpers' |
2 | 4 | ||
3 | module.exports = function (sequelize, DataTypes) { | 5 | import { addMethodsToModel } from './utils' |
4 | const Author = sequelize.define('Author', | 6 | import { |
7 | AuthorClass, | ||
8 | AuthorInstance, | ||
9 | AuthorAttributes, | ||
10 | |||
11 | AuthorMethods | ||
12 | } from './author-interface' | ||
13 | |||
14 | let Author: Sequelize.Model<AuthorInstance, AuthorAttributes> | ||
15 | let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor | ||
16 | |||
17 | export 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 | ||
47 | function associate (models) { | 59 | function 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 | ||
65 | function findOrCreateAuthor (name, podId, userId, transaction, callback) { | 77 | findOrCreateAuthor = 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 ] |