]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { join } from 'path' |
6fcd19ba | 2 | import { flattenDepth } from 'lodash' |
b0f9f39e | 3 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string |
4d4e5cd4 | 4 | import * as Sequelize from 'sequelize' |
6fcd19ba | 5 | import * as Promise from 'bluebird' |
8c308c2b | 6 | |
65fcc311 C |
7 | import { CONFIG } from './constants' |
8 | // Do not use barrel, we need to load database first | |
9 | import { logger } from '../helpers/logger' | |
6fcd19ba | 10 | import { isTestInstance, readdirPromise } from '../helpers/core-utils' |
fdbda9e3 C |
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' | |
72c7248b | 17 | import { VideoChannelModel } from './../models/video/video-channel-interface' |
fdbda9e3 C |
18 | import { UserModel } from './../models/user/user-interface' |
19 | import { UserVideoRateModel } from './../models/user/user-video-rate-interface' | |
20 | import { TagModel } from './../models/video/tag-interface' | |
21 | import { RequestModel } from './../models/request/request-interface' | |
22 | import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface' | |
23 | import { RequestVideoEventModel } from './../models/request/request-video-event-interface' | |
24 | import { RequestToPodModel } from './../models/request/request-to-pod-interface' | |
25 | import { PodModel } from './../models/pod/pod-interface' | |
26 | import { OAuthTokenModel } from './../models/oauth/oauth-token-interface' | |
27 | import { OAuthClientModel } from './../models/oauth/oauth-client-interface' | |
28 | import { JobModel } from './../models/job/job-interface' | |
29 | import { AuthorModel } from './../models/video/author-interface' | |
30 | import { ApplicationModel } from './../models/application/application-interface' | |
8c308c2b | 31 | |
65fcc311 C |
32 | const dbname = CONFIG.DATABASE.DBNAME |
33 | const username = CONFIG.DATABASE.USERNAME | |
34 | const password = CONFIG.DATABASE.PASSWORD | |
8c308c2b | 35 | |
e02643f3 C |
36 | const 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, | |
72c7248b | 54 | VideoChannel?: VideoChannelModel, |
93e1258c | 55 | VideoFile?: VideoFileModel, |
e02643f3 C |
56 | BlacklistedVideo?: BlacklistedVideoModel, |
57 | VideoTag?: VideoTagModel, | |
58 | Video?: VideoModel | |
59 | } = {} | |
b769007f C |
60 | |
61 | const sequelize = new Sequelize(dbname, username, password, { | |
feb4bdfd | 62 | dialect: 'postgres', |
65fcc311 C |
63 | host: CONFIG.DATABASE.HOSTNAME, |
64 | port: CONFIG.DATABASE.PORT, | |
65 | benchmark: isTestInstance(), | |
7920c273 | 66 | |
075f16ca | 67 | logging: (message: string, benchmark: number) => { |
7920c273 | 68 | let newMessage = message |
769d3321 | 69 | if (isTestInstance() === true && benchmark !== undefined) { |
7920c273 C |
70 | newMessage += ' | ' + benchmark + 'ms' |
71 | } | |
72 | ||
73 | logger.debug(newMessage) | |
74 | } | |
feb4bdfd C |
75 | }) |
76 | ||
b769007f | 77 | database.sequelize = sequelize |
feb4bdfd | 78 | |
075f16ca | 79 | database.init = (silent: boolean) => { |
65fcc311 | 80 | const modelDirectory = join(__dirname, '..', 'models') |
c45f7f84 | 81 | |
6fcd19ba C |
82 | return getModelFiles(modelDirectory).then(filePaths => { |
83 | filePaths.forEach(filePath => { | |
74889a71 | 84 | const model = sequelize.import(filePath) |
b769007f | 85 | |
65fcc311 | 86 | database[model['name']] = model |
b769007f C |
87 | }) |
88 | ||
6fcd19ba | 89 | Object.keys(database).forEach(modelName => { |
b769007f C |
90 | if ('associate' in database[modelName]) { |
91 | database[modelName].associate(database) | |
92 | } | |
93 | }) | |
94 | ||
11ac88de | 95 | if (!silent) logger.info('Database %s is ready.', dbname) |
b769007f | 96 | |
6fcd19ba | 97 | return undefined |
b769007f C |
98 | }) |
99 | } | |
65fcc311 C |
100 | |
101 | // --------------------------------------------------------------------------- | |
102 | ||
e02643f3 C |
103 | export { |
104 | database | |
105 | } | |
74889a71 C |
106 | |
107 | // --------------------------------------------------------------------------- | |
108 | ||
6fcd19ba C |
109 | function getModelFiles (modelDirectory: string) { |
110 | return readdirPromise(modelDirectory) | |
111 | .then(files => { | |
075f16ca | 112 | const directories: string[] = files.filter(directory => { |
6fcd19ba C |
113 | // Find directories |
114 | if ( | |
115 | directory.endsWith('.js.map') || | |
116 | directory === 'index.js' || directory === 'index.ts' || | |
117 | directory === 'utils.js' || directory === 'utils.ts' | |
118 | ) return false | |
119 | ||
120 | return true | |
121 | }) | |
74889a71 | 122 | |
6fcd19ba | 123 | return directories |
74889a71 | 124 | }) |
6fcd19ba C |
125 | .then(directories => { |
126 | const tasks = [] | |
127 | ||
128 | // For each directory we read it and append model in the modelFilePaths array | |
129 | directories.forEach(directory => { | |
130 | const modelDirectoryPath = join(modelDirectory, directory) | |
131 | ||
132 | const promise = readdirPromise(modelDirectoryPath).then(files => { | |
133 | const filteredFiles = files.filter(file => { | |
134 | if ( | |
135 | file === 'index.js' || file === 'index.ts' || | |
136 | file === 'utils.js' || file === 'utils.ts' || | |
137 | file.endsWith('-interface.js') || file.endsWith('-interface.ts') || | |
138 | file.endsWith('.js.map') | |
139 | ) return false | |
140 | ||
141 | return true | |
142 | }).map(file => join(modelDirectoryPath, file)) | |
143 | ||
144 | return filteredFiles | |
74889a71 C |
145 | }) |
146 | ||
6fcd19ba | 147 | tasks.push(promise) |
74889a71 | 148 | }) |
6fcd19ba C |
149 | |
150 | return Promise.all(tasks) | |
151 | }) | |
152 | .then((filteredFiles: string[][]) => { | |
153 | return flattenDepth<string>(filteredFiles, 1) | |
74889a71 | 154 | }) |
74889a71 | 155 | } |