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,
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 (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)