Reorganize model files

This commit is contained in:
Chocobozzz 2017-06-16 09:45:46 +02:00
parent 15a302943d
commit 74889a71fe
43 changed files with 128 additions and 83 deletions

View File

@ -1,6 +1,7 @@
import * as fs from 'fs' import * as fs from 'fs'
import { join } from 'path' import { join } from 'path'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { each } from 'async'
import { CONFIG } from './constants' import { CONFIG } from './constants'
// Do not use barrel, we need to load database first // Do not use barrel, we need to load database first
@ -72,24 +73,13 @@ const sequelize = new Sequelize(dbname, username, password, {
database.sequelize = sequelize database.sequelize = sequelize
database.init = function (silent: boolean, callback: (err: Error) => void) { database.init = function (silent: boolean, callback: (err: Error) => void) {
const modelDirectory = join(__dirname, '..', 'models') const modelDirectory = join(__dirname, '..', 'models')
fs.readdir(modelDirectory, function (err, files) {
getModelFiles(modelDirectory, function (err, filePaths) {
if (err) throw err if (err) throw err
files.filter(function (file) { filePaths.forEach(function (filePath) {
// For all models but not utils.js const model = sequelize.import(filePath)
if (
file === 'index.js' || file === 'index.ts' ||
file === 'utils.js' || file === 'utils.ts' ||
file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
file.endsWith('.js.map')
) return false
return true
})
.forEach(function (file) {
const model = sequelize.import(join(modelDirectory, file))
database[model['name']] = model database[model['name']] = model
}) })
@ -111,3 +101,51 @@ database.init = function (silent: boolean, callback: (err: Error) => void) {
export { export {
database database
} }
// ---------------------------------------------------------------------------
function getModelFiles (modelDirectory: string, callback: (err: Error, filePaths: string[]) => void) {
fs.readdir(modelDirectory, function (err, files) {
if (err) throw err
const directories = files.filter(function (directory) {
// For all models but not utils.js
if (
directory === 'index.js' || directory === 'index.ts' ||
directory === 'utils.js' || directory === 'utils.ts'
) return false
return true
})
let modelFilePaths: string[] = []
// For each directory we read it and append model in the modelFilePaths array
each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) {
const modelDirectoryPath = join(modelDirectory, directory)
fs.readdir(modelDirectoryPath, function (err, files) {
if (err) return eachCallback(err)
const filteredFiles = files.filter(file => {
if (
file === 'index.js' || file === 'index.ts' ||
file === 'utils.js' || file === 'utils.ts' ||
file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
file.endsWith('.js.map')
) return false
return true
}).map(file => {
return join(modelDirectoryPath, file)
})
modelFilePaths = modelFilePaths.concat(filteredFiles)
return eachCallback(null)
})
}, function(err: Error) {
return callback(err, modelFilePaths)
})
})
}

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
ApplicationClass, ApplicationClass,
ApplicationAttributes, ApplicationAttributes,

View File

@ -0,0 +1 @@
export * from './application-interface'

View File

@ -1,17 +1,7 @@
export * from './application-interface' export * from './application'
export * from './author-interface' export * from './job'
export * from './job-interface' export * from './oauth'
export * from './oauth-client-interface' export * from './pod'
export * from './oauth-token-interface' export * from './request'
export * from './pod-interface' export * from './user'
export * from './request-interface' export * from './video'
export * from './request-to-pod-interface'
export * from './request-video-event-interface'
export * from './request-video-qadu-interface'
export * from './tag-interface'
export * from './user-video-rate-interface'
export * from './user-interface'
export * from './video-abuse-interface'
export * from './video-blacklist-interface'
export * from './video-tag-interface'
export * from './video-interface'

View File

@ -0,0 +1 @@
export * from './job-interface'

View File

@ -1,9 +1,9 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { JOB_STATES } from '../initializers' import { JOB_STATES } from '../../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
JobClass, JobClass,
JobInstance, JobInstance,

View File

@ -0,0 +1,2 @@
export * from './oauth-client-interface'
export * from './oauth-token-interface'

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
OAuthClientClass, OAuthClientClass,
OAuthClientInstance, OAuthClientInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Bluebird from 'bluebird' import * as Bluebird from 'bluebird'
import { UserModel } from './user-interface' import { UserModel } from '../user'
export type OAuthTokenInfo = { export type OAuthTokenInfo = {
refreshToken: string refreshToken: string

View File

@ -1,8 +1,8 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { logger } from '../helpers' import { logger } from '../../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
OAuthTokenClass, OAuthTokenClass,
OAuthTokenInstance, OAuthTokenInstance,

View File

@ -0,0 +1 @@
export * from './pod-interface'

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { Pod as FormatedPod } from '../../shared/models/pod.model' import { Pod as FormatedPod } from '../../../shared/models/pod.model'
export namespace PodMethods { export namespace PodMethods {
export type ToFormatedJSON = () => FormatedPod export type ToFormatedJSON = () => FormatedPod

View File

@ -2,10 +2,10 @@ import { each, waterfall } from 'async'
import { map } from 'lodash' import { map } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { FRIEND_SCORE, PODS_SCORE } from '../initializers' import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
import { logger, isHostValid } from '../helpers' import { logger, isHostValid } from '../../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
PodClass, PodClass,
PodInstance, PodInstance,

View File

@ -0,0 +1,4 @@
export * from './request-interface'
export * from './request-to-pod-interface'
export * from './request-video-event-interface'
export * from './request-video-qadu-interface'

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { PodInstance, PodAttributes } from './pod-interface' import { PodInstance, PodAttributes } from '../pod'
export type RequestsGrouped = { export type RequestsGrouped = {
[ podId: number ]: { [ podId: number ]: {

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
RequestToPodClass, RequestToPodClass,
RequestToPodInstance, RequestToPodInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VideoInstance } from './video-interface' import { VideoInstance } from '../video'
import { PodInstance } from './pod-interface' import { PodInstance } from '../pod'
export type RequestsVideoEventGrouped = { export type RequestsVideoEventGrouped = {
[ podId: number ]: { [ podId: number ]: {

View File

@ -5,10 +5,10 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../../initializers/database'
import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
import { isVideoEventCountValid } from '../helpers' import { isVideoEventCountValid } from '../../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
RequestVideoEventClass, RequestVideoEventClass,
RequestVideoEventInstance, RequestVideoEventInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VideoInstance } from './video-interface' import { VideoInstance } from '../video'
import { PodInstance } from './pod-interface' import { PodInstance } from '../pod'
export type RequestsVideoQaduGrouped = { export type RequestsVideoQaduGrouped = {
[ podId: number ]: { [ podId: number ]: {

View File

@ -12,9 +12,9 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../../initializers/database'
import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
RequestVideoQaduClass, RequestVideoQaduClass,
RequestVideoQaduInstance, RequestVideoQaduInstance,

View File

@ -1,9 +1,9 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../../initializers/database'
import { REQUEST_ENDPOINTS } from '../initializers' import { REQUEST_ENDPOINTS } from '../../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
RequestClass, RequestClass,
RequestInstance, RequestInstance,

View File

@ -0,0 +1,2 @@
export * from './user-video-rate-interface'
export * from './user-interface'

View File

@ -2,7 +2,7 @@ import * as Sequelize from 'sequelize'
import * as Bluebird from 'bluebird' import * as Bluebird from 'bluebird'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { User as FormatedUser } from '../../shared/models/user.model' import { User as FormatedUser } from '../../../shared/models/user.model'
export namespace UserMethods { export namespace UserMethods {
export type IsPasswordMatchCallback = (err: Error, same: boolean) => void export type IsPasswordMatchCallback = (err: Error, same: boolean) => void

View File

@ -5,9 +5,9 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VIDEO_RATE_TYPES } from '../initializers' import { VIDEO_RATE_TYPES } from '../../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
UserVideoRateClass, UserVideoRateClass,
UserVideoRateInstance, UserVideoRateInstance,

View File

@ -1,17 +1,17 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { getSort } from './utils' import { getSort } from '../utils'
import { USER_ROLES } from '../initializers' import { USER_ROLES } from '../../initializers'
import { import {
cryptPassword, cryptPassword,
comparePassword, comparePassword,
isUserPasswordValid, isUserPasswordValid,
isUserUsernameValid, isUserUsernameValid,
isUserDisplayNSFWValid isUserDisplayNSFWValid
} from '../helpers' } from '../../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
UserClass, UserClass,
UserInstance, UserInstance,

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { PodInstance } from './pod-interface' import { PodInstance } from '../pod'
export namespace AuthorMethods { export namespace AuthorMethods {
export type FindOrCreateAuthorCallback = (err: Error, authorInstance?: AuthorInstance) => void export type FindOrCreateAuthorCallback = (err: Error, authorInstance?: AuthorInstance) => void

View File

@ -1,8 +1,8 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { isUserUsernameValid } from '../helpers' import { isUserUsernameValid } from '../../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
AuthorClass, AuthorClass,
AuthorInstance, AuthorInstance,

View File

@ -0,0 +1,6 @@
export * from './author-interface'
export * from './tag-interface'
export * from './video-abuse-interface'
export * from './video-blacklist-interface'
export * from './video-tag-interface'
export * from './video-interface'

View File

@ -1,7 +1,7 @@
import { each } from 'async' import { each } from 'async'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
TagClass, TagClass,
TagInstance, TagInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { VideoAbuse as FormatedVideoAbuse } from '../../shared/models/video-abuse.model' import { VideoAbuse as FormatedVideoAbuse } from '../../../shared/models/video-abuse.model'
export namespace VideoAbuseMethods { export namespace VideoAbuseMethods {
export type toFormatedJSON = () => FormatedVideoAbuse export type toFormatedJSON = () => FormatedVideoAbuse

View File

@ -1,9 +1,9 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { CONFIG } from '../initializers' import { CONFIG } from '../../initializers'
import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../helpers' import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers'
import { addMethodsToModel, getSort } from './utils' import { addMethodsToModel, getSort } from '../utils'
import { import {
VideoAbuseClass, VideoAbuseClass,
VideoAbuseInstance, VideoAbuseInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../shared/models/video-blacklist.model' import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../../shared/models/video-blacklist.model'
export namespace BlacklistedVideoMethods { export namespace BlacklistedVideoMethods {
export type ToFormatedJSON = () => FormatedBlacklistedVideo export type ToFormatedJSON = () => FormatedBlacklistedVideo

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel, getSort } from './utils' import { addMethodsToModel, getSort } from '../utils'
import { import {
BlacklistedVideoClass, BlacklistedVideoClass,
BlacklistedVideoInstance, BlacklistedVideoInstance,

View File

@ -4,7 +4,7 @@ import { AuthorInstance } from './author-interface'
import { VideoTagInstance } from './video-tag-interface' import { VideoTagInstance } from './video-tag-interface'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { Video as FormatedVideo } from '../../shared/models/video.model' import { Video as FormatedVideo } from '../../../shared/models/video.model'
export type FormatedAddRemoteVideo = { export type FormatedAddRemoteVideo = {
name: string name: string

View File

@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from '../utils'
import { import {
VideoTagClass, VideoTagClass,
VideoTagInstance, VideoTagInstance,

View File

@ -10,7 +10,7 @@ import * as parseTorrent from 'parse-torrent'
import { join } from 'path' import { join } from 'path'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../../initializers/database'
import { VideoTagInstance } from './video-tag-interface' import { VideoTagInstance } from './video-tag-interface'
import { import {
logger, logger,
@ -22,7 +22,7 @@ import {
isVideoDescriptionValid, isVideoDescriptionValid,
isVideoInfoHashValid, isVideoInfoHashValid,
isVideoDurationValid isVideoDurationValid
} from '../helpers' } from '../../helpers'
import { import {
CONSTRAINTS_FIELDS, CONSTRAINTS_FIELDS,
CONFIG, CONFIG,
@ -32,10 +32,10 @@ import {
VIDEO_LICENCES, VIDEO_LICENCES,
VIDEO_LANGUAGES, VIDEO_LANGUAGES,
THUMBNAILS_SIZE THUMBNAILS_SIZE
} from '../initializers' } from '../../initializers'
import { JobScheduler, removeVideoToFriends } from '../lib' import { JobScheduler, removeVideoToFriends } from '../../lib'
import { addMethodsToModel, getSort } from './utils' import { addMethodsToModel, getSort } from '../utils'
import { import {
VideoClass, VideoClass,
VideoInstance, VideoInstance,