From 473371193b54c35cde7c2e0781e4fbcd14a43134 Mon Sep 17 00:00:00 2001 From: scott-wyatt Date: Wed, 6 Mar 2019 15:58:07 -0600 Subject: [PATCH] [feat] removeAttributes --- lib/SequelizeResolver.ts | 14 ++++++++++++++ lib/transformer.ts | 12 ++++++++---- package-lock.json | 6 +++--- package.json | 2 +- test/fixtures/app.js | 5 ++++- test/fixtures/testmodel3.js | 32 +++++++++++++++++++++++++++++++ test/unit/lib/transformer.test.js | 4 ++++ 7 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/testmodel3.js diff --git a/lib/SequelizeResolver.ts b/lib/SequelizeResolver.ts index a9e557f..cca1846 100644 --- a/lib/SequelizeResolver.ts +++ b/lib/SequelizeResolver.ts @@ -46,6 +46,20 @@ export class SequelizeResolver extends FabrixResolver { public connect(modelName, schema, options) { // Define the Sequelize Connection on the provided connection this._sequelizeModel = this._connection.define(modelName, schema, options) + + // Special shim to make removing the primary key easier if not needed + if (options.primaryKey === false) { + this._sequelizeModel.removeAttribute('id') + } + + // Special to make it easy to remove attributes created by Sequelize + // Why this isn't just part of Sequelize? Who knows. + if (options.removeAttributes && options.removeAttributes.length > 0) { + options.removeAttributes.forEach(a => { + this._sequelizeModel.removeAttribute(a) + }) + } + // Add a copy of the Fabrix app to the connection model this._sequelizeModel.app = this.app diff --git a/lib/transformer.ts b/lib/transformer.ts index 715a202..92bcf9a 100755 --- a/lib/transformer.ts +++ b/lib/transformer.ts @@ -205,7 +205,7 @@ export const Transformer = { return n } }) - return [ + const plugs = [ ...global_plugins.map(n => { app.log.debug(`Defining Global Sequelize Plugin ${n}`) return plugins[n] @@ -215,6 +215,7 @@ export const Transformer = { return store_config[n] }) ] + return plugs }, /** @@ -228,10 +229,13 @@ export const Transformer = { } const plugs = Transformer.definePlugins(app, config.plugins, plugins) + // Make a copy so plugins don't collide on multiple stores + const Seq = Sequelize + // Add plugins plugs.forEach(plug => { try { - plug(Sequelize) + plug(Seq) } catch (err) { app.log.error(err) @@ -240,10 +244,10 @@ export const Transformer = { if (config.uri) { // Sequelize modify options object - return new Sequelize(config.uri, Object.assign({}, { logging: logger }, config)) + return new Seq(config.uri, Object.assign({}, { logging: logger }, config)) } else { - return new Sequelize( + return new Seq( config.database, config.username || process.env.POSTGRES_USER, config.password || process.env.POSTGRES_PASSWORD, diff --git a/package-lock.json b/package-lock.json index 140958e..727bdbf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.4", + "version": "1.6.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2402,7 +2402,7 @@ "dependencies": { "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true } @@ -2410,7 +2410,7 @@ }, "readable-stream": { "version": "2.3.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "dev": true, "requires": { diff --git a/package.json b/package.json index 2e1f687..a20d8f8 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.4", + "version": "1.6.5", "description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com", "scripts": { "build": "tsc -p ./lib/tsconfig.release.json", diff --git a/test/fixtures/app.js b/test/fixtures/app.js index 9050f34..6046df8 100755 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -4,6 +4,8 @@ const _ = require('lodash') const smokesignals = require('smokesignals') const testModel = require('./testmodel') const testModel2 = require('./testmodel2') +const testModel3 = require('./testmodel3') + const SequelizeResolver = require('../../dist/index').SequelizeResolver const Model = require('@fabrix/fabrix/dist/common').FabrixModel @@ -197,7 +199,8 @@ const App = { } }, testModel, - testModel2 + testModel2, + testModel3 } }, config: { diff --git a/test/fixtures/testmodel3.js b/test/fixtures/testmodel3.js new file mode 100644 index 0000000..4dfa06d --- /dev/null +++ b/test/fixtures/testmodel3.js @@ -0,0 +1,32 @@ +const SequelizeResolver = require('../../dist/index').SequelizeResolver +const Test = require('./testmodel') + +const Utils = require('../../dist/utils').Utils + +const TestResolver = class TestResolver extends SequelizeResolver { + +} + +module.exports = Test3 = class Test3 extends Test { + static config(app, Sequelize) { + return Utils.mergeConfig(Test.config(app, Sequelize), { + options: { + primaryKey: false + } + }) + } + + static schema(app, Sequelize) { + return Utils.mergeConfig(Test.schema(app, Sequelize), { + name2: {type: Sequelize.STRING, allowNull: false} + }) + } + + static get resolver () { + return TestResolver + } + + classLevelMethod() { + return 'foo' + } +} diff --git a/test/unit/lib/transformer.test.js b/test/unit/lib/transformer.test.js index 7258d55..4052677 100755 --- a/test/unit/lib/transformer.test.js +++ b/test/unit/lib/transformer.test.js @@ -42,5 +42,9 @@ describe('lib.Transformer', () => { const connections = lib.Transformer.getConnections(app) const models = lib.Transformer.getModels(app, connections) }) + it('should not have a primary key for testModel3', () => { + const connections = lib.Transformer.getConnections(app) + const models = lib.Transformer.getModels(app, connections) + }) }) })