Skip to content

Commit

Permalink
Merge pull request #44 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[fix] #43
  • Loading branch information
scott-wyatt authored Jun 5, 2019
2 parents 0eaab5a + 70f18e6 commit f72bcc4
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 31 deletions.
3 changes: 3 additions & 0 deletions lib/SequelizeSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export class SequelizeSpool extends DatastoreSpool {
* Merge configuration into models, load Sequelize collections.
*/
configure() {
// This set tracks the plugins that are being added to a single sequelize instance
this._datastore['plugins'] = this._datastore['plugins'] || new Set()

this._plugins = Transformer.getPlugins(this.app)
// Holds a collection of the connections made through Sequelize
this._connections = Transformer.getConnections(this.app, this._datastore, this.plugins)
Expand Down
78 changes: 53 additions & 25 deletions lib/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,15 @@ export const Transformer = {
return n
}
})
const plugs = [
...global_plugins.map(n => {
app.log.debug(`Defining Global Sequelize Plugin ${n}`)
return plugins[n]
}),
...store_plugins.map(n => {
app.log.debug(`Defining Local Sequelize Plugin ${n}`)
return store_config[n]
})
]
const plugs = new Map()

global_plugins.forEach(n => {
plugs.set(n, plugins[n])
})
store_plugins.forEach(n => {
plugs.set(n, plugins[n])
})

return plugs
},

Expand All @@ -226,32 +225,61 @@ export const Transformer = {
* @return {Sequelize} Sequelize instance
*/
createConnectionsFromConfig (app: FabrixApp, sequelize, config: {[key: string]: any}, plugins: {[key: string]: any} = {}) {
const logger = function(str) {
app.log.debug(str)
const logger = function(val) {
// https://github.com/sequelize/sequelize/issues/3781#issuecomment-421282703
// If for whatever reason the Sequelize logger exports a Sequelize object, then, this must be done
if (val && val.toJSON && typeof val.toJSON === 'function') {
val = val.toJSON()
}
app.log.debug(val)
}
const plugs = Transformer.definePlugins(app, config.plugins, plugins)

const plugs: Map<string, any> = Transformer.definePlugins(app, config.plugins, plugins)

// Make a copy so plugins don't collide on multiple stores
let Seq = sequelize

// Add plugins
plugs.forEach((plug: any) => {
try {
if (typeof plug === 'function') {
Seq = plug(Seq)
}
else if (typeof plug === 'object' && plug.func && plug.config) {
Seq = plug.func(Seq, plug.config)
plugs.forEach((plug, key, map) => {
if (!sequelize.plugins.has(key)) {
try {
if (typeof plug === 'function') {
Seq = plug(Seq)
}
else if (typeof plug === 'object' && plug.func && plug.config) {
Seq = plug.func(Seq, plug.config)
}
else {
app.log.debug(`Transformer: ${key} ${plug} was not a function or Fabrix sequelize object`)
}
sequelize.plugins.add(key)
}
else {
app.log.debug(`Transformer: ${plug} was not a function or Fabrix sequelize object`)
catch (err) {
app.log.error(err)
}
}
catch (err) {
app.log.error(err)
else {
app.log.debug(`Attempted to add ${ key } as a sequelize instance plugin more than once`)
}
})

// // Add plugins
// plugs.forEach((plug: any) => {
// try {
// if (typeof plug === 'function') {
// Seq = plug(Seq)
// }
// else if (typeof plug === 'object' && plug.func && plug.config) {
// Seq = plug.func(Seq, plug.config)
// }
// else {
// app.log.debug(`Transformer: ${plug} was not a function or Fabrix sequelize object`)
// }
// }
// catch (err) {
// app.log.error(err)
// }
// })

if (config.uri) {
// Sequelize modify options object
return new Seq(config.uri, Object.assign({}, { logging: logger }, config))
Expand Down
Loading

0 comments on commit f72bcc4

Please sign in to comment.