Skip to content

Commit

Permalink
Merge pull request #35 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] better plugs, model options and schema accessors
  • Loading branch information
scott-wyatt authored Mar 16, 2019
2 parents 8ebf473 + d9f64b0 commit d27675c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
30 changes: 29 additions & 1 deletion lib/SequelizeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Transformer } from './transformer'

export class SequelizeResolver extends FabrixResolver {
private _connection
private _options
private _schema
private _sequelize
private _sequelizeModel

Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand Down Expand Up @@ -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)
}
}

Expand Down
12 changes: 9 additions & 3 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 10 additions & 2 deletions lib/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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.6.5",
"version": "1.6.6",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/testPlugin2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(Sequelize, config) {
if (!Sequelize) {
Sequelize = require('sequelize')
}

// return Sequelize
return Sequelize
}

0 comments on commit d27675c

Please sign in to comment.