]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as fs from 'fs' |
65fcc311 | 2 | import { join } from 'path' |
4d4e5cd4 | 3 | import * as Sequelize from 'sequelize' |
74889a71 | 4 | import { each } from 'async' |
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' | |
1840c2f7 | 9 | import { isTestInstance } 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, | |
26 | VideoTagModel, | |
27 | VideoModel | |
28 | } from '../models' | |
8c308c2b | 29 | |
65fcc311 C |
30 | const dbname = CONFIG.DATABASE.DBNAME |
31 | const username = CONFIG.DATABASE.USERNAME | |
32 | const password = CONFIG.DATABASE.PASSWORD | |
8c308c2b | 33 | |
e02643f3 C |
34 | const database: { |
35 | sequelize?: Sequelize.Sequelize, | |
36 | init?: (silent: any, callback: any) => void, | |
37 | ||
38 | Application?: ApplicationModel, | |
39 | Author?: AuthorModel, | |
40 | Job?: JobModel, | |
41 | OAuthClient?: OAuthClientModel, | |
42 | OAuthToken?: OAuthTokenModel, | |
43 | Pod?: PodModel, | |
44 | RequestToPod?: RequestToPodModel, | |
45 | RequestVideoEvent?: RequestVideoEventModel, | |
46 | RequestVideoQadu?: RequestVideoQaduModel, | |
47 | Request?: RequestModel, | |
48 | Tag?: TagModel, | |
49 | UserVideoRate?: UserVideoRateModel, | |
50 | User?: UserModel, | |
51 | VideoAbuse?: VideoAbuseModel, | |
52 | BlacklistedVideo?: BlacklistedVideoModel, | |
53 | VideoTag?: VideoTagModel, | |
54 | Video?: VideoModel | |
55 | } = {} | |
b769007f C |
56 | |
57 | const sequelize = new Sequelize(dbname, username, password, { | |
feb4bdfd | 58 | dialect: 'postgres', |
65fcc311 C |
59 | host: CONFIG.DATABASE.HOSTNAME, |
60 | port: CONFIG.DATABASE.PORT, | |
61 | benchmark: isTestInstance(), | |
7920c273 | 62 | |
69818c93 | 63 | logging: function (message: string, benchmark: number) { |
7920c273 C |
64 | let newMessage = message |
65 | if (benchmark !== undefined) { | |
66 | newMessage += ' | ' + benchmark + 'ms' | |
67 | } | |
68 | ||
69 | logger.debug(newMessage) | |
70 | } | |
feb4bdfd C |
71 | }) |
72 | ||
b769007f | 73 | database.sequelize = sequelize |
feb4bdfd | 74 | |
69818c93 | 75 | database.init = function (silent: boolean, callback: (err: Error) => void) { |
65fcc311 | 76 | const modelDirectory = join(__dirname, '..', 'models') |
c45f7f84 | 77 | |
74889a71 C |
78 | getModelFiles(modelDirectory, function (err, filePaths) { |
79 | if (err) throw err | |
8c308c2b | 80 | |
74889a71 C |
81 | filePaths.forEach(function (filePath) { |
82 | const model = sequelize.import(filePath) | |
b769007f | 83 | |
65fcc311 | 84 | database[model['name']] = model |
b769007f C |
85 | }) |
86 | ||
87 | Object.keys(database).forEach(function (modelName) { | |
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 C |
94 | |
95 | return callback(null) | |
96 | }) | |
97 | } | |
65fcc311 C |
98 | |
99 | // --------------------------------------------------------------------------- | |
100 | ||
e02643f3 C |
101 | export { |
102 | database | |
103 | } | |
74889a71 C |
104 | |
105 | // --------------------------------------------------------------------------- | |
106 | ||
107 | function getModelFiles (modelDirectory: string, callback: (err: Error, filePaths: string[]) => void) { | |
108 | fs.readdir(modelDirectory, function (err, files) { | |
109 | if (err) throw err | |
110 | ||
111 | const directories = files.filter(function (directory) { | |
0c238854 | 112 | // Find directories |
74889a71 | 113 | if ( |
0c238854 | 114 | directory.endsWith('.js.map') || |
74889a71 C |
115 | directory === 'index.js' || directory === 'index.ts' || |
116 | directory === 'utils.js' || directory === 'utils.ts' | |
117 | ) return false | |
118 | ||
119 | return true | |
120 | }) | |
121 | ||
122 | let modelFilePaths: string[] = [] | |
123 | ||
124 | // For each directory we read it and append model in the modelFilePaths array | |
125 | each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) { | |
126 | const modelDirectoryPath = join(modelDirectory, directory) | |
127 | ||
128 | fs.readdir(modelDirectoryPath, function (err, files) { | |
129 | if (err) return eachCallback(err) | |
130 | ||
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 => { | |
141 | return join(modelDirectoryPath, file) | |
142 | }) | |
143 | ||
144 | modelFilePaths = modelFilePaths.concat(filteredFiles) | |
145 | ||
146 | return eachCallback(null) | |
147 | }) | |
df98563e | 148 | }, function (err: Error) { |
74889a71 C |
149 | return callback(err, modelFilePaths) |
150 | }) | |
151 | }) | |
152 | } |