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.ts99
1 files changed, 49 insertions, 50 deletions
diff --git a/server/initializers/database.ts b/server/initializers/database.ts
index 705dec6da..6e3a8d009 100644
--- a/server/initializers/database.ts
+++ b/server/initializers/database.ts
@@ -1,12 +1,12 @@
1import * as fs from 'fs'
2import { join } from 'path' 1import { join } from 'path'
2import { flattenDepth } from 'lodash'
3import * as Sequelize from 'sequelize' 3import * as Sequelize from 'sequelize'
4import { each } from 'async' 4import * as Promise from 'bluebird'
5 5
6import { CONFIG } from './constants' 6import { CONFIG } from './constants'
7// Do not use barrel, we need to load database first 7// Do not use barrel, we need to load database first
8import { logger } from '../helpers/logger' 8import { logger } from '../helpers/logger'
9import { isTestInstance } from '../helpers/core-utils' 9import { isTestInstance, readdirPromise } from '../helpers/core-utils'
10import { 10import {
11 ApplicationModel, 11 ApplicationModel,
12 AuthorModel, 12 AuthorModel,
@@ -33,7 +33,7 @@ const password = CONFIG.DATABASE.PASSWORD
33 33
34const database: { 34const database: {
35 sequelize?: Sequelize.Sequelize, 35 sequelize?: Sequelize.Sequelize,
36 init?: (silent: any, callback: any) => void, 36 init?: (silent: boolean) => Promise<void>,
37 37
38 Application?: ApplicationModel, 38 Application?: ApplicationModel,
39 Author?: AuthorModel, 39 Author?: AuthorModel,
@@ -72,19 +72,17 @@ const sequelize = new Sequelize(dbname, username, password, {
72 72
73database.sequelize = sequelize 73database.sequelize = sequelize
74 74
75database.init = function (silent: boolean, callback: (err: Error) => void) { 75database.init = function (silent: boolean) {
76 const modelDirectory = join(__dirname, '..', 'models') 76 const modelDirectory = join(__dirname, '..', 'models')
77 77
78 getModelFiles(modelDirectory, function (err, filePaths) { 78 return getModelFiles(modelDirectory).then(filePaths => {
79 if (err) throw err 79 filePaths.forEach(filePath => {
80
81 filePaths.forEach(function (filePath) {
82 const model = sequelize.import(filePath) 80 const model = sequelize.import(filePath)
83 81
84 database[model['name']] = model 82 database[model['name']] = model
85 }) 83 })
86 84
87 Object.keys(database).forEach(function (modelName) { 85 Object.keys(database).forEach(modelName => {
88 if ('associate' in database[modelName]) { 86 if ('associate' in database[modelName]) {
89 database[modelName].associate(database) 87 database[modelName].associate(database)
90 } 88 }
@@ -92,7 +90,7 @@ database.init = function (silent: boolean, callback: (err: Error) => void) {
92 90
93 if (!silent) logger.info('Database %s is ready.', dbname) 91 if (!silent) logger.info('Database %s is ready.', dbname)
94 92
95 return callback(null) 93 return undefined
96 }) 94 })
97} 95}
98 96
@@ -104,49 +102,50 @@ export {
104 102
105// --------------------------------------------------------------------------- 103// ---------------------------------------------------------------------------
106 104
107function getModelFiles (modelDirectory: string, callback: (err: Error, filePaths: string[]) => void) { 105function getModelFiles (modelDirectory: string) {
108 fs.readdir(modelDirectory, function (err, files) { 106 return readdirPromise(modelDirectory)
109 if (err) throw err 107 .then(files => {
110 108 const directories: string[] = files.filter(function (directory) {
111 const directories = files.filter(function (directory) { 109 // Find directories
112 // Find directories 110 if (
113 if ( 111 directory.endsWith('.js.map') ||
114 directory.endsWith('.js.map') || 112 directory === 'index.js' || directory === 'index.ts' ||
115 directory === 'index.js' || directory === 'index.ts' || 113 directory === 'utils.js' || directory === 'utils.ts'
116 directory === 'utils.js' || directory === 'utils.ts' 114 ) return false
117 ) return false 115
116 return true
117 })
118 118
119 return true 119 return directories
120 }) 120 })
121 121 .then(directories => {
122 let modelFilePaths: string[] = [] 122 const tasks = []
123 123
124 // For each directory we read it and append model in the modelFilePaths array 124 // For each directory we read it and append model in the modelFilePaths array
125 each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) { 125 directories.forEach(directory => {
126 const modelDirectoryPath = join(modelDirectory, directory) 126 const modelDirectoryPath = join(modelDirectory, directory)
127 127
128 fs.readdir(modelDirectoryPath, function (err, files) { 128 const promise = readdirPromise(modelDirectoryPath).then(files => {
129 if (err) return eachCallback(err) 129 const filteredFiles = files.filter(file => {
130 130 if (
131 const filteredFiles = files.filter(file => { 131 file === 'index.js' || file === 'index.ts' ||
132 if ( 132 file === 'utils.js' || file === 'utils.ts' ||
133 file === 'index.js' || file === 'index.ts' || 133 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
134 file === 'utils.js' || file === 'utils.ts' || 134 file.endsWith('.js.map')
135 file.endsWith('-interface.js') || file.endsWith('-interface.ts') || 135 ) return false
136 file.endsWith('.js.map') 136
137 ) return false 137 return true
138 138 }).map(file => join(modelDirectoryPath, file))
139 return true 139
140 }).map(file => { 140 return filteredFiles
141 return join(modelDirectoryPath, file)
142 }) 141 })
143 142
144 modelFilePaths = modelFilePaths.concat(filteredFiles) 143 tasks.push(promise)
145
146 return eachCallback(null)
147 }) 144 })
148 }, function (err: Error) { 145
149 return callback(err, modelFilePaths) 146 return Promise.all(tasks)
147 })
148 .then((filteredFiles: string[][]) => {
149 return flattenDepth<string>(filteredFiles, 1)
150 }) 150 })
151 })
152} 151}