]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - database.ts
6e3a8d009bba62512cdfccd5b0bc929ff1bc04e2
[github/Chocobozzz/PeerTube.git] / 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 VideoTagModel,
27 VideoModel
28 } from '../models'
29
30 const dbname = CONFIG.DATABASE.DBNAME
31 const username = CONFIG.DATABASE.USERNAME
32 const password = CONFIG.DATABASE.PASSWORD
33
34 const database: {
35 sequelize?: Sequelize.Sequelize,
36 init?: (silent: boolean) => Promise<void>,
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 } = {}
56
57 const sequelize = new Sequelize(dbname, username, password, {
58 dialect: 'postgres',
59 host: CONFIG.DATABASE.HOSTNAME,
60 port: CONFIG.DATABASE.PORT,
61 benchmark: isTestInstance(),
62
63 logging: function (message: string, benchmark: number) {
64 let newMessage = message
65 if (benchmark !== undefined) {
66 newMessage += ' | ' + benchmark + 'ms'
67 }
68
69 logger.debug(newMessage)
70 }
71 })
72
73 database.sequelize = sequelize
74
75 database.init = function (silent: boolean) {
76 const modelDirectory = join(__dirname, '..', 'models')
77
78 return getModelFiles(modelDirectory).then(filePaths => {
79 filePaths.forEach(filePath => {
80 const model = sequelize.import(filePath)
81
82 database[model['name']] = model
83 })
84
85 Object.keys(database).forEach(modelName => {
86 if ('associate' in database[modelName]) {
87 database[modelName].associate(database)
88 }
89 })
90
91 if (!silent) logger.info('Database %s is ready.', dbname)
92
93 return undefined
94 })
95 }
96
97 // ---------------------------------------------------------------------------
98
99 export {
100 database
101 }
102
103 // ---------------------------------------------------------------------------
104
105 function 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 })
118
119 return directories
120 })
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
141 })
142
143 tasks.push(promise)
144 })
145
146 return Promise.all(tasks)
147 })
148 .then((filteredFiles: string[][]) => {
149 return flattenDepth<string>(filteredFiles, 1)
150 })
151 }