]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Fix spelling (#126)
[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'
f5028693 5import * as Bluebird 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,
c2962505 67 operatorsAliases: false,
7920c273 68
075f16ca 69 logging: (message: string, benchmark: number) => {
7920c273 70 let newMessage = message
769d3321 71 if (isTestInstance() === true && benchmark !== undefined) {
7920c273
C
72 newMessage += ' | ' + benchmark + 'ms'
73 }
74
75 logger.debug(newMessage)
76 }
feb4bdfd
C
77})
78
b769007f 79database.sequelize = sequelize
feb4bdfd 80
f5028693 81database.init = async (silent: boolean) => {
65fcc311 82 const modelDirectory = join(__dirname, '..', 'models')
c45f7f84 83
f5028693 84 const filePaths = await getModelFiles(modelDirectory)
b769007f 85
f5028693 86 for (const filePath of filePaths) {
9567011b
C
87 try {
88 const model = sequelize.import(filePath)
b769007f 89
9567011b
C
90 database[model['name']] = model
91 } catch (err) {
92 logger.error('Cannot import database model %s.', filePath, err)
93 process.exit(0)
94 }
f5028693 95 }
b769007f 96
f5028693
C
97 for (const modelName of Object.keys(database)) {
98 if ('associate' in database[modelName]) {
99 database[modelName].associate(database)
100 }
101 }
b769007f 102
f5028693
C
103 if (!silent) logger.info('Database %s is ready.', dbname)
104
d412e80e 105 return
b769007f 106}
65fcc311
C
107
108// ---------------------------------------------------------------------------
109
e02643f3
C
110export {
111 database
112}
74889a71
C
113
114// ---------------------------------------------------------------------------
115
f5028693
C
116async function getModelFiles (modelDirectory: string) {
117 const files = await readdirPromise(modelDirectory)
118 const directories = files.filter(directory => {
119 // Find directories
120 if (
121 directory.endsWith('.js.map') ||
122 directory === 'index.js' || directory === 'index.ts' ||
123 directory === 'utils.js' || directory === 'utils.ts'
124 ) return false
125
126 return true
127 })
74889a71 128
f5028693 129 const tasks: Bluebird<any>[] = []
6fcd19ba 130
f5028693
C
131 // For each directory we read it and append model in the modelFilePaths array
132 for (const directory of directories) {
133 const modelDirectoryPath = join(modelDirectory, directory)
6fcd19ba 134
f5028693
C
135 const promise = readdirPromise(modelDirectoryPath)
136 .then(files => {
137 const filteredFiles = files
138 .filter(file => {
6fcd19ba
C
139 if (
140 file === 'index.js' || file === 'index.ts' ||
141 file === 'utils.js' || file === 'utils.ts' ||
142 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
143 file.endsWith('.js.map')
144 ) return false
145
146 return true
f5028693
C
147 })
148 .map(file => join(modelDirectoryPath, file))
74889a71 149
f5028693 150 return filteredFiles
74889a71 151 })
6fcd19ba 152
f5028693
C
153 tasks.push(promise)
154 }
155
156 const filteredFilesArray: string[][] = await Promise.all(tasks)
157 return flattenDepth<string>(filteredFilesArray, 1)
74889a71 158}