Skip to content

Commit

Permalink
Merge pull request #34 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] removeAttributes
  • Loading branch information
scott-wyatt authored Mar 6, 2019
2 parents e96cc3e + 4733711 commit 8ebf473
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/SequelizeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions lib/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -215,6 +215,7 @@ export const Transformer = {
return store_config[n]
})
]
return plugs
},

/**
Expand All @@ -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)
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions 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.4",
"version": "1.6.5",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,7 +199,8 @@ const App = {
}
},
testModel,
testModel2
testModel2,
testModel3
}
},
config: {
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/testmodel3.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
4 changes: 4 additions & 0 deletions test/unit/lib/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 8ebf473

Please sign in to comment.