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'
20 RequestVideoEventModel,
21 RequestVideoQaduModel,
26 BlacklistedVideoModel,
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 VideoFile?: VideoFileModel,
55 BlacklistedVideo?: BlacklistedVideoModel,
56 VideoTag?: VideoTagModel,
60 const sequelize = new Sequelize(dbname, username, password, {
62 host: CONFIG.DATABASE.HOSTNAME,
63 port: CONFIG.DATABASE.PORT,
64 benchmark: isTestInstance(),
66 logging: (message: string, benchmark: number) => {
67 let newMessage = message
68 if (benchmark !== undefined) {
69 newMessage += ' | ' + benchmark + 'ms'
72 logger.debug(newMessage)
76 database.sequelize = sequelize
78 database.init = (silent: boolean) => {
79 const modelDirectory = join(__dirname, '..', 'models')
81 return getModelFiles(modelDirectory).then(filePaths => {
82 filePaths.forEach(filePath => {
83 const model = sequelize.import(filePath)
85 database[model['name']] = model
88 Object.keys(database).forEach(modelName => {
89 if ('associate' in database[modelName]) {
90 database[modelName].associate(database)
94 if (!silent) logger.info('Database %s is ready.', dbname)
100 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 function getModelFiles (modelDirectory: string) {
109 return readdirPromise(modelDirectory)
111 const directories: string[] = files.filter(directory => {
114 directory.endsWith('.js.map') ||
115 directory === 'index.js' || directory === 'index.ts' ||
116 directory === 'utils.js' || directory === 'utils.ts'
124 .then(directories => {
127 // For each directory we read it and append model in the modelFilePaths array
128 directories.forEach(directory => {
129 const modelDirectoryPath = join(modelDirectory, directory)
131 const promise = readdirPromise(modelDirectoryPath).then(files => {
132 const filteredFiles = files.filter(file => {
134 file === 'index.js' || file === 'index.ts' ||
135 file === 'utils.js' || file === 'utils.ts' ||
136 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
137 file.endsWith('.js.map')
141 }).map(file => join(modelDirectoryPath, file))
149 return Promise.all(tasks)
151 .then((filteredFiles: string[][]) => {
152 return flattenDepth<string>(filteredFiles, 1)