1 import { join } from 'path'
2 import { flattenDepth } from 'lodash'
3 import * as Sequelize from 'sequelize'
4 import * as Promise from 'bluebird'
6 import { CONFIG } from './constants'
7 // Do not use barrel, we need to load database first
8 import { logger } from '../helpers/logger'
9 import { isTestInstance, readdirPromise } from '../helpers/core-utils'
19 RequestVideoEventModel,
20 RequestVideoQaduModel,
25 BlacklistedVideoModel,
30 const dbname = CONFIG.DATABASE.DBNAME
31 const username = CONFIG.DATABASE.USERNAME
32 const password = CONFIG.DATABASE.PASSWORD
35 sequelize?: Sequelize.Sequelize,
36 init?: (silent: boolean) => Promise<void>,
38 Application?: ApplicationModel,
41 OAuthClient?: OAuthClientModel,
42 OAuthToken?: OAuthTokenModel,
44 RequestToPod?: RequestToPodModel,
45 RequestVideoEvent?: RequestVideoEventModel,
46 RequestVideoQadu?: RequestVideoQaduModel,
47 Request?: RequestModel,
49 UserVideoRate?: UserVideoRateModel,
51 VideoAbuse?: VideoAbuseModel,
52 BlacklistedVideo?: BlacklistedVideoModel,
53 VideoTag?: VideoTagModel,
57 const sequelize = new Sequelize(dbname, username, password, {
59 host: CONFIG.DATABASE.HOSTNAME,
60 port: CONFIG.DATABASE.PORT,
61 benchmark: isTestInstance(),
63 logging: function (message: string, benchmark: number) {
64 let newMessage = message
65 if (benchmark !== undefined) {
66 newMessage += ' | ' + benchmark + 'ms'
69 logger.debug(newMessage)
73 database.sequelize = sequelize
75 database.init = function (silent: boolean) {
76 const modelDirectory = join(__dirname, '..', 'models')
78 return getModelFiles(modelDirectory).then(filePaths => {
79 filePaths.forEach(filePath => {
80 const model = sequelize.import(filePath)
82 database[model['name']] = model
85 Object.keys(database).forEach(modelName => {
86 if ('associate' in database[modelName]) {
87 database[modelName].associate(database)
91 if (!silent) logger.info('Database %s is ready.', dbname)
97 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 function getModelFiles (modelDirectory: string) {
106 return readdirPromise(modelDirectory)
108 const directories: string[] = files.filter(function (directory) {
111 directory.endsWith('.js.map') ||
112 directory === 'index.js' || directory === 'index.ts' ||
113 directory === 'utils.js' || directory === 'utils.ts'
121 .then(directories => {
124 // For each directory we read it and append model in the modelFilePaths array
125 directories.forEach(directory => {
126 const modelDirectoryPath = join(modelDirectory, directory)
128 const promise = readdirPromise(modelDirectoryPath).then(files => {
129 const filteredFiles = files.filter(file => {
131 file === 'index.js' || file === 'index.ts' ||
132 file === 'utils.js' || file === 'utils.ts' ||
133 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
134 file.endsWith('.js.map')
138 }).map(file => join(modelDirectoryPath, file))
146 return Promise.all(tasks)
148 .then((filteredFiles: string[][]) => {
149 return flattenDepth<string>(filteredFiles, 1)