diff options
Diffstat (limited to 'server/initializers/database.ts')
-rw-r--r-- | server/initializers/database.ts | 68 |
1 files changed, 53 insertions, 15 deletions
diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 0ab9e98db..1662c1968 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import * as fs from 'fs' | 1 | import * as fs from 'fs' |
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | import * as Sequelize from 'sequelize' | 3 | import * as Sequelize from 'sequelize' |
4 | import { each } from 'async' | ||
4 | 5 | ||
5 | import { CONFIG } from './constants' | 6 | import { CONFIG } from './constants' |
6 | // Do not use barrel, we need to load database first | 7 | // Do not use barrel, we need to load database first |
@@ -72,24 +73,13 @@ const sequelize = new Sequelize(dbname, username, password, { | |||
72 | database.sequelize = sequelize | 73 | database.sequelize = sequelize |
73 | 74 | ||
74 | database.init = function (silent: boolean, callback: (err: Error) => void) { | 75 | database.init = function (silent: boolean, callback: (err: Error) => void) { |
75 | |||
76 | const modelDirectory = join(__dirname, '..', 'models') | 76 | const modelDirectory = join(__dirname, '..', 'models') |
77 | fs.readdir(modelDirectory, function (err, files) { | ||
78 | if (err) throw err | ||
79 | 77 | ||
80 | files.filter(function (file) { | 78 | getModelFiles(modelDirectory, function (err, filePaths) { |
81 | // For all models but not utils.js | 79 | if (err) throw err |
82 | if ( | ||
83 | file === 'index.js' || file === 'index.ts' || | ||
84 | file === 'utils.js' || file === 'utils.ts' || | ||
85 | file.endsWith('-interface.js') || file.endsWith('-interface.ts') || | ||
86 | file.endsWith('.js.map') | ||
87 | ) return false | ||
88 | 80 | ||
89 | return true | 81 | filePaths.forEach(function (filePath) { |
90 | }) | 82 | const model = sequelize.import(filePath) |
91 | .forEach(function (file) { | ||
92 | const model = sequelize.import(join(modelDirectory, file)) | ||
93 | 83 | ||
94 | database[model['name']] = model | 84 | database[model['name']] = model |
95 | }) | 85 | }) |
@@ -111,3 +101,51 @@ database.init = function (silent: boolean, callback: (err: Error) => void) { | |||
111 | export { | 101 | export { |
112 | database | 102 | database |
113 | } | 103 | } |
104 | |||
105 | // --------------------------------------------------------------------------- | ||
106 | |||
107 | function getModelFiles (modelDirectory: string, callback: (err: Error, filePaths: string[]) => void) { | ||
108 | fs.readdir(modelDirectory, function (err, files) { | ||
109 | if (err) throw err | ||
110 | |||
111 | const directories = files.filter(function (directory) { | ||
112 | // For all models but not utils.js | ||
113 | if ( | ||
114 | directory === 'index.js' || directory === 'index.ts' || | ||
115 | directory === 'utils.js' || directory === 'utils.ts' | ||
116 | ) return false | ||
117 | |||
118 | return true | ||
119 | }) | ||
120 | |||
121 | let modelFilePaths: string[] = [] | ||
122 | |||
123 | // For each directory we read it and append model in the modelFilePaths array | ||
124 | each(directories, function (directory: string, eachCallback: ErrorCallback<Error>) { | ||
125 | const modelDirectoryPath = join(modelDirectory, directory) | ||
126 | |||
127 | fs.readdir(modelDirectoryPath, function (err, files) { | ||
128 | if (err) return eachCallback(err) | ||
129 | |||
130 | const filteredFiles = files.filter(file => { | ||
131 | if ( | ||
132 | file === 'index.js' || file === 'index.ts' || | ||
133 | file === 'utils.js' || file === 'utils.ts' || | ||
134 | file.endsWith('-interface.js') || file.endsWith('-interface.ts') || | ||
135 | file.endsWith('.js.map') | ||
136 | ) return false | ||
137 | |||
138 | return true | ||
139 | }).map(file => { | ||
140 | return join(modelDirectoryPath, file) | ||
141 | }) | ||
142 | |||
143 | modelFilePaths = modelFilePaths.concat(filteredFiles) | ||
144 | |||
145 | return eachCallback(null) | ||
146 | }) | ||
147 | }, function(err: Error) { | ||
148 | return callback(err, modelFilePaths) | ||
149 | }) | ||
150 | }) | ||
151 | } | ||