1 import { join } from 'path'
2 import { flattenDepth } from 'lodash'
3 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
4 import * as Sequelize from 'sequelize'
5 import * as Promise from 'bluebird'
7 import { CONFIG } from './constants'
8 // Do not use barrel, we need to load database first
9 import { logger } from '../helpers/logger'
10 import { isTestInstance, readdirPromise } from '../helpers/core-utils'
12 import { VideoModel } from './../models/video/video-interface'
13 import { VideoTagModel } from './../models/video/video-tag-interface'
14 import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
15 import { VideoFileModel } from './../models/video/video-file-interface'
16 import { VideoAbuseModel } from './../models/video/video-abuse-interface'
17 import { UserModel } from './../models/user/user-interface'
18 import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
19 import { TagModel } from './../models/video/tag-interface'
20 import { RequestModel } from './../models/request/request-interface'
21 import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
22 import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
23 import { RequestToPodModel } from './../models/request/request-to-pod-interface'
24 import { PodModel } from './../models/pod/pod-interface'
25 import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
26 import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
27 import { JobModel } from './../models/job/job-interface'
28 import { AuthorModel } from './../models/video/author-interface'
29 import { ApplicationModel } from './../models/application/application-interface'
31 const dbname = CONFIG.DATABASE.DBNAME
32 const username = CONFIG.DATABASE.USERNAME
33 const password = CONFIG.DATABASE.PASSWORD
36 sequelize?: Sequelize.Sequelize,
37 init?: (silent: boolean) => Promise<void>,
39 Application?: ApplicationModel,
42 OAuthClient?: OAuthClientModel,
43 OAuthToken?: OAuthTokenModel,
45 RequestToPod?: RequestToPodModel,
46 RequestVideoEvent?: RequestVideoEventModel,
47 RequestVideoQadu?: RequestVideoQaduModel,
48 Request?: RequestModel,
50 UserVideoRate?: UserVideoRateModel,
52 VideoAbuse?: VideoAbuseModel,
53 VideoFile?: VideoFileModel,
54 BlacklistedVideo?: BlacklistedVideoModel,
55 VideoTag?: VideoTagModel,
59 const sequelize = new Sequelize(dbname, username, password, {
61 host: CONFIG.DATABASE.HOSTNAME,
62 port: CONFIG.DATABASE.PORT,
63 benchmark: isTestInstance(),
65 logging: (message: string, benchmark: number) => {
66 let newMessage = message
67 if (isTestInstance() === true && benchmark !== undefined) {
68 newMessage += ' | ' + benchmark + 'ms'
71 logger.debug(newMessage)
75 database.sequelize = sequelize
77 database.init = (silent: boolean) => {
78 const modelDirectory = join(__dirname, '..', 'models')
80 return getModelFiles(modelDirectory).then(filePaths => {
81 filePaths.forEach(filePath => {
82 const model = sequelize.import(filePath)
84 database[model['name']] = model
87 Object.keys(database).forEach(modelName => {
88 if ('associate' in database[modelName]) {
89 database[modelName].associate(database)
93 if (!silent) logger.info('Database %s is ready.', dbname)
99 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 function getModelFiles (modelDirectory: string) {
108 return readdirPromise(modelDirectory)
110 const directories: string[] = files.filter(directory => {
113 directory.endsWith('.js.map') ||
114 directory === 'index.js' || directory === 'index.ts' ||
115 directory === 'utils.js' || directory === 'utils.ts'
123 .then(directories => {
126 // For each directory we read it and append model in the modelFilePaths array
127 directories.forEach(directory => {
128 const modelDirectoryPath = join(modelDirectory, directory)
130 const promise = readdirPromise(modelDirectoryPath).then(files => {
131 const filteredFiles = files.filter(file => {
133 file === 'index.js' || file === 'index.ts' ||
134 file === 'utils.js' || file === 'utils.ts' ||
135 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
136 file.endsWith('.js.map')
140 }).map(file => join(modelDirectoryPath, file))
148 return Promise.all(tasks)
150 .then((filteredFiles: string[][]) => {
151 return flattenDepth<string>(filteredFiles, 1)