]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { Sequelize as SequelizeTypescript } from 'sequelize-typescript' |
2 | import { isTestInstance } from '../helpers/core-utils' | |
3 | import { logger } from '../helpers/logger' | |
8c308c2b | 4 | |
3fd3ab2d C |
5 | import { AccountModel } from '../models/account/account' |
6 | import { AccountFollowModel } from '../models/account/account-follow' | |
7 | import { AccountVideoRateModel } from '../models/account/account-video-rate' | |
8 | import { UserModel } from '../models/account/user' | |
9 | import { ApplicationModel } from '../models/application/application' | |
10 | import { AvatarModel } from '../models/avatar/avatar' | |
11 | import { JobModel } from '../models/job/job' | |
12 | import { OAuthClientModel } from '../models/oauth/oauth-client' | |
13 | import { OAuthTokenModel } from '../models/oauth/oauth-token' | |
14 | import { ServerModel } from '../models/server/server' | |
15 | import { TagModel } from '../models/video/tag' | |
16 | import { VideoModel } from '../models/video/video' | |
17 | import { VideoAbuseModel } from '../models/video/video-abuse' | |
18 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | |
19 | import { VideoChannelModel } from '../models/video/video-channel' | |
20 | import { VideoChannelShareModel } from '../models/video/video-channel-share' | |
21 | import { VideoFileModel } from '../models/video/video-file' | |
22 | import { VideoShareModel } from '../models/video/video-share' | |
23 | import { VideoTagModel } from '../models/video/video-tag' | |
65fcc311 | 24 | import { CONFIG } from './constants' |
fdbda9e3 | 25 | |
3fd3ab2d | 26 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string |
8c308c2b | 27 | |
65fcc311 C |
28 | const dbname = CONFIG.DATABASE.DBNAME |
29 | const username = CONFIG.DATABASE.USERNAME | |
30 | const password = CONFIG.DATABASE.PASSWORD | |
8c308c2b | 31 | |
3fd3ab2d C |
32 | const sequelizeTypescript = new SequelizeTypescript({ |
33 | database: dbname, | |
feb4bdfd | 34 | dialect: 'postgres', |
3fd3ab2d C |
35 | username, |
36 | password, | |
37 | modelPaths: [__dirname + '/models'], | |
65fcc311 | 38 | benchmark: isTestInstance(), |
3fd3ab2d | 39 | isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE, |
c2962505 | 40 | operatorsAliases: false, |
075f16ca | 41 | logging: (message: string, benchmark: number) => { |
165cdc75 C |
42 | if (process.env.NODE_DB_LOG === 'false') return |
43 | ||
7920c273 | 44 | let newMessage = message |
769d3321 | 45 | if (isTestInstance() === true && benchmark !== undefined) { |
7920c273 C |
46 | newMessage += ' | ' + benchmark + 'ms' |
47 | } | |
48 | ||
49 | logger.debug(newMessage) | |
50 | } | |
feb4bdfd C |
51 | }) |
52 | ||
91fea9fc | 53 | async function initDatabaseModels (silent: boolean) { |
3fd3ab2d C |
54 | sequelizeTypescript.addModels([ |
55 | ApplicationModel, | |
56 | AvatarModel, | |
57 | AccountModel, | |
58 | JobModel, | |
59 | OAuthClientModel, | |
60 | OAuthTokenModel, | |
61 | ServerModel, | |
62 | TagModel, | |
63 | AccountVideoRateModel, | |
64 | AccountFollowModel, | |
65 | UserModel, | |
66 | VideoAbuseModel, | |
67 | VideoChannelModel, | |
68 | VideoChannelShareModel, | |
69 | VideoShareModel, | |
70 | VideoFileModel, | |
71 | VideoBlacklistModel, | |
72 | VideoTagModel, | |
73 | VideoModel | |
74 | ]) | |
b769007f | 75 | |
f5028693 C |
76 | if (!silent) logger.info('Database %s is ready.', dbname) |
77 | ||
d412e80e | 78 | return |
b769007f | 79 | } |
65fcc311 C |
80 | |
81 | // --------------------------------------------------------------------------- | |
82 | ||
e02643f3 | 83 | export { |
91fea9fc | 84 | initDatabaseModels, |
3fd3ab2d | 85 | sequelizeTypescript |
74889a71 | 86 | } |