From e40d1fb5829a95b45780ebca516aaf31cb805599 Mon Sep 17 00:00:00 2001 From: scott-wyatt Date: Mon, 15 Apr 2019 21:59:33 -0500 Subject: [PATCH] [feat] parent proto extension --- lib/SequelizeResolver.ts | 9 ++++++++ lib/transformer.ts | 6 ++++-- package-lock.json | 2 +- package.json | 2 +- test/fixtures/app.js | 6 +++++- test/fixtures/testmodelExtend.js | 35 +++++++++++++++++++++++++++++++ test/fixtures/testmodelExtend2.js | 32 ++++++++++++++++++++++++++++ test/integrations/spool.test.js | 21 +++++++++++++++++++ 8 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/testmodelExtend.js create mode 100644 test/fixtures/testmodelExtend2.js diff --git a/lib/SequelizeResolver.ts b/lib/SequelizeResolver.ts index fc51861..898fb27 100644 --- a/lib/SequelizeResolver.ts +++ b/lib/SequelizeResolver.ts @@ -180,6 +180,15 @@ export class SequelizeResolver extends FabrixResolver { } } + /** + * + */ + bulkUpdate(values: any, records: any, options = {}) { + if (this._sequelizeModel) { + return this._sequelizeModel.bulkUpdate(values, records, options) + } + } + /** * */ diff --git a/lib/transformer.ts b/lib/transformer.ts index 5649f12..d69c363 100755 --- a/lib/transformer.ts +++ b/lib/transformer.ts @@ -158,10 +158,12 @@ export const Transformer = { }, /** - * Get the prototypes of a model + * Get the prototypes of a model and it's parent */ getModelPrototypes: (model) => { - return Object.getPrototypeOf(model) + const sup = model.__proto__ ? Object.getPrototypeOf(model.__proto__) : {} + const props = Object.getPrototypeOf(model) + return {...sup, ...props} }, /** diff --git a/package-lock.json b/package-lock.json index 3dd5107..a17aa22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.10", + "version": "1.6.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8162d41..924ac04 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.10", + "version": "1.6.11", "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 c129a4e..e283c79 100755 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -5,6 +5,8 @@ const smokesignals = require('smokesignals') const testModel = require('./testmodel') const testModel2 = require('./testmodel2') const testModel3 = require('./testmodel3') +const testModelExtend = require('./testmodelExtend') +const testModelExtend2 = require('./testmodelExtend2') const SequelizeResolver = require('../../dist/index').SequelizeResolver @@ -200,7 +202,9 @@ const App = { }, testModel, testModel2, - testModel3 + testModel3, + testModelExtend, + testModelExtend2 } }, config: { diff --git a/test/fixtures/testmodelExtend.js b/test/fixtures/testmodelExtend.js new file mode 100644 index 0000000..2a173f6 --- /dev/null +++ b/test/fixtures/testmodelExtend.js @@ -0,0 +1,35 @@ +const Model = require('@fabrix/fabrix/dist/common').FabrixModel +const SequelizeResolver = require('../../dist/index').SequelizeResolver + +const TestResolver = class TestResolver extends SequelizeResolver { + +} + +module.exports = TestExtend = class TestExtend extends Model { + static config(app, Sequelize) { + return { + options: {} + } + } + + static schema(app, Sequelize) { + return { + name: {type: Sequelize.STRING, allowNull: false} + } + } + + static get resolver () { + return TestResolver + } + + classLevelMethod() { + return 'foo' + } +} + +TestExtend.prototype.instanceLevelMethod = function() { + return 'bar' +} +TestExtend.prototype.superLevelMethod = function() { + return 'baf' +} diff --git a/test/fixtures/testmodelExtend2.js b/test/fixtures/testmodelExtend2.js new file mode 100644 index 0000000..6f723dc --- /dev/null +++ b/test/fixtures/testmodelExtend2.js @@ -0,0 +1,32 @@ +const TestExtend = require('./testmodelExtend') +const SequelizeResolver = require('../../dist/index').SequelizeResolver + +const TestResolver = class TestResolver extends SequelizeResolver { + +} + +module.exports = TestExtend2 = class TestExtend2 extends TestExtend { + static config(app, Sequelize) { + return { + options: {} + } + } + + static schema(app, Sequelize) { + return { + name: {type: Sequelize.STRING, allowNull: false} + } + } + + static get resolver () { + return TestResolver + } + + classLevelMethod() { + return 'foo' + } +} +TestExtend2.prototype.instanceLevelMethod = TestExtend.prototype.instanceLevelMethod +TestExtend2.prototype.nextInstanceLevelMethod = function() { + return 'baz' +} diff --git a/test/integrations/spool.test.js b/test/integrations/spool.test.js index 5ab3788..ada7ee7 100755 --- a/test/integrations/spool.test.js +++ b/test/integrations/spool.test.js @@ -46,6 +46,17 @@ describe('Spool', () => { done() }) + + it('should access an inherited instanceLevelMethod', (done) => { + const instance = global.app.models.testModelExtend2.build({name: 'test'}) + assert.equal(instance.nextInstanceLevelMethod(), 'baz') + assert.equal(instance.instanceLevelMethod(), 'bar') + assert.equal(instance.superLevelMethod(), 'baf') + assert.ok(instance.app) + assert.ok(instance.app.config.get('main')) + done() + }) + it('should save an instance', (done) => { const instance = global.app.models.testModel.build({name: 'test'}) instance.save().then(i => { @@ -133,6 +144,16 @@ describe('Spool', () => { done(err) }) }) + // TODO ENABLE IN Sequelize 5.0 + it.skip('should bulk create instances', (done) => { + const instance = global.app.models.testModel.bulkUpdate({name: 'test'}, { name: 'test' }) + .then(i => { + done() + }) + .catch(err => { + done(err) + }) + }) it('should count instances', (done) => { const instance = global.app.models.testModel.count({where: {name: 'test'}})