]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/database.ts
Reorganize model files
[github/Chocobozzz/PeerTube.git] / server / initializers / database.ts
1 import * as fs from 'fs'
2 import { join } from 'path'
3 import * as Sequelize from 'sequelize'
4 import { each } from 'async'
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 } 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: any, callback: any) => 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, callback: (err: Error) => void) {
76 const modelDirectory = join(__dirname, '..', 'models')
77
78 getModelFiles(modelDirectory, function (err, filePaths) {
79 if (err) throw err
80
81 filePaths.forEach(function (filePath) {
82 const model = sequelize.import(filePath)
83
84 database[model['name']] = model
85 })
86
87 Object.keys(database).forEach(function (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 callback(null)
96 })
97 }
98
99 // ---------------------------------------------------------------------------
100
101 export {
102 database
103 }
104
105 // ---------------------------------------------------------------------------
106
107 function getModelFiles (modelDirectory: string, callback: (err: Error, filePaths: string[]) => void) {
108 fs.readdir(modelDirectory, function (err, files) {
109 if (err) throw err
110
111 const directories = files.filter(function (directory) {
112 // For all models but not utils.js
113 if (
114 directory === 'index.js' || directory === 'index.ts' ||
115 directory === 'utils.js' || directory === 'utils.ts'
116 ) return false
117
118 return true
119 })
120
121 let modelFilePaths: string[] = []
122
123 // For each directory we read it and append model in the modelFilePaths array
124 each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) {
125 const modelDirectoryPath = join(modelDirectory, directory)
126
127 fs.readdir(modelDirectoryPath, function (err, files) {
128 if (err) return eachCallback(err)
129
130 const filteredFiles = files.filter(file => {
131 if (
132 file === 'index.js' || file === 'index.ts' ||
133 file === 'utils.js' || file === 'utils.ts' ||
134 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
135 file.endsWith('.js.map')
136 ) return false
137
138 return true
139 }).map(file => {
140 return join(modelDirectoryPath, file)
141 })
142
143 modelFilePaths = modelFilePaths.concat(filteredFiles)
144
145 return eachCallback(null)
146 })
147 }, function(err: Error) {
148 return callback(err, modelFilePaths)
149 })
150 })
151 }