1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-06 03:50:26 +02:00

Type models

This commit is contained in:
Chocobozzz 2017-05-22 20:58:25 +02:00
parent 65fcc3119c
commit e02643f32e
76 changed files with 1710 additions and 816 deletions

View file

@ -1,7 +1,21 @@
import * as Sequelize from 'sequelize'
import { isUserUsernameValid } from '../helpers'
module.exports = function (sequelize, DataTypes) {
const Author = sequelize.define('Author',
import { addMethodsToModel } from './utils'
import {
AuthorClass,
AuthorInstance,
AuthorAttributes,
AuthorMethods
} from './author-interface'
let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor
export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes) {
Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
{
name: {
type: DataTypes.STRING,
@ -30,22 +44,20 @@ module.exports = function (sequelize, DataTypes) {
fields: [ 'name', 'podId' ],
unique: true
}
],
classMethods: {
associate,
findOrCreateAuthor
}
]
}
)
const classMethods = [ associate, findOrCreateAuthor ]
addMethodsToModel(Author, classMethods)
return Author
}
// ---------------------------------------------------------------------------
function associate (models) {
this.belongsTo(models.Pod, {
Author.belongsTo(models.Pod, {
foreignKey: {
name: 'podId',
allowNull: true
@ -53,7 +65,7 @@ function associate (models) {
onDelete: 'cascade'
})
this.belongsTo(models.User, {
Author.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: true
@ -62,7 +74,7 @@ function associate (models) {
})
}
function findOrCreateAuthor (name, podId, userId, transaction, callback) {
findOrCreateAuthor = function (name, podId, userId, transaction, callback) {
if (!callback) {
callback = transaction
transaction = null
@ -81,7 +93,7 @@ function findOrCreateAuthor (name, podId, userId, transaction, callback) {
if (transaction) query.transaction = transaction
this.findOrCreate(query).asCallback(function (err, result) {
Author.findOrCreate(query).asCallback(function (err, result) {
if (err) return callback(err)
// [ instance, wasCreated ]