mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-17 10:39:09 +00:00
lerna: split up bulk of packages
This commit is contained in:
parent
cb0b1d2410
commit
47a2e5694e
90 changed files with 0 additions and 0 deletions
47
packages/roleypoly-server/models/index.js
Normal file
47
packages/roleypoly-server/models/index.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
// @flow
|
||||
import logger from '../logger'
|
||||
import glob from 'glob'
|
||||
import path from 'path'
|
||||
import type Sequelize, { Model } from 'sequelize'
|
||||
|
||||
const log = logger(__filename)
|
||||
|
||||
export type Models = {
|
||||
[modelName: string]: Model<any> & {
|
||||
__associations: (models: Models) => void,
|
||||
__instanceMethods: (model: Model<any>) => void,
|
||||
}
|
||||
}
|
||||
|
||||
export default (sql: Sequelize): Models => {
|
||||
const models: Models = {}
|
||||
const modelFiles = glob.sync('./models/**/!(index).js')
|
||||
log.debug('found models', modelFiles)
|
||||
|
||||
modelFiles.forEach((v) => {
|
||||
let name = path.basename(v).replace('.js', '')
|
||||
if (v === './models/index.js') {
|
||||
log.debug('index.js hit, skipped')
|
||||
return
|
||||
}
|
||||
try {
|
||||
log.debug('importing..', v.replace('models/', ''))
|
||||
let model = sql.import(v.replace('models/', ''))
|
||||
models[name] = model
|
||||
} catch (err) {
|
||||
log.fatal('error importing model ' + v, err)
|
||||
process.exit(-1)
|
||||
}
|
||||
})
|
||||
|
||||
Object.keys(models).forEach((v) => {
|
||||
if (models[v].hasOwnProperty('__associations')) {
|
||||
models[v].__associations(models)
|
||||
}
|
||||
if (models[v].hasOwnProperty('__instanceMethods')) {
|
||||
models[v].__instanceMethods(models[v])
|
||||
}
|
||||
})
|
||||
|
||||
return models
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue