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