Skip to content

Commit

Permalink
Merge pull request #39 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] unified global sequelize
  • Loading branch information
scott-wyatt authored Mar 28, 2019
2 parents ba0adb3 + 2a96361 commit 4a23cd7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/SequelizeSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export class SequelizeSpool extends DatastoreSpool {
configure() {
this._plugins = Transformer.getPlugins(this.app)
// Holds a collection of the connections made through Sequelize
this._connections = Transformer.getConnections(this.app, this.plugins)
this._connections = Transformer.getConnections(this.app, this._datastore, this.plugins)
// Holds a collection of the Sequelize models
this._models = Transformer.getModels(this.app, this.connections)
this._models = Transformer.getModels(this.app, this._datastore, this.connections)
}

/**
Expand Down
46 changes: 23 additions & 23 deletions lib/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as _ from 'lodash'
import * as Sequelize from 'sequelize'
// import * as Sequelize from 'sequelize'
import { FabrixApp } from '@fabrix/fabrix'
import { FabrixModel } from '@fabrix/fabrix/dist/common'
import { pickBy, isString, startsWith } from 'lodash'
Expand Down Expand Up @@ -95,8 +95,8 @@ export const Transformer = {
return props
},

getModelOptions: (app: FabrixApp, model) => {
const config = model.constructor.config(app, Sequelize)
getModelOptions: (app: FabrixApp, sequelize, model) => {
const config = model.constructor.config(app, sequelize)
// Options must be
if (!config.options) {
config.options = {}
Expand All @@ -109,18 +109,18 @@ export const Transformer = {
return config.options
},

getModelSchema: (app: FabrixApp, model) => {
const schema = Transformer.transformSchema(model.constructor.schema(app, Sequelize))
getModelSchema: (app: FabrixApp, sequelize, model) => {
const schema = Transformer.transformSchema(sequelize, model.constructor.schema(app, sequelize))
return schema
},

replaceDataType: (dataType) => {
replaceDataType: (sequelize, dataType) => {
let transformed
try {
Object.keys(Transformer.dataTypes).forEach(type => {
const exp = new RegExp(type)
if (exp.test(dataType)) {
transformed = Sequelize[dataType.replace(exp, Transformer.dataTypes[type])]
transformed = sequelize[dataType.replace(exp, Transformer.dataTypes[type])]
throw Transformer.BreakException
}
})
Expand All @@ -136,11 +136,11 @@ export const Transformer = {
* Transforms Schema to Sequelize method if defined as a string
* Common from Spools built for waterline
*/
transformSchema: (schema) => {
transformSchema: (sequelize, schema) => {
const transformed: {[key: string]: any } = {}
Object.keys(schema).forEach(s => {
if (typeof schema[s] === 'string') {
transformed[s] = Transformer.replaceDataType(schema[s])
transformed[s] = Transformer.replaceDataType(sequelize, schema[s])
}
// else if (
// typeof schema[s] === 'object'
Expand Down Expand Up @@ -174,14 +174,14 @@ export const Transformer = {
return methods
},

defineModel: (app: FabrixApp, model: FabrixModel, connections) => {
defineModel: (app: FabrixApp, sequelize, model: FabrixModel, connections) => {
const modelName = model.constructor.name
const modelConfig = model.config
const store = modelConfig.store || app.config.get('models.defaultStore')
const connection = connections[store]
const migrate = modelConfig.migrate || app.config.get('models.migrate') || connection.migrate
const options = Transformer.getModelOptions(app, model)
const schema = Transformer.getModelSchema(app, model)
const options = Transformer.getModelOptions(app, sequelize, model)
const schema = Transformer.getModelSchema(app, sequelize, model)

if (!model.resolver || !model.resolver.connect) {
throw new Error(`${modelName} was set to use Sequelize but the resolver is missing "connect"`)
Expand Down Expand Up @@ -223,14 +223,14 @@ export const Transformer = {
* @param {Object} app.config.store
* @return {Sequelize} Sequelize instance
*/
createConnectionsFromConfig (app: FabrixApp, config: {[key: string]: any}, plugins: {[key: string]: any} = {}) {
createConnectionsFromConfig (app: FabrixApp, sequelize, config: {[key: string]: any}, plugins: {[key: string]: any} = {}) {
const logger = function(str) {
app.log.debug(str)
}
const plugs = Transformer.definePlugins(app, config.plugins, plugins)

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

// Add plugins
plugs.forEach((plug: any) => {
Expand Down Expand Up @@ -292,27 +292,27 @@ export const Transformer = {
/**
* Transform the FabrixApp "app.config.stores" into a Sequelize object
*/
getConnections (app: FabrixApp, plugins: {[key: string]: any} = {}) {
getConnections (app: FabrixApp, sequelize, plugins: {[key: string]: any} = {}) {
const stores = Transformer.pickStores(app.config.get('stores'))
const sequelize = {}
const _sequelize = {}
Object.keys(stores).forEach(key => {
sequelize[key] = Transformer.createConnectionsFromConfig(app, stores[key], plugins)
sequelize[key].fabrixApp = app
sequelize[key].migrate = stores[key].migrate
sequelize[key].models = {}
_sequelize[key] = Transformer.createConnectionsFromConfig(app, sequelize, stores[key], plugins)
_sequelize[key].fabrixApp = app
_sequelize[key].migrate = stores[key].migrate
_sequelize[key].models = {}
})

return sequelize
return _sequelize
},

/**
* Transform sequelize connections for sequelize models
*/
getModels (app: FabrixApp, connections) {
getModels (app: FabrixApp, sequelize, connections) {
const models = Transformer.pickModels(app, connections)
const sModels = {}
Object.keys(models).forEach(modelName => {
sModels[modelName] = Transformer.defineModel(app, models[modelName], connections).resolver.sequelizeModel
sModels[modelName] = Transformer.defineModel(app, sequelize, models[modelName], connections).resolver.sequelizeModel
})
Transformer.associateModels(app, models, sModels)
return sModels
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-sequelize",
"version": "1.6.9",
"version": "1.6.10",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down
18 changes: 10 additions & 8 deletions test/unit/lib/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const assert = require('assert')
const lib = require('../../../dist/index')
const FabrixApp = require('@fabrix/fabrix').FabrixApp

const Sequelize = require('sequelize')

describe('lib.Transformer', () => {
let app
beforeEach(() => {
Expand All @@ -11,21 +13,21 @@ describe('lib.Transformer', () => {
describe('#getPlugins', () => {
it('should transform properly', () => {
const plugins = lib.Transformer.getPlugins(app)
const connections = lib.Transformer.getConnections(app, plugins)
const models = lib.Transformer.getModels(app, connections)
const connections = lib.Transformer.getConnections(app, Sequelize, plugins)
const models = lib.Transformer.getModels(app, Sequelize, connections)
assert.ok(plugins)
})
})

describe('#getConnections', () => {
it('should transform properly', () => {
const connections = lib.Transformer.getConnections(app)
const connections = lib.Transformer.getConnections(app, Sequelize)

assert(connections.teststore)
assert.equal(connections.storeoverride.options.dialect, 'postgres')
})
it('should transform uri properly', () => {
const connections = lib.Transformer.getConnections(app)
const connections = lib.Transformer.getConnections(app, Sequelize)
assert(connections.uristore)
assert.equal(connections.uristore.options.dialect, 'sqlite')
assert.equal(connections.uristore.options.host, 'testhost')
Expand All @@ -39,12 +41,12 @@ describe('lib.Transformer', () => {
})
describe('#getModels', () => {
it('should transform properly', () => {
const connections = lib.Transformer.getConnections(app)
const models = lib.Transformer.getModels(app, connections)
const connections = lib.Transformer.getConnections(app, Sequelize)
const models = lib.Transformer.getModels(app, Sequelize, connections)
})
it('should not have a primary key for testModel3', () => {
const connections = lib.Transformer.getConnections(app)
const models = lib.Transformer.getModels(app, connections)
const connections = lib.Transformer.getConnections(app, Sequelize)
const models = lib.Transformer.getModels(app, Sequelize, connections)
})
})
})
6 changes: 4 additions & 2 deletions test/unit/services/SchemaMigrationService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const lib = require('../../../dist/index')
const assert = require('assert')
const FabrixApp = require('@fabrix/fabrix').FabrixApp
const TestModel = require('../../fixtures/testmodel')
const Sequelize = require('sequelize')

const App = {
pkg: {
name: 'spool-sequelize-test'
Expand Down Expand Up @@ -45,8 +47,8 @@ describe('api.services.SchemaMigrationService', () => {
beforeEach(() => {
app = new FabrixApp(App)
SchemaMigrationService = app.services.SchemaMigrationService
connections = lib.Transformer.getConnections(app) || {}
models = lib.Transformer.getModels(app, connections) || {}
connections = lib.Transformer.getConnections(app, Sequelize) || {}
models = lib.Transformer.getModels(app, Sequelize, connections) || {}

it('should exist', () => {
assert(SchemaMigrationService)
Expand Down

0 comments on commit 4a23cd7

Please sign in to comment.