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 { VideoChannelModel } from './../models/video/video-channel-interface'
18 import { UserModel } from './../models/user/user-interface'
19 import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
20 import { TagModel } from './../models/video/tag-interface'
21 import { RequestModel } from './../models/request/request-interface'
22 import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
23 import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
24 import { RequestToPodModel } from './../models/request/request-to-pod-interface'
25 import { PodModel } from './../models/pod/pod-interface'
26 import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
27 import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
28 import { JobModel } from './../models/job/job-interface'
29 import { AuthorModel } from './../models/video/author-interface'
30 import { ApplicationModel } from './../models/application/application-interface'
32 const dbname = CONFIG.DATABASE.DBNAME
33 const username = CONFIG.DATABASE.USERNAME
34 const password = CONFIG.DATABASE.PASSWORD
37 sequelize?: Sequelize.Sequelize,
38 init?: (silent: boolean) => Promise<void>,
40 Application?: ApplicationModel,
43 OAuthClient?: OAuthClientModel,
44 OAuthToken?: OAuthTokenModel,
46 RequestToPod?: RequestToPodModel,
47 RequestVideoEvent?: RequestVideoEventModel,
48 RequestVideoQadu?: RequestVideoQaduModel,
49 Request?: RequestModel,
51 UserVideoRate?: UserVideoRateModel,
53 VideoAbuse?: VideoAbuseModel,
54 VideoChannel?: VideoChannelModel,
55 VideoFile?: VideoFileModel,
56 BlacklistedVideo?: BlacklistedVideoModel,
57 VideoTag?: VideoTagModel,
61 const sequelize = new Sequelize(dbname, username, password, {
63 host: CONFIG.DATABASE.HOSTNAME,
64 port: CONFIG.DATABASE.PORT,
65 benchmark: isTestInstance(),
66 isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
68 logging: (message: string, benchmark: number) => {
69 let newMessage = message
70 if (isTestInstance() === true && benchmark !== undefined) {
71 newMessage += ' | ' + benchmark + 'ms'
74 logger.debug(newMessage)
78 database.sequelize = sequelize
80 database.init = (silent: boolean) => {
81 const modelDirectory = join(__dirname, '..', 'models')
83 return getModelFiles(modelDirectory).then(filePaths => {
84 filePaths.forEach(filePath => {
85 const model = sequelize.import(filePath)
87 database[model['name']] = model
90 Object.keys(database).forEach(modelName => {
91 if ('associate' in database[modelName]) {
92 database[modelName].associate(database)
96 if (!silent) logger.info('Database %s is ready.', dbname)
102 // ---------------------------------------------------------------------------
108 // ---------------------------------------------------------------------------
110 function getModelFiles (modelDirectory: string) {
111 return readdirPromise(modelDirectory)
113 const directories: string[] = files.filter(directory => {
116 directory.endsWith('.js.map') ||
117 directory === 'index.js' || directory === 'index.ts' ||
118 directory === 'utils.js' || directory === 'utils.ts'
126 .then(directories => {
129 // For each directory we read it and append model in the modelFilePaths array
130 directories.forEach(directory => {
131 const modelDirectoryPath = join(modelDirectory, directory)
133 const promise = readdirPromise(modelDirectoryPath).then(files => {
134 const filteredFiles = files.filter(file => {
136 file === 'index.js' || file === 'index.ts' ||
137 file === 'utils.js' || file === 'utils.ts' ||
138 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
139 file.endsWith('.js.map')
143 }).map(file => join(modelDirectoryPath, file))
151 return Promise.all(tasks)
153 .then((filteredFiles: string[][]) => {
154 return flattenDepth<string>(filteredFiles, 1)