]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Update webpack config
[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'
e02643f3
C
11import {
12 ApplicationModel,
13 AuthorModel,
14 JobModel,
15 OAuthClientModel,
16 OAuthTokenModel,
17 PodModel,
18 RequestModel,
19 RequestToPodModel,
20 RequestVideoEventModel,
21 RequestVideoQaduModel,
22 TagModel,
23 UserModel,
24 UserVideoRateModel,
25 VideoAbuseModel,
26 BlacklistedVideoModel,
93e1258c 27 VideoFileModel,
e02643f3
C
28 VideoTagModel,
29 VideoModel
30} from '../models'
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,
93e1258c 54 VideoFile?: VideoFileModel,
e02643f3
C
55 BlacklistedVideo?: BlacklistedVideoModel,
56 VideoTag?: VideoTagModel,
57 Video?: VideoModel
58} = {}
b769007f
C
59
60const sequelize = new Sequelize(dbname, username, password, {
feb4bdfd 61 dialect: 'postgres',
65fcc311
C
62 host: CONFIG.DATABASE.HOSTNAME,
63 port: CONFIG.DATABASE.PORT,
64 benchmark: isTestInstance(),
7920c273 65
075f16ca 66 logging: (message: string, benchmark: number) => {
7920c273
C
67 let newMessage = message
68 if (benchmark !== undefined) {
69 newMessage += ' | ' + benchmark + 'ms'
70 }
71
72 logger.debug(newMessage)
73 }
feb4bdfd
C
74})
75
b769007f 76database.sequelize = sequelize
feb4bdfd 77
075f16ca 78database.init = (silent: boolean) => {
65fcc311 79 const modelDirectory = join(__dirname, '..', 'models')
c45f7f84 80
6fcd19ba
C
81 return getModelFiles(modelDirectory).then(filePaths => {
82 filePaths.forEach(filePath => {
74889a71 83 const model = sequelize.import(filePath)
b769007f 84
65fcc311 85 database[model['name']] = model
b769007f
C
86 })
87
6fcd19ba 88 Object.keys(database).forEach(modelName => {
b769007f
C
89 if ('associate' in database[modelName]) {
90 database[modelName].associate(database)
91 }
92 })
93
11ac88de 94 if (!silent) logger.info('Database %s is ready.', dbname)
b769007f 95
6fcd19ba 96 return undefined
b769007f
C
97 })
98}
65fcc311
C
99
100// ---------------------------------------------------------------------------
101
e02643f3
C
102export {
103 database
104}
74889a71
C
105
106// ---------------------------------------------------------------------------
107
6fcd19ba
C
108function getModelFiles (modelDirectory: string) {
109 return readdirPromise(modelDirectory)
110 .then(files => {
075f16ca 111 const directories: string[] = files.filter(directory => {
6fcd19ba
C
112 // Find directories
113 if (
114 directory.endsWith('.js.map') ||
115 directory === 'index.js' || directory === 'index.ts' ||
116 directory === 'utils.js' || directory === 'utils.ts'
117 ) return false
118
119 return true
120 })
74889a71 121
6fcd19ba 122 return directories
74889a71 123 })
6fcd19ba
C
124 .then(directories => {
125 const tasks = []
126
127 // For each directory we read it and append model in the modelFilePaths array
128 directories.forEach(directory => {
129 const modelDirectoryPath = join(modelDirectory, directory)
130
131 const promise = readdirPromise(modelDirectoryPath).then(files => {
132 const filteredFiles = files.filter(file => {
133 if (
134 file === 'index.js' || file === 'index.ts' ||
135 file === 'utils.js' || file === 'utils.ts' ||
136 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
137 file.endsWith('.js.map')
138 ) return false
139
140 return true
141 }).map(file => join(modelDirectoryPath, file))
142
143 return filteredFiles
74889a71
C
144 })
145
6fcd19ba 146 tasks.push(promise)
74889a71 147 })
6fcd19ba
C
148
149 return Promise.all(tasks)
150 })
151 .then((filteredFiles: string[][]) => {
152 return flattenDepth<string>(filteredFiles, 1)
74889a71 153 })
74889a71 154}