]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Support roles with rights and add moderator role
[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
C
86 for (const filePath of filePaths) {
87 const model = sequelize.import(filePath)
b769007f 88
f5028693
C
89 database[model['name']] = model
90 }
b769007f 91
f5028693
C
92 for (const modelName of Object.keys(database)) {
93 if ('associate' in database[modelName]) {
94 database[modelName].associate(database)
95 }
96 }
b769007f 97
f5028693
C
98 if (!silent) logger.info('Database %s is ready.', dbname)
99
d412e80e 100 return
b769007f 101}
65fcc311
C
102
103// ---------------------------------------------------------------------------
104
e02643f3
C
105export {
106 database
107}
74889a71
C
108
109// ---------------------------------------------------------------------------
110
f5028693
C
111async function getModelFiles (modelDirectory: string) {
112 const files = await readdirPromise(modelDirectory)
113 const directories = files.filter(directory => {
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
f5028693 124 const tasks: Bluebird<any>[] = []
6fcd19ba 125
f5028693
C
126 // For each directory we read it and append model in the modelFilePaths array
127 for (const directory of directories) {
128 const modelDirectoryPath = join(modelDirectory, directory)
6fcd19ba 129
f5028693
C
130 const promise = readdirPromise(modelDirectoryPath)
131 .then(files => {
132 const filteredFiles = files
133 .filter(file => {
6fcd19ba
C
134 if (
135 file === 'index.js' || file === 'index.ts' ||
136 file === 'utils.js' || file === 'utils.ts' ||
137 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
138 file.endsWith('.js.map')
139 ) return false
140
141 return true
f5028693
C
142 })
143 .map(file => join(modelDirectoryPath, file))
74889a71 144
f5028693 145 return filteredFiles
74889a71 146 })
6fcd19ba 147
f5028693
C
148 tasks.push(promise)
149 }
150
151 const filteredFilesArray: string[][] = await Promise.all(tasks)
152 return flattenDepth<string>(filteredFilesArray, 1)
74889a71 153}