Skip to content

Commit

Permalink
Merge pull request #7 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[feat] add "app" to sequelize instances.
  • Loading branch information
scott-wyatt authored Aug 1, 2018
2 parents bdb25aa + 86199e3 commit 45a80bc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/SequelizeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ 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)
// Add a copy of the Fabrix app to the connection model
this._sequelizeModel.app = this.app

// A helpful exposure of the instance of Sequelize being used
this._sequelize = this._sequelizeModel.sequelize
this.model.datastore = this.model['sequelize'] = this.datastore

// Get the instance methods
const instanceMethods = Transformer.getModelPrototypes(this.model)
const classMethods = Transformer.getModelMethods(this.model, instanceMethods)

Expand All @@ -63,10 +67,13 @@ export class SequelizeResolver extends FabrixResolver {
this._sequelizeModel.prototype[i] = instanceMethods[i]
})

// Attach Fabrix to the instance prototype
this._sequelizeModel.prototype.app = this.app

// Add this model to the connection.models for use later
this._connection.models[modelName] = this._sequelizeModel

// Bind the new methods to the models
// Bind the new methods to the Fabrix model
const resolverMethods = Transformer.getClassMethods(this)
Object.entries(resolverMethods).forEach(([ _, method]: [any, string]) => {
this.model[method] = this[method].bind(this)
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.1.3",
"version": "1.1.4",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down
4 changes: 3 additions & 1 deletion test/integrations/spool.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const assert = require('assert')
const Transformer = require('../../dist/transformer').Transformer

describe('Spool', () => {
let spool
Expand Down Expand Up @@ -40,7 +41,8 @@ describe('Spool', () => {
it('should access a instanceLevelMethod', (done) => {
const instance = global.app.models.testModel.build({name: 'test'})
assert.equal(instance.instanceLevelMethod(), 'bar')
// assert.ok(instance.app)
assert.ok(instance.app)
assert.ok(instance.app.config.get('main'))
done()
})

Expand Down

0 comments on commit 45a80bc

Please sign in to comment.