]>
Commit | Line | Data |
---|---|---|
1 | import { Sequelize as SequelizeTypescript } from 'sequelize-typescript' | |
2 | import { isTestInstance } from '../helpers/core-utils' | |
3 | import { logger } from '../helpers/logger' | |
4 | ||
5 | import { AccountModel } from '../models/account/account' | |
6 | import { AccountVideoRateModel } from '../models/account/account-video-rate' | |
7 | import { UserModel } from '../models/account/user' | |
8 | import { ActorModel } from '../models/activitypub/actor' | |
9 | import { ActorFollowModel } from '../models/activitypub/actor-follow' | |
10 | import { ApplicationModel } from '../models/application/application' | |
11 | import { AvatarModel } from '../models/avatar/avatar' | |
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 { VideoCommentModel } from '../models/video/video-comment' | |
21 | import { VideoFileModel } from '../models/video/video-file' | |
22 | import { VideoShareModel } from '../models/video/video-share' | |
23 | import { VideoTagModel } from '../models/video/video-tag' | |
24 | import { CONFIG } from './constants' | |
25 | import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update' | |
26 | import { VideoCaptionModel } from '../models/video/video-caption' | |
27 | ||
28 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string | |
29 | ||
30 | const dbname = CONFIG.DATABASE.DBNAME | |
31 | const username = CONFIG.DATABASE.USERNAME | |
32 | const password = CONFIG.DATABASE.PASSWORD | |
33 | const host = CONFIG.DATABASE.HOSTNAME | |
34 | const port = CONFIG.DATABASE.PORT | |
35 | ||
36 | const sequelizeTypescript = new SequelizeTypescript({ | |
37 | database: dbname, | |
38 | dialect: 'postgres', | |
39 | host, | |
40 | port, | |
41 | username, | |
42 | password, | |
43 | benchmark: isTestInstance(), | |
44 | isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE, | |
45 | operatorsAliases: false, | |
46 | logging: (message: string, benchmark: number) => { | |
47 | if (process.env.NODE_DB_LOG === 'false') return | |
48 | ||
49 | let newMessage = message | |
50 | if (isTestInstance() === true && benchmark !== undefined) { | |
51 | newMessage += ' | ' + benchmark + 'ms' | |
52 | } | |
53 | ||
54 | logger.debug(newMessage) | |
55 | } | |
56 | }) | |
57 | ||
58 | async function initDatabaseModels (silent: boolean) { | |
59 | sequelizeTypescript.addModels([ | |
60 | ApplicationModel, | |
61 | ActorModel, | |
62 | ActorFollowModel, | |
63 | AvatarModel, | |
64 | AccountModel, | |
65 | OAuthClientModel, | |
66 | OAuthTokenModel, | |
67 | ServerModel, | |
68 | TagModel, | |
69 | AccountVideoRateModel, | |
70 | UserModel, | |
71 | VideoAbuseModel, | |
72 | VideoChannelModel, | |
73 | VideoShareModel, | |
74 | VideoFileModel, | |
75 | VideoCaptionModel, | |
76 | VideoBlacklistModel, | |
77 | VideoTagModel, | |
78 | VideoModel, | |
79 | VideoCommentModel, | |
80 | ScheduleVideoUpdateModel | |
81 | ]) | |
82 | ||
83 | if (!silent) logger.info('Database %s is ready.', dbname) | |
84 | ||
85 | return | |
86 | } | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | export { | |
91 | initDatabaseModels, | |
92 | sequelizeTypescript | |
93 | } |