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