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 { join } from 'path'
import * as Sequelize from 'sequelize'
import { each } from 'async'
import { CONFIG } from './constants'
// 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.init = function (silent: boolean, callback: (err: Error) => void) {
const modelDirectory = join(__dirname, '..', 'models')
fs.readdir(modelDirectory, function (err, files) {
getModelFiles(modelDirectory, function (err, filePaths) {
if (err) throw err
files.filter(function (file) {
// For all models but not utils.js
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))
filePaths.forEach(function (filePath) {
const model = sequelize.import(filePath)
database[model['name']] = model
})
@ -111,3 +101,51 @@ database.init = function (silent: boolean, callback: (err: Error) => void) {
export {
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 { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
ApplicationClass,
ApplicationAttributes,

View File

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

View File

@ -1,17 +1,7 @@
export * from './application-interface'
export * from './author-interface'
export * from './job-interface'
export * from './oauth-client-interface'
export * from './oauth-token-interface'
export * from './pod-interface'
export * from './request-interface'
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'
export * from './application'
export * from './job'
export * from './oauth'
export * from './pod'
export * from './request'
export * from './user'
export * from './video'

View File

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

View File

@ -1,9 +1,9 @@
import { values } from 'lodash'
import * as Sequelize from 'sequelize'
import { JOB_STATES } from '../initializers'
import { JOB_STATES } from '../../initializers'
import { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
JobClass,
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 { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
OAuthClientClass,
OAuthClientInstance,

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize'
// 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 type ToFormatedJSON = () => FormatedPod

View File

@ -2,10 +2,10 @@ import { each, waterfall } from 'async'
import { map } from 'lodash'
import * as Sequelize from 'sequelize'
import { FRIEND_SCORE, PODS_SCORE } from '../initializers'
import { logger, isHostValid } from '../helpers'
import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
import { logger, isHostValid } from '../../helpers'
import { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
PodClass,
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 { PodInstance, PodAttributes } from './pod-interface'
import { PodInstance, PodAttributes } from '../pod'
export type RequestsGrouped = {
[ podId: number ]: {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,9 @@
import { values } from 'lodash'
import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database'
import { REQUEST_ENDPOINTS } from '../initializers'
import { addMethodsToModel } from './utils'
import { database as db } from '../../initializers/database'
import { REQUEST_ENDPOINTS } from '../../initializers'
import { addMethodsToModel } from '../utils'
import {
RequestClass,
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'
// 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 type IsPasswordMatchCallback = (err: Error, same: boolean) => void

View File

@ -5,9 +5,9 @@
import { values } from 'lodash'
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 {
UserVideoRateClass,
UserVideoRateInstance,

View File

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

View File

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

View File

@ -1,8 +1,8 @@
import * as Sequelize from 'sequelize'
import { isUserUsernameValid } from '../helpers'
import { isUserUsernameValid } from '../../helpers'
import { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
AuthorClass,
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 * as Sequelize from 'sequelize'
import { addMethodsToModel } from './utils'
import { addMethodsToModel } from '../utils'
import {
TagClass,
TagInstance,

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize'
// 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 type toFormatedJSON = () => FormatedVideoAbuse

View File

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

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize'
// 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 type ToFormatedJSON = () => FormatedBlacklistedVideo

View File

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

View File

@ -4,7 +4,7 @@ import { AuthorInstance } from './author-interface'
import { VideoTagInstance } from './video-tag-interface'
// 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 = {
name: string

View File

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

View File

@ -10,7 +10,7 @@ import * as parseTorrent from 'parse-torrent'
import { join } from 'path'
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 {
logger,
@ -22,7 +22,7 @@ import {
isVideoDescriptionValid,
isVideoInfoHashValid,
isVideoDurationValid
} from '../helpers'
} from '../../helpers'
import {
CONSTRAINTS_FIELDS,
CONFIG,
@ -32,10 +32,10 @@ import {
VIDEO_LICENCES,
VIDEO_LANGUAGES,
THUMBNAILS_SIZE
} from '../initializers'
import { JobScheduler, removeVideoToFriends } from '../lib'
} from '../../initializers'
import { JobScheduler, removeVideoToFriends } from '../../lib'
import { addMethodsToModel, getSort } from './utils'
import { addMethodsToModel, getSort } from '../utils'
import {
VideoClass,
VideoInstance,