diff options
Diffstat (limited to 'server/models/author.js')
-rw-r--r-- | server/models/author.js | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/server/models/author.js b/server/models/author.js index 5835ada99..7d15fb6ec 100644 --- a/server/models/author.js +++ b/server/models/author.js | |||
@@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) { | |||
29 | } | 29 | } |
30 | ], | 30 | ], |
31 | classMethods: { | 31 | classMethods: { |
32 | associate | 32 | associate, |
33 | |||
34 | findOrCreateAuthor | ||
33 | } | 35 | } |
34 | } | 36 | } |
35 | ) | 37 | ) |
@@ -56,3 +58,28 @@ function associate (models) { | |||
56 | onDelete: 'cascade' | 58 | onDelete: 'cascade' |
57 | }) | 59 | }) |
58 | } | 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 | } | ||