aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/database.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers/database.ts')
-rw-r--r--server/initializers/database.ts90
1 files changed, 43 insertions, 47 deletions
diff --git a/server/initializers/database.ts b/server/initializers/database.ts
index ea2b68f59..ade72b62f 100644
--- a/server/initializers/database.ts
+++ b/server/initializers/database.ts
@@ -2,7 +2,7 @@ import { join } from 'path'
2import { flattenDepth } from 'lodash' 2import { flattenDepth } from 'lodash'
3require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string 3require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
4import * as Sequelize from 'sequelize' 4import * as Sequelize from 'sequelize'
5import * as Promise from 'bluebird' 5import * as Bluebird from 'bluebird'
6 6
7import { CONFIG } from './constants' 7import { CONFIG } from './constants'
8// Do not use barrel, we need to load database first 8// Do not use barrel, we need to load database first
@@ -77,26 +77,26 @@ const sequelize = new Sequelize(dbname, username, password, {
77 77
78database.sequelize = sequelize 78database.sequelize = sequelize
79 79
80database.init = (silent: boolean) => { 80database.init = async (silent: boolean) => {
81 const modelDirectory = join(__dirname, '..', 'models') 81 const modelDirectory = join(__dirname, '..', 'models')
82 82
83 return getModelFiles(modelDirectory).then(filePaths => { 83 const filePaths = await getModelFiles(modelDirectory)
84 filePaths.forEach(filePath => {
85 const model = sequelize.import(filePath)
86 84
87 database[model['name']] = model 85 for (const filePath of filePaths) {
88 }) 86 const model = sequelize.import(filePath)
89 87
90 Object.keys(database).forEach(modelName => { 88 database[model['name']] = model
91 if ('associate' in database[modelName]) { 89 }
92 database[modelName].associate(database)
93 }
94 })
95 90
96 if (!silent) logger.info('Database %s is ready.', dbname) 91 for (const modelName of Object.keys(database)) {
92 if ('associate' in database[modelName]) {
93 database[modelName].associate(database)
94 }
95 }
97 96
98 return undefined 97 if (!silent) logger.info('Database %s is ready.', dbname)
99 }) 98
99 return undefined
100} 100}
101 101
102// --------------------------------------------------------------------------- 102// ---------------------------------------------------------------------------
@@ -107,31 +107,29 @@ export {
107 107
108// --------------------------------------------------------------------------- 108// ---------------------------------------------------------------------------
109 109
110function getModelFiles (modelDirectory: string) { 110async function getModelFiles (modelDirectory: string) {
111 return readdirPromise(modelDirectory) 111 const files = await readdirPromise(modelDirectory)
112 .then(files => { 112 const directories = files.filter(directory => {
113 const directories: string[] = files.filter(directory => { 113 // Find directories
114 // Find directories 114 if (
115 if ( 115 directory.endsWith('.js.map') ||
116 directory.endsWith('.js.map') || 116 directory === 'index.js' || directory === 'index.ts' ||
117 directory === 'index.js' || directory === 'index.ts' || 117 directory === 'utils.js' || directory === 'utils.ts'
118 directory === 'utils.js' || directory === 'utils.ts' 118 ) return false
119 ) return false 119
120 120 return true
121 return true 121 })
122 })
123 122
124 return directories 123 const tasks: Bluebird<any>[] = []
125 })
126 .then(directories => {
127 const tasks = []
128 124
129 // For each directory we read it and append model in the modelFilePaths array 125 // For each directory we read it and append model in the modelFilePaths array
130 directories.forEach(directory => { 126 for (const directory of directories) {
131 const modelDirectoryPath = join(modelDirectory, directory) 127 const modelDirectoryPath = join(modelDirectory, directory)
132 128
133 const promise = readdirPromise(modelDirectoryPath).then(files => { 129 const promise = readdirPromise(modelDirectoryPath)
134 const filteredFiles = files.filter(file => { 130 .then(files => {
131 const filteredFiles = files
132 .filter(file => {
135 if ( 133 if (
136 file === 'index.js' || file === 'index.ts' || 134 file === 'index.js' || file === 'index.ts' ||
137 file === 'utils.js' || file === 'utils.ts' || 135 file === 'utils.js' || file === 'utils.ts' ||
@@ -140,17 +138,15 @@ function getModelFiles (modelDirectory: string) {
140 ) return false 138 ) return false
141 139
142 return true 140 return true
143 }).map(file => join(modelDirectoryPath, file)) 141 })
144 142 .map(file => join(modelDirectoryPath, file))
145 return filteredFiles
146 })
147 143
148 tasks.push(promise) 144 return filteredFiles
149 }) 145 })
150 146
151 return Promise.all(tasks) 147 tasks.push(promise)
152 }) 148 }
153 .then((filteredFiles: string[][]) => { 149
154 return flattenDepth<string>(filteredFiles, 1) 150 const filteredFilesArray: string[][] = await Promise.all(tasks)
155 }) 151 return flattenDepth<string>(filteredFilesArray, 1)
156} 152}