]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Create comment on replied mastodon statutes
[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'
12import { JobModel } from '../models/job/job'
13import { OAuthClientModel } from '../models/oauth/oauth-client'
14import { OAuthTokenModel } from '../models/oauth/oauth-token'
15import { ServerModel } from '../models/server/server'
16import { TagModel } from '../models/video/tag'
17import { VideoModel } from '../models/video/video'
18import { VideoAbuseModel } from '../models/video/video-abuse'
19import { VideoBlacklistModel } from '../models/video/video-blacklist'
20import { VideoChannelModel } from '../models/video/video-channel'
6d852470 21import { VideoCommentModel } from '../models/video/video-comment'
3fd3ab2d
C
22import { VideoFileModel } from '../models/video/video-file'
23import { VideoShareModel } from '../models/video/video-share'
24import { VideoTagModel } from '../models/video/video-tag'
65fcc311 25import { CONFIG } from './constants'
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,
64 JobModel,
65 OAuthClientModel,
66 OAuthTokenModel,
67 ServerModel,
68 TagModel,
69 AccountVideoRateModel,
3fd3ab2d
C
70 UserModel,
71 VideoAbuseModel,
72 VideoChannelModel,
3fd3ab2d
C
73 VideoShareModel,
74 VideoFileModel,
75 VideoBlacklistModel,
76 VideoTagModel,
6d852470
C
77 VideoModel,
78 VideoCommentModel
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}