]>
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' |
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' |
fdbda9e3 C |
10 | |
11 | import { VideoModel } from './../models/video/video-interface' | |
12 | import { VideoTagModel } from './../models/video/video-tag-interface' | |
13 | import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface' | |
14 | import { VideoFileModel } from './../models/video/video-file-interface' | |
15 | import { VideoAbuseModel } from './../models/video/video-abuse-interface' | |
72c7248b | 16 | import { VideoChannelModel } from './../models/video/video-channel-interface' |
e4f97bab C |
17 | import { UserModel } from '../models/account/user-interface' |
18 | import { AccountVideoRateModel } from '../models/account/account-video-rate-interface' | |
19 | import { AccountFollowModel } from '../models/account/account-follow-interface' | |
fdbda9e3 | 20 | import { TagModel } from './../models/video/tag-interface' |
60862425 | 21 | import { ServerModel } from '../models/server/server-interface' |
fdbda9e3 C |
22 | import { OAuthTokenModel } from './../models/oauth/oauth-token-interface' |
23 | import { OAuthClientModel } from './../models/oauth/oauth-client-interface' | |
24 | import { JobModel } from './../models/job/job-interface' | |
e4f97bab | 25 | import { AccountModel } from './../models/account/account-interface' |
fdbda9e3 | 26 | import { ApplicationModel } from './../models/application/application-interface' |
8c308c2b | 27 | |
65fcc311 C |
28 | const dbname = CONFIG.DATABASE.DBNAME |
29 | const username = CONFIG.DATABASE.USERNAME | |
30 | const password = CONFIG.DATABASE.PASSWORD | |
8c308c2b | 31 | |
e02643f3 C |
32 | const database: { |
33 | sequelize?: Sequelize.Sequelize, | |
6fcd19ba | 34 | init?: (silent: boolean) => Promise<void>, |
e02643f3 C |
35 | |
36 | Application?: ApplicationModel, | |
e4f97bab | 37 | Account?: AccountModel, |
e02643f3 C |
38 | Job?: JobModel, |
39 | OAuthClient?: OAuthClientModel, | |
40 | OAuthToken?: OAuthTokenModel, | |
60862425 | 41 | Server?: ServerModel, |
e02643f3 | 42 | Tag?: TagModel, |
e4f97bab C |
43 | AccountVideoRate?: AccountVideoRateModel, |
44 | AccountFollow?: AccountFollowModel, | |
e02643f3 C |
45 | User?: UserModel, |
46 | VideoAbuse?: VideoAbuseModel, | |
72c7248b | 47 | VideoChannel?: VideoChannelModel, |
93e1258c | 48 | VideoFile?: VideoFileModel, |
e02643f3 C |
49 | BlacklistedVideo?: BlacklistedVideoModel, |
50 | VideoTag?: VideoTagModel, | |
51 | Video?: VideoModel | |
52 | } = {} | |
b769007f C |
53 | |
54 | const sequelize = new Sequelize(dbname, username, password, { | |
feb4bdfd | 55 | dialect: 'postgres', |
65fcc311 C |
56 | host: CONFIG.DATABASE.HOSTNAME, |
57 | port: CONFIG.DATABASE.PORT, | |
58 | benchmark: isTestInstance(), | |
eb080476 | 59 | isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE, |
c2962505 | 60 | operatorsAliases: false, |
7920c273 | 61 | |
075f16ca | 62 | logging: (message: string, benchmark: number) => { |
7920c273 | 63 | let newMessage = message |
769d3321 | 64 | if (isTestInstance() === true && benchmark !== undefined) { |
7920c273 C |
65 | newMessage += ' | ' + benchmark + 'ms' |
66 | } | |
67 | ||
68 | logger.debug(newMessage) | |
69 | } | |
feb4bdfd C |
70 | }) |
71 | ||
b769007f | 72 | database.sequelize = sequelize |
feb4bdfd | 73 | |
f5028693 | 74 | database.init = async (silent: boolean) => { |
65fcc311 | 75 | const modelDirectory = join(__dirname, '..', 'models') |
c45f7f84 | 76 | |
f5028693 | 77 | const filePaths = await getModelFiles(modelDirectory) |
b769007f | 78 | |
f5028693 | 79 | for (const filePath of filePaths) { |
9567011b C |
80 | try { |
81 | const model = sequelize.import(filePath) | |
b769007f | 82 | |
9567011b C |
83 | database[model['name']] = model |
84 | } catch (err) { | |
85 | logger.error('Cannot import database model %s.', filePath, err) | |
86 | process.exit(0) | |
87 | } | |
f5028693 | 88 | } |
b769007f | 89 | |
f5028693 C |
90 | for (const modelName of Object.keys(database)) { |
91 | if ('associate' in database[modelName]) { | |
59c857da C |
92 | try { |
93 | database[modelName].associate(database) | |
94 | } catch (err) { | |
95 | logger.error('Cannot associate model %s.', modelName, err) | |
96 | process.exit(0) | |
97 | } | |
f5028693 C |
98 | } |
99 | } | |
b769007f | 100 | |
f5028693 C |
101 | if (!silent) logger.info('Database %s is ready.', dbname) |
102 | ||
d412e80e | 103 | return |
b769007f | 104 | } |
65fcc311 C |
105 | |
106 | // --------------------------------------------------------------------------- | |
107 | ||
e02643f3 C |
108 | export { |
109 | database | |
110 | } | |
74889a71 C |
111 | |
112 | // --------------------------------------------------------------------------- | |
113 | ||
f5028693 C |
114 | async function getModelFiles (modelDirectory: string) { |
115 | const files = await readdirPromise(modelDirectory) | |
116 | const directories = files.filter(directory => { | |
117 | // Find directories | |
118 | if ( | |
119 | directory.endsWith('.js.map') || | |
120 | directory === 'index.js' || directory === 'index.ts' || | |
121 | directory === 'utils.js' || directory === 'utils.ts' | |
122 | ) return false | |
123 | ||
124 | return true | |
125 | }) | |
74889a71 | 126 | |
e4f97bab | 127 | const tasks: Promise<any>[] = [] |
6fcd19ba | 128 | |
f5028693 C |
129 | // For each directory we read it and append model in the modelFilePaths array |
130 | for (const directory of directories) { | |
131 | const modelDirectoryPath = join(modelDirectory, directory) | |
6fcd19ba | 132 | |
f5028693 C |
133 | const promise = readdirPromise(modelDirectoryPath) |
134 | .then(files => { | |
135 | const filteredFiles = files | |
136 | .filter(file => { | |
6fcd19ba C |
137 | if ( |
138 | file === 'index.js' || file === 'index.ts' || | |
139 | file === 'utils.js' || file === 'utils.ts' || | |
140 | file.endsWith('-interface.js') || file.endsWith('-interface.ts') || | |
141 | file.endsWith('.js.map') | |
142 | ) return false | |
143 | ||
144 | return true | |
f5028693 C |
145 | }) |
146 | .map(file => join(modelDirectoryPath, file)) | |
74889a71 | 147 | |
f5028693 | 148 | return filteredFiles |
74889a71 | 149 | }) |
6fcd19ba | 150 | |
f5028693 C |
151 | tasks.push(promise) |
152 | } | |
153 | ||
154 | const filteredFilesArray: string[][] = await Promise.all(tasks) | |
155 | return flattenDepth<string>(filteredFilesArray, 1) | |
74889a71 | 156 | } |