From d9f64b09b83c09f26580b5ba4c15a9145d157f92 Mon Sep 17 00:00:00 2001 From: scott-wyatt Date: Sat, 16 Mar 2019 11:24:14 -0500 Subject: [PATCH] [feat] better plugs, model options and schema accessors --- lib/SequelizeResolver.ts | 30 +++++++++++++++++++++++++++++- lib/schemas.ts | 12 +++++++++--- lib/transformer.ts | 12 ++++++++++-- package-lock.json | 2 +- package.json | 2 +- test/fixtures/app.js | 6 +++++- test/fixtures/testPlugin2.js | 8 ++++++++ 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/testPlugin2.js diff --git a/lib/SequelizeResolver.ts b/lib/SequelizeResolver.ts index cca1846..4002a45 100644 --- a/lib/SequelizeResolver.ts +++ b/lib/SequelizeResolver.ts @@ -4,6 +4,8 @@ import { Transformer } from './transformer' export class SequelizeResolver extends FabrixResolver { private _connection + private _options + private _schema private _sequelize private _sequelizeModel @@ -35,6 +37,20 @@ export class SequelizeResolver extends FabrixResolver { return this._sequelize } + /** + * Get options provided to the model when connected + */ + get options() { + return this._options + } + + /** + * Get schema provided to the model when connected + */ + get schame() { + return this._schema + } + get datastore() { return this._sequelize } @@ -44,6 +60,8 @@ export class SequelizeResolver extends FabrixResolver { } public connect(modelName, schema, options) { + this._options = options + // Define the Sequelize Connection on the provided connection this._sequelizeModel = this._connection.define(modelName, schema, options) @@ -248,7 +266,17 @@ export class SequelizeResolver extends FabrixResolver { */ findById(id, options = { }) { if (this._sequelizeModel) { - return this._sequelizeModel.findById(id, options) + this.app.log.info('findById is deprecated, use findByPk instead') + return this._sequelizeModel.findByPk(id, options) + } + } + + /** + * + */ + findByPk(id, options = { }) { + if (this._sequelizeModel) { + return this._sequelizeModel.findByPk(id, options) } } diff --git a/lib/schemas.ts b/lib/schemas.ts index bf57ad9..8fb09fd 100755 --- a/lib/schemas.ts +++ b/lib/schemas.ts @@ -2,9 +2,15 @@ const joi = require('joi') export const Schemas = { - storesConfig: joi.object(), - modelsConfig: joi.object(), - pluginsConfig: joi.object(), + storesConfig: joi.object().keys({ + + }).unknown(), + modelsConfig: joi.object().keys({ + + }).unknown(), + pluginsConfig: joi.object().keys({ + + }).unknown(), models: joi.object().keys({ autoPK: joi.boolean(), diff --git a/lib/transformer.ts b/lib/transformer.ts index 92bcf9a..bef9795 100755 --- a/lib/transformer.ts +++ b/lib/transformer.ts @@ -233,9 +233,17 @@ export const Transformer = { const Seq = Sequelize // Add plugins - plugs.forEach(plug => { + plugs.forEach((plug: any) => { try { - plug(Seq) + if (typeof plug === 'function') { + plug(Seq) + } + else if (typeof plug === 'object' && plug.func && plug.config) { + plug.func(Seq, plug.config) + } + else { + app.log.debug(`Transformer: ${plug} was not a function or Fabrix sequelize object`) + } } catch (err) { app.log.error(err) diff --git a/package-lock.json b/package-lock.json index 727bdbf..4319677 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.5", + "version": "1.6.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a20d8f8..453b872 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-sequelize", - "version": "1.6.5", + "version": "1.6.6", "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 6046df8..c129a4e 100755 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -224,7 +224,11 @@ const App = { host: '127.0.0.1', dialect: 'postgres', plugins: { - test_local: require('./testPlugin') + test_local: require('./testPlugin'), + test_local_config: { + func: require('./testPlugin2'), + config: {} + } } }, storeoverride: { diff --git a/test/fixtures/testPlugin2.js b/test/fixtures/testPlugin2.js new file mode 100644 index 0000000..1d8e36d --- /dev/null +++ b/test/fixtures/testPlugin2.js @@ -0,0 +1,8 @@ +module.exports = function(Sequelize, config) { + if (!Sequelize) { + Sequelize = require('sequelize') + } + + // return Sequelize + return Sequelize +}