2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-06-16 03:45:46 -04:00
|
|
|
import { isUserUsernameValid } from '../../helpers'
|
2016-12-28 09:49:23 -05:00
|
|
|
|
2017-06-16 03:45:46 -04:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 14:58:25 -04:00
|
|
|
import {
|
|
|
|
AuthorInstance,
|
|
|
|
AuthorAttributes,
|
|
|
|
|
|
|
|
AuthorMethods
|
|
|
|
} from './author-interface'
|
|
|
|
|
|
|
|
let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
|
|
|
|
let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor
|
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
2017-05-22 14:58:25 -04:00
|
|
|
Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
|
2016-12-11 15:50:51 -05:00
|
|
|
{
|
|
|
|
name: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
usernameValid: function (value) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const res = isUserUsernameValid(value)
|
2016-12-28 09:49:23 -05:00
|
|
|
if (res === false) throw new Error('Username is not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 03:33:28 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'name' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'podId' ]
|
2016-12-29 04:33:36 -05:00
|
|
|
},
|
|
|
|
{
|
2017-02-16 13:24:34 -05:00
|
|
|
fields: [ 'userId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'name', 'podId' ],
|
|
|
|
unique: true
|
2016-12-29 03:33:28 -05:00
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [ associate, findOrCreateAuthor ]
|
|
|
|
addMethodsToModel(Author, classMethods)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return Author
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
2017-05-22 14:58:25 -04:00
|
|
|
Author.belongsTo(models.Pod, {
|
2016-12-11 15:50:51 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'podId',
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-29 04:33:36 -05:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
Author.belongsTo(models.User, {
|
2016-12-29 04:33:36 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'userId',
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
2016-12-29 12:02:03 -05:00
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) {
|
2016-12-29 12:02:03 -05:00
|
|
|
const author = {
|
|
|
|
name,
|
|
|
|
podId,
|
|
|
|
userId
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
const query: Sequelize.FindOrInitializeOptions<AuthorAttributes> = {
|
2016-12-29 12:02:03 -05:00
|
|
|
where: author,
|
2017-07-05 07:26:25 -04:00
|
|
|
defaults: author,
|
|
|
|
transaction
|
2016-12-29 12:02:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
return Author.findOrCreate(query).then(([ authorInstance ]) => authorInstance)
|
2016-12-29 12:02:03 -05:00
|
|
|
}
|