]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/database.ts
Add tests for npm run scripts
[github/Chocobozzz/PeerTube.git] / server / initializers / database.ts
1 import { join } from 'path'
2 import { flattenDepth } from 'lodash'
3 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
4 import * as Sequelize from 'sequelize'
5 import * as Promise from 'bluebird'
6
7 import { CONFIG } from './constants'
8 // Do not use barrel, we need to load database first
9 import { logger } from '../helpers/logger'
10 import { isTestInstance, readdirPromise } from '../helpers/core-utils'
11
12 import { VideoModel } from './../models/video/video-interface'
13 import { VideoTagModel } from './../models/video/video-tag-interface'
14 import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
15 import { VideoFileModel } from './../models/video/video-file-interface'
16 import { VideoAbuseModel } from './../models/video/video-abuse-interface'
17 import { UserModel } from './../models/user/user-interface'
18 import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
19 import { TagModel } from './../models/video/tag-interface'
20 import { RequestModel } from './../models/request/request-interface'
21 import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
22 import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
23 import { RequestToPodModel } from './../models/request/request-to-pod-interface'
24 import { PodModel } from './../models/pod/pod-interface'
25 import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
26 import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
27 import { JobModel } from './../models/job/job-interface'
28 import { AuthorModel } from './../models/video/author-interface'
29 import { ApplicationModel } from './../models/application/application-interface'
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 }