d04c8db1b40beca01481bccf1d814ec26d42fb3b
[oweals/peertube.git] / server / initializers / database.ts
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'
6
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'
11 import {
12   ApplicationModel,
13   AuthorModel,
14   JobModel,
15   OAuthClientModel,
16   OAuthTokenModel,
17   PodModel,
18   RequestModel,
19   RequestToPodModel,
20   RequestVideoEventModel,
21   RequestVideoQaduModel,
22   TagModel,
23   UserModel,
24   UserVideoRateModel,
25   VideoAbuseModel,
26   BlacklistedVideoModel,
27   VideoFileModel,
28   VideoTagModel,
29   VideoModel
30 } from '../models'
31
32 const dbname = CONFIG.DATABASE.DBNAME
33 const username = CONFIG.DATABASE.USERNAME
34 const password = CONFIG.DATABASE.PASSWORD
35
36 const database: {
37   sequelize?: Sequelize.Sequelize,
38   init?: (silent: boolean) => Promise<void>,
39
40   Application?: ApplicationModel,
41   Author?: AuthorModel,
42   Job?: JobModel,
43   OAuthClient?: OAuthClientModel,
44   OAuthToken?: OAuthTokenModel,
45   Pod?: PodModel,
46   RequestToPod?: RequestToPodModel,
47   RequestVideoEvent?: RequestVideoEventModel,
48   RequestVideoQadu?: RequestVideoQaduModel,
49   Request?: RequestModel,
50   Tag?: TagModel,
51   UserVideoRate?: UserVideoRateModel,
52   User?: UserModel,
53   VideoAbuse?: VideoAbuseModel,
54   VideoFile?: VideoFileModel,
55   BlacklistedVideo?: BlacklistedVideoModel,
56   VideoTag?: VideoTagModel,
57   Video?: VideoModel
58 } = {}
59
60 const sequelize = new Sequelize(dbname, username, password, {
61   dialect: 'postgres',
62   host: CONFIG.DATABASE.HOSTNAME,
63   port: CONFIG.DATABASE.PORT,
64   benchmark: isTestInstance(),
65
66   logging: (message: string, benchmark: number) => {
67     let newMessage = message
68     if (benchmark !== undefined) {
69       newMessage += ' | ' + benchmark + 'ms'
70     }
71
72     logger.debug(newMessage)
73   }
74 })
75
76 database.sequelize = sequelize
77
78 database.init = (silent: boolean) => {
79   const modelDirectory = join(__dirname, '..', 'models')
80
81   return getModelFiles(modelDirectory).then(filePaths => {
82     filePaths.forEach(filePath => {
83       const model = sequelize.import(filePath)
84
85       database[model['name']] = model
86     })
87
88     Object.keys(database).forEach(modelName => {
89       if ('associate' in database[modelName]) {
90         database[modelName].associate(database)
91       }
92     })
93
94     if (!silent) logger.info('Database %s is ready.', dbname)
95
96     return undefined
97   })
98 }
99
100 // ---------------------------------------------------------------------------
101
102 export {
103   database
104 }
105
106 // ---------------------------------------------------------------------------
107
108 function getModelFiles (modelDirectory: string) {
109   return readdirPromise(modelDirectory)
110     .then(files => {
111       const directories: string[] = files.filter(directory => {
112         // Find directories
113         if (
114           directory.endsWith('.js.map') ||
115           directory === 'index.js' || directory === 'index.ts' ||
116           directory === 'utils.js' || directory === 'utils.ts'
117         ) return false
118
119         return true
120       })
121
122       return directories
123     })
124     .then(directories => {
125       const tasks = []
126
127       // For each directory we read it and append model in the modelFilePaths array
128       directories.forEach(directory => {
129         const modelDirectoryPath = join(modelDirectory, directory)
130
131         const promise = readdirPromise(modelDirectoryPath).then(files => {
132           const filteredFiles = files.filter(file => {
133             if (
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')
138             ) return false
139
140             return true
141           }).map(file => join(modelDirectoryPath, file))
142
143           return filteredFiles
144         })
145
146         tasks.push(promise)
147       })
148
149       return Promise.all(tasks)
150     })
151     .then((filteredFiles: string[][]) => {
152       return flattenDepth<string>(filteredFiles, 1)
153     })
154 }