From 2a96361f20693a5e5650c558b38e2478b2932428 Mon Sep 17 00:00:00 2001 From: scott-wyatt Date: Thu, 28 Mar 2019 09:40:17 -0500 Subject: [PATCH] [feat] unified global sequelize --- lib/SequelizeSpool.ts | 4 +- lib/transformer.ts | 46 +++++++++---------- package-lock.json | 2 +- package.json | 2 +- test/unit/lib/transformer.test.js | 18 ++++---- .../services/SchemaMigrationService.test.js | 6 ++- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/SequelizeSpool.ts b/lib/SequelizeSpool.ts index 531644c..009d298 100755 --- a/lib/SequelizeSpool.ts +++ b/lib/SequelizeSpool.ts @@ -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) } /** diff --git a/lib/transformer.ts b/lib/transformer.ts index 7c1d5ae..5649f12 100755 --- a/lib/transformer.ts +++ b/lib/transformer.ts @@ -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' @@ -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 = {} @@ -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 } }) @@ -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' @@ -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"`) @@ -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) => { @@ -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 diff --git a/package-lock.json b/package-lock.json index a9e53b3..3dd5107 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.9", + "version": "1.6.10", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f85f5d3..8162d41 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/lib/transformer.test.js b/test/unit/lib/transformer.test.js index 4052677..879f975 100755 --- a/test/unit/lib/transformer.test.js +++ b/test/unit/lib/transformer.test.js @@ -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(() => { @@ -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') @@ -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) }) }) }) diff --git a/test/unit/services/SchemaMigrationService.test.js b/test/unit/services/SchemaMigrationService.test.js index 80cd0b9..31c8b59 100644 --- a/test/unit/services/SchemaMigrationService.test.js +++ b/test/unit/services/SchemaMigrationService.test.js @@ -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' @@ -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)