import { addMethodsToModel } from '../utils'
import {
- AuthorClass,
AuthorInstance,
AuthorAttributes,
type: DataTypes.STRING,
allowNull: false,
validate: {
- usernameValid: function (value) {
+ usernameValid: value => {
const res = isUserUsernameValid(value)
if (res === false) throw new Error('Username is not valid.')
}
},
onDelete: 'cascade'
})
+
+ Author.hasMany(models.Video, {
+ foreignKey: {
+ name: 'authorId',
+ allowNull: false
+ },
+ onDelete: 'cascade'
+ })
}
-findOrCreateAuthor = function (
- name: string,
- podId: number,
- userId: number,
- transaction: Sequelize.Transaction,
- callback: AuthorMethods.FindOrCreateAuthorCallback
-) {
+findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) {
const author = {
name,
podId,
userId
}
- const query: any = {
+ const query: Sequelize.FindOrInitializeOptions<AuthorAttributes> = {
where: author,
- defaults: author
+ defaults: author,
+ transaction
}
- if (transaction !== null) query.transaction = transaction
-
- Author.findOrCreate(query).asCallback(function (err, result) {
- if (err) return callback(err)
-
- // [ instance, wasCreated ]
- return callback(null, result[0])
- })
+ return Author.findOrCreate(query).then(([ authorInstance ]) => authorInstance)
}