]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Use async/await in controllers
[github/Chocobozzz/PeerTube.git] / server / initializers / database.ts
CommitLineData
65fcc311 1import { join } from 'path'
6fcd19ba 2import { flattenDepth } from 'lodash'
b0f9f39e 3require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
4d4e5cd4 4import * as Sequelize from 'sequelize'
6fcd19ba 5import * as Promise from 'bluebird'
8c308c2b 6
65fcc311
C
7import { CONFIG } from './constants'
8// Do not use barrel, we need to load database first
9import { logger } from '../helpers/logger'
6fcd19ba 10import { isTestInstance, readdirPromise } from '../helpers/core-utils'
fdbda9e3
C
11
12import { VideoModel } from './../models/video/video-interface'
13import { VideoTagModel } from './../models/video/video-tag-interface'
14import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
15import { VideoFileModel } from './../models/video/video-file-interface'
16import { VideoAbuseModel } from './../models/video/video-abuse-interface'
72c7248b 17import { VideoChannelModel } from './../models/video/video-channel-interface'
fdbda9e3
C
18import { UserModel } from './../models/user/user-interface'
19import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
20import { TagModel } from './../models/video/tag-interface'
21import { RequestModel } from './../models/request/request-interface'
22import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
23import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
24import { RequestToPodModel } from './../models/request/request-to-pod-interface'
25import { PodModel } from './../models/pod/pod-interface'
26import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
27import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
28import { JobModel } from './../models/job/job-interface'
29import { AuthorModel } from './../models/video/author-interface'
30import { ApplicationModel } from './../models/application/application-interface'
8c308c2b 31
65fcc311
C
32const dbname = CONFIG.DATABASE.DBNAME
33const username = CONFIG.DATABASE.USERNAME
34const password = CONFIG.DATABASE.PASSWORD
8c308c2b 35
e02643f3
C
36const database: {
37 sequelize?: Sequelize.Sequelize,
6fcd19ba 38 init?: (silent: boolean) => Promise<void>,
e02643f3
C
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,
72c7248b 54 VideoChannel?: VideoChannelModel,
93e1258c 55 VideoFile?: VideoFileModel,
e02643f3
C
56 BlacklistedVideo?: BlacklistedVideoModel,
57 VideoTag?: VideoTagModel,
58 Video?: VideoModel
59} = {}
b769007f
C
60
61const sequelize = new Sequelize(dbname, username, password, {
feb4bdfd 62 dialect: 'postgres',
65fcc311
C
63 host: CONFIG.DATABASE.HOSTNAME,
64 port: CONFIG.DATABASE.PORT,
65 benchmark: isTestInstance(),
eb080476 66 isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
7920c273 67
075f16ca 68 logging: (message: string, benchmark: number) => {
7920c273 69 let newMessage = message
769d3321 70 if (isTestInstance() === true && benchmark !== undefined) {
7920c273
C
71 newMessage += ' | ' + benchmark + 'ms'
72 }
73
74 logger.debug(newMessage)
75 }
feb4bdfd
C
76})
77
b769007f 78database.sequelize = sequelize
feb4bdfd 79
075f16ca 80database.init = (silent: boolean) => {
65fcc311 81 const modelDirectory = join(__dirname, '..', 'models')
c45f7f84 82
6fcd19ba
C
83 return getModelFiles(modelDirectory).then(filePaths => {
84 filePaths.forEach(filePath => {
74889a71 85 const model = sequelize.import(filePath)
b769007f 86
65fcc311 87 database[model['name']] = model
b769007f
C
88 })
89
6fcd19ba 90 Object.keys(database).forEach(modelName => {
b769007f
C
91 if ('associate' in database[modelName]) {
92 database[modelName].associate(database)
93 }
94 })
95
11ac88de 96 if (!silent) logger.info('Database %s is ready.', dbname)
b769007f 97
6fcd19ba 98 return undefined
b769007f
C
99 })
100}
65fcc311
C
101
102// ---------------------------------------------------------------------------
103
e02643f3
C
104export {
105 database
106}
74889a71
C
107
108// ---------------------------------------------------------------------------
109
6fcd19ba
C
110function getModelFiles (modelDirectory: string) {
111 return readdirPromise(modelDirectory)
112 .then(files => {
075f16ca 113 const directories: string[] = files.filter(directory => {
6fcd19ba
C
114 // Find directories
115 if (
116 directory.endsWith('.js.map') ||
117 directory === 'index.js' || directory === 'index.ts' ||
118 directory === 'utils.js' || directory === 'utils.ts'
119 ) return false
120
121 return true
122 })
74889a71 123
6fcd19ba 124 return directories
74889a71 125 })
6fcd19ba
C
126 .then(directories => {
127 const tasks = []
128
129 // For each directory we read it and append model in the modelFilePaths array
130 directories.forEach(directory => {
131 const modelDirectoryPath = join(modelDirectory, directory)
132
133 const promise = readdirPromise(modelDirectoryPath).then(files => {
134 const filteredFiles = files.filter(file => {
135 if (
136 file === 'index.js' || file === 'index.ts' ||
137 file === 'utils.js' || file === 'utils.ts' ||
138 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
139 file.endsWith('.js.map')
140 ) return false
141
142 return true
143 }).map(file => join(modelDirectoryPath, file))
144
145 return filteredFiles
74889a71
C
146 })
147
6fcd19ba 148 tasks.push(promise)
74889a71 149 })
6fcd19ba
C
150
151 return Promise.all(tasks)
152 })
153 .then((filteredFiles: string[][]) => {
154 return flattenDepth<string>(filteredFiles, 1)
74889a71 155 })
74889a71 156}