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