]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/database.ts
0a716e4fb0fc34e95f84fa58bb9fc00ab86694b6
[github/Chocobozzz/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
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, readdirPromise } from '../helpers/core-utils'
10
11 import { VideoModel } from './../models/video/video-interface'
12 import { VideoTagModel } from './../models/video/video-tag-interface'
13 import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
14 import { VideoFileModel } from './../models/video/video-file-interface'
15 import { VideoAbuseModel } from './../models/video/video-abuse-interface'
16 import { VideoChannelModel } from './../models/video/video-channel-interface'
17 import { UserModel } from '../models/account/user-interface'
18 import { AccountVideoRateModel } from '../models/account/account-video-rate-interface'
19 import { AccountFollowModel } from '../models/account/account-follow-interface'
20 import { TagModel } from './../models/video/tag-interface'
21 import { ServerModel } from '../models/server/server-interface'
22 import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
23 import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
24 import { JobModel } from './../models/job/job-interface'
25 import { AccountModel } from './../models/account/account-interface'
26 import { ApplicationModel } from './../models/application/application-interface'
27
28 const dbname = CONFIG.DATABASE.DBNAME
29 const username = CONFIG.DATABASE.USERNAME
30 const password = CONFIG.DATABASE.PASSWORD
31
32 const database: {
33 sequelize?: Sequelize.Sequelize,
34 init?: (silent: boolean) => Promise<void>,
35
36 Application?: ApplicationModel,
37 Account?: AccountModel,
38 Job?: JobModel,
39 OAuthClient?: OAuthClientModel,
40 OAuthToken?: OAuthTokenModel,
41 Server?: ServerModel,
42 Tag?: TagModel,
43 AccountVideoRate?: AccountVideoRateModel,
44 AccountFollow?: AccountFollowModel,
45 User?: UserModel,
46 VideoAbuse?: VideoAbuseModel,
47 VideoChannel?: VideoChannelModel,
48 VideoFile?: VideoFileModel,
49 BlacklistedVideo?: BlacklistedVideoModel,
50 VideoTag?: VideoTagModel,
51 Video?: VideoModel
52 } = {}
53
54 const sequelize = new Sequelize(dbname, username, password, {
55 dialect: 'postgres',
56 host: CONFIG.DATABASE.HOSTNAME,
57 port: CONFIG.DATABASE.PORT,
58 benchmark: isTestInstance(),
59 isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
60 operatorsAliases: false,
61
62 logging: (message: string, benchmark: number) => {
63 let newMessage = message
64 if (isTestInstance() === true && benchmark !== undefined) {
65 newMessage += ' | ' + benchmark + 'ms'
66 }
67
68 logger.debug(newMessage)
69 }
70 })
71
72 database.sequelize = sequelize
73
74 database.init = async (silent: boolean) => {
75 const modelDirectory = join(__dirname, '..', 'models')
76
77 const filePaths = await getModelFiles(modelDirectory)
78
79 for (const filePath of filePaths) {
80 try {
81 const model = sequelize.import(filePath)
82
83 database[model['name']] = model
84 } catch (err) {
85 logger.error('Cannot import database model %s.', filePath, err)
86 process.exit(0)
87 }
88 }
89
90 for (const modelName of Object.keys(database)) {
91 if ('associate' in database[modelName]) {
92 try {
93 database[modelName].associate(database)
94 } catch (err) {
95 logger.error('Cannot associate model %s.', modelName, err)
96 process.exit(0)
97 }
98 }
99 }
100
101 if (!silent) logger.info('Database %s is ready.', dbname)
102
103 return
104 }
105
106 // ---------------------------------------------------------------------------
107
108 export {
109 database
110 }
111
112 // ---------------------------------------------------------------------------
113
114 async function getModelFiles (modelDirectory: string) {
115 const files = await readdirPromise(modelDirectory)
116 const directories = files.filter(directory => {
117 // Find directories
118 if (
119 directory.endsWith('.js.map') ||
120 directory === 'index.js' || directory === 'index.ts' ||
121 directory === 'utils.js' || directory === 'utils.ts'
122 ) return false
123
124 return true
125 })
126
127 const tasks: Promise<any>[] = []
128
129 // For each directory we read it and append model in the modelFilePaths array
130 for (const directory of directories) {
131 const modelDirectoryPath = join(modelDirectory, directory)
132
133 const promise = readdirPromise(modelDirectoryPath)
134 .then(files => {
135 const filteredFiles = files
136 .filter(file => {
137 if (
138 file === 'index.js' || file === 'index.ts' ||
139 file === 'utils.js' || file === 'utils.ts' ||
140 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
141 file.endsWith('.js.map')
142 ) return false
143
144 return true
145 })
146 .map(file => join(modelDirectoryPath, file))
147
148 return filteredFiles
149 })
150
151 tasks.push(promise)
152 }
153
154 const filteredFilesArray: string[][] = await Promise.all(tasks)
155 return flattenDepth<string>(filteredFilesArray, 1)
156 }