]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { join } from 'path' |
6fcd19ba | 2 | import { flattenDepth } from 'lodash' |
4d4e5cd4 | 3 | import * as Sequelize from 'sequelize' |
6fcd19ba | 4 | import * as Promise from 'bluebird' |
8c308c2b | 5 | |
65fcc311 C |
6 | import { CONFIG } from './constants' |
7 | // Do not use barrel, we need to load database first | |
8 | import { logger } from '../helpers/logger' | |
6fcd19ba | 9 | import { isTestInstance, readdirPromise } from '../helpers/core-utils' |
e02643f3 C |
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, | |
93e1258c | 26 | VideoFileModel, |
e02643f3 C |
27 | VideoTagModel, |
28 | VideoModel | |
29 | } from '../models' | |
8c308c2b | 30 | |
65fcc311 C |
31 | const dbname = CONFIG.DATABASE.DBNAME |
32 | const username = CONFIG.DATABASE.USERNAME | |
33 | const password = CONFIG.DATABASE.PASSWORD | |
8c308c2b | 34 | |
e02643f3 C |
35 | const database: { |
36 | sequelize?: Sequelize.Sequelize, | |
6fcd19ba | 37 | init?: (silent: boolean) => Promise<void>, |
e02643f3 C |
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, | |
93e1258c | 53 | VideoFile?: VideoFileModel, |
e02643f3 C |
54 | BlacklistedVideo?: BlacklistedVideoModel, |
55 | VideoTag?: VideoTagModel, | |
56 | Video?: VideoModel | |
57 | } = {} | |
b769007f C |
58 | |
59 | const sequelize = new Sequelize(dbname, username, password, { | |
feb4bdfd | 60 | dialect: 'postgres', |
65fcc311 C |
61 | host: CONFIG.DATABASE.HOSTNAME, |
62 | port: CONFIG.DATABASE.PORT, | |
63 | benchmark: isTestInstance(), | |
7920c273 | 64 | |
075f16ca | 65 | logging: (message: string, benchmark: number) => { |
7920c273 C |
66 | let newMessage = message |
67 | if (benchmark !== undefined) { | |
68 | newMessage += ' | ' + benchmark + 'ms' | |
69 | } | |
70 | ||
71 | logger.debug(newMessage) | |
72 | } | |
feb4bdfd C |
73 | }) |
74 | ||
b769007f | 75 | database.sequelize = sequelize |
feb4bdfd | 76 | |
075f16ca | 77 | database.init = (silent: boolean) => { |
65fcc311 | 78 | const modelDirectory = join(__dirname, '..', 'models') |
c45f7f84 | 79 | |
6fcd19ba C |
80 | return getModelFiles(modelDirectory).then(filePaths => { |
81 | filePaths.forEach(filePath => { | |
74889a71 | 82 | const model = sequelize.import(filePath) |
b769007f | 83 | |
65fcc311 | 84 | database[model['name']] = model |
b769007f C |
85 | }) |
86 | ||
6fcd19ba | 87 | Object.keys(database).forEach(modelName => { |
b769007f C |
88 | if ('associate' in database[modelName]) { |
89 | database[modelName].associate(database) | |
90 | } | |
91 | }) | |
92 | ||
11ac88de | 93 | if (!silent) logger.info('Database %s is ready.', dbname) |
b769007f | 94 | |
6fcd19ba | 95 | return undefined |
b769007f C |
96 | }) |
97 | } | |
65fcc311 C |
98 | |
99 | // --------------------------------------------------------------------------- | |
100 | ||
e02643f3 C |
101 | export { |
102 | database | |
103 | } | |
74889a71 C |
104 | |
105 | // --------------------------------------------------------------------------- | |
106 | ||
6fcd19ba C |
107 | function getModelFiles (modelDirectory: string) { |
108 | return readdirPromise(modelDirectory) | |
109 | .then(files => { | |
075f16ca | 110 | const directories: string[] = files.filter(directory => { |
6fcd19ba C |
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 | }) | |
74889a71 | 120 | |
6fcd19ba | 121 | return directories |
74889a71 | 122 | }) |
6fcd19ba C |
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 | |
74889a71 C |
143 | }) |
144 | ||
6fcd19ba | 145 | tasks.push(promise) |
74889a71 | 146 | }) |
6fcd19ba C |
147 | |
148 | return Promise.all(tasks) | |
149 | }) | |
150 | .then((filteredFiles: string[][]) => { | |
151 | return flattenDepth<string>(filteredFiles, 1) | |
74889a71 | 152 | }) |
74889a71 | 153 | } |