}
},
{
+ indexes: [
+ {
+ fields: [ 'name' ]
+ },
+ {
+ fields: [ 'podId' ]
+ },
+ {
+ fields: [ 'userId' ]
+ }
+ ],
classMethods: {
- associate
+ associate,
+
+ findOrCreateAuthor
}
}
)
},
onDelete: 'cascade'
})
+
+ this.belongsTo(models.User, {
+ foreignKey: {
+ name: 'userId',
+ allowNull: true
+ },
+ onDelete: 'cascade'
+ })
+}
+
+function findOrCreateAuthor (name, podId, userId, transaction, callback) {
+ if (!callback) {
+ callback = transaction
+ transaction = null
+ }
+
+ const author = {
+ name,
+ podId,
+ userId
+ }
+
+ const query = {
+ where: author,
+ defaults: author
+ }
+
+ if (transaction) query.transaction = transaction
+
+ this.findOrCreate(query).asCallback(function (err, result) {
+ // [ instance, wasCreated ]
+ return callback(err, result[0])
+ })
}