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