Add account avatar
[oweals/peertube.git] / server / initializers / database.ts
index 141566c3ae480d9a7881044f48bd31048bbea6e3..bb95992e1334ddb0f27a0019036a00790ce25d52 100644 (file)
@@ -2,7 +2,7 @@ import { join } from 'path'
 import { flattenDepth } from 'lodash'
 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
 import * as Sequelize from 'sequelize'
-import * as Bluebird from 'bluebird'
+import { AvatarModel } from '../models/avatar'
 
 import { CONFIG } from './constants'
 // Do not use barrel, we need to load database first
@@ -15,48 +15,49 @@ import { BlacklistedVideoModel } from './../models/video/video-blacklist-interfa
 import { VideoFileModel } from './../models/video/video-file-interface'
 import { VideoAbuseModel } from './../models/video/video-abuse-interface'
 import { VideoChannelModel } from './../models/video/video-channel-interface'
-import { UserModel } from './../models/user/user-interface'
-import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
+import { UserModel } from '../models/account/user-interface'
+import { AccountVideoRateModel } from '../models/account/account-video-rate-interface'
+import { AccountFollowModel } from '../models/account/account-follow-interface'
 import { TagModel } from './../models/video/tag-interface'
-import { RequestModel } from './../models/request/request-interface'
-import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
-import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
-import { RequestToPodModel } from './../models/request/request-to-pod-interface'
-import { PodModel } from './../models/pod/pod-interface'
+import { ServerModel } from '../models/server/server-interface'
 import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
 import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
 import { JobModel } from './../models/job/job-interface'
-import { AuthorModel } from './../models/video/author-interface'
+import { AccountModel } from './../models/account/account-interface'
 import { ApplicationModel } from './../models/application/application-interface'
+import { VideoChannelShareModel } from '../models/video/video-channel-share-interface'
+import { VideoShareModel } from '../models/video/video-share-interface'
 
 const dbname = CONFIG.DATABASE.DBNAME
 const username = CONFIG.DATABASE.USERNAME
 const password = CONFIG.DATABASE.PASSWORD
 
-const database: {
+export type PeerTubeDatabase = {
   sequelize?: Sequelize.Sequelize,
   init?: (silent: boolean) => Promise<void>,
 
   Application?: ApplicationModel,
-  Author?: AuthorModel,
+  Avatar?: AvatarModel,
+  Account?: AccountModel,
   Job?: JobModel,
   OAuthClient?: OAuthClientModel,
   OAuthToken?: OAuthTokenModel,
-  Pod?: PodModel,
-  RequestToPod?: RequestToPodModel,
-  RequestVideoEvent?: RequestVideoEventModel,
-  RequestVideoQadu?: RequestVideoQaduModel,
-  Request?: RequestModel,
+  Server?: ServerModel,
   Tag?: TagModel,
-  UserVideoRate?: UserVideoRateModel,
+  AccountVideoRate?: AccountVideoRateModel,
+  AccountFollow?: AccountFollowModel,
   User?: UserModel,
   VideoAbuse?: VideoAbuseModel,
   VideoChannel?: VideoChannelModel,
+  VideoChannelShare?: VideoChannelShareModel,
+  VideoShare?: VideoShareModel,
   VideoFile?: VideoFileModel,
   BlacklistedVideo?: BlacklistedVideoModel,
   VideoTag?: VideoTagModel,
   Video?: VideoModel
-} = {}
+}
+
+const database: PeerTubeDatabase = {}
 
 const sequelize = new Sequelize(dbname, username, password, {
   dialect: 'postgres',
@@ -67,6 +68,8 @@ const sequelize = new Sequelize(dbname, username, password, {
   operatorsAliases: false,
 
   logging: (message: string, benchmark: number) => {
+    if (process.env.NODE_DB_LOG === 'false') return
+
     let newMessage = message
     if (isTestInstance() === true && benchmark !== undefined) {
       newMessage += ' | ' + benchmark + 'ms'
@@ -96,7 +99,12 @@ database.init = async (silent: boolean) => {
 
   for (const modelName of Object.keys(database)) {
     if ('associate' in database[modelName]) {
-      database[modelName].associate(database)
+      try {
+        database[modelName].associate(database)
+      } catch (err) {
+        logger.error('Cannot associate model %s.', modelName, err)
+        process.exit(0)
+      }
     }
   }
 
@@ -126,7 +134,7 @@ async function getModelFiles (modelDirectory: string) {
     return true
   })
 
-  const tasks: Bluebird<any>[] = []
+  const tasks: Promise<any>[] = []
 
   // For each directory we read it and append model in the modelFilePaths array
   for (const directory of directories) {