1 import * as fs from 'fs'
2 import { join } from 'path'
3 import * as Sequelize from 'sequelize'
4 import { each } from 'async'
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 } 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: any, callback: any) => 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, callback: (err: Error) => void) {
76 const modelDirectory = join(__dirname, '..', 'models')
78 getModelFiles(modelDirectory, function (err, filePaths) {
81 filePaths.forEach(function (filePath) {
82 const model = sequelize.import(filePath)
84 database[model['name']] = model
87 Object.keys(database).forEach(function (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, callback: (err: Error, filePaths: string[]) => void) {
108 fs.readdir(modelDirectory, function (err, files) {
111 const directories = files.filter(function (directory) {
112 // For all models but not utils.js
114 directory === 'index.js' || directory === 'index.ts' ||
115 directory === 'utils.js' || directory === 'utils.ts'
121 let modelFilePaths: string[] = []
123 // For each directory we read it and append model in the modelFilePaths array
124 each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) {
125 const modelDirectoryPath = join(modelDirectory, directory)
127 fs.readdir(modelDirectoryPath, function (err, files) {
128 if (err) return eachCallback(err)
130 const filteredFiles = files.filter(file => {
132 file === 'index.js' || file === 'index.ts' ||
133 file === 'utils.js' || file === 'utils.ts' ||
134 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
135 file.endsWith('.js.map')
140 return join(modelDirectoryPath, file)
143 modelFilePaths = modelFilePaths.concat(filteredFiles)
145 return eachCallback(null)
147 }, function(err: Error) {
148 return callback(err, modelFilePaths)