Skip to content

Commit

Permalink
Merge pull request #32 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
V1.6
  • Loading branch information
scott-wyatt authored Mar 5, 2019
2 parents f103e07 + 0cdeb4c commit d7bf320
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 12 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ export const main = {

A basic `config/store.ts` can be found here : https://github.com/fabrix-app/spool-sequelize/blob/master/lib/archetype/config/database.js

### Plugins
There are 2 ways to define a plugin for Sequelize in spool-sequelize, for all sequelize connections or just for a particular one:

1) Define `plugins` in `config.sequelize`
```js
// config/sequelize.ts
export const sequelize = {
plugins: {
my_global_plugin: require('sequelize-plugin-name')
}
}
```

2) Define a plugins just for a particular store connection
```js
// config/stores.ts
export const stores = {
uristore: {
migrate: 'drop',
orm: 'sequelize',
uri: 'sqlite://testuser:password@testhost:1234/testdb',
plugins: {
my_local_plugin: require('sequelize-plugin-name')
}
}
}
```

### Models

```js
Expand Down
7 changes: 7 additions & 0 deletions archetype/src/config/sequelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Sequelize Configuration
* (app.config.sequelize)
*/
export const sequelize = {
plugins: {}
}
17 changes: 13 additions & 4 deletions lib/SequelizeSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as api from './api/index'
export class SequelizeSpool extends DatastoreSpool {
_datastore = Sequelize

private _plugins: {[key: string]: any} = { }
private _connections: {[key: string]: any} = { }
private _models: {[key: string]: any} = { }

Expand All @@ -26,13 +27,19 @@ export class SequelizeSpool extends DatastoreSpool {
})
}

get models() {
return this._models || {}
get plugins () {
return this._plugins || {}
}

get connections () {
return this._connections || {}
}

get models() {
return this._models || {}
}


/**
* Validate the database config, and api.model definitions
*/
Expand All @@ -55,16 +62,18 @@ export class SequelizeSpool extends DatastoreSpool {
}
return Promise.all([
Validator.validateStoresConfig(stores),
Validator.validateModelsConfig(models)
Validator.validateModelsConfig(models),
Validator.validatePluginsConfig(models)
])
}

/**
* Merge configuration into models, load Sequelize collections.
*/
configure() {
this._plugins = Transformer.getPlugins(this.app)
// Holds a collection of the connections made through Sequelize
this._connections = Transformer.getConnections(this.app)
this._connections = Transformer.getConnections(this.app, this.plugins)
// Holds a collection of the Sequelize models
this._models = Transformer.getModels(this.app, this.connections)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
* (app.config.sequelize)
*/
export const sequelize = {
// Leave the file even empty because if the file is not present, env config is not merged
plugins: {}
}
1 change: 1 addition & 0 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const joi = require('joi')
export const Schemas = {
storesConfig: joi.object(),
modelsConfig: joi.object(),
pluginsConfig: joi.object(),

models: joi.object().keys({
autoPK: joi.boolean(),
Expand Down
45 changes: 42 additions & 3 deletions lib/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,46 @@ export const Transformer = {
return model
},

definePlugins(app: FabrixApp, store_config = {}, plugins = {}) {
const global_plugins = Object.keys(plugins)
const store_plugins = Object.keys(store_config).filter(n => {
if (global_plugins.indexOf(n) === -1) {
return n
}
})
return [
...global_plugins.map(n => {
app.log.debug(`Defining Global Sequelize Plugin ${n}`)
return plugins[n]
}),
...store_plugins.map(n => {
app.log.debug(`Defining Local Sequelize Plugin ${n}`)
return store_config[n]
})
]
},

/**
* Create Sequelize object based on config options
* @param {Object} app.config.store
* @return {Sequelize} Sequelize instance
*/
createConnectionsFromConfig (app: FabrixApp, config: {[key: string]: any}) {
createConnectionsFromConfig (app: FabrixApp, config: {[key: string]: any}, plugins: {[key: string]: any} = {}) {
const logger = function(str) {
app.log.debug(str)
}
const plugs = Transformer.definePlugins(app, config.plugins, plugins)

// Add plugins
plugs.forEach(plug => {
try {
plug(Sequelize)
}
catch (err) {
app.log.error(err)
}
})

if (config.uri) {
// Sequelize modify options object
return new Sequelize(config.uri, Object.assign({}, { logging: logger }, config))
Expand Down Expand Up @@ -249,11 +280,11 @@ export const Transformer = {
/**
* Transform the FabrixApp "app.config.stores" into a Sequelize object
*/
getConnections (app: FabrixApp) {
getConnections (app: FabrixApp, plugins: {[key: string]: any} = {}) {
const stores = Transformer.pickStores(app.config.get('stores'))
const sequelize = {}
Object.keys(stores).forEach(key => {
sequelize[key] = Transformer.createConnectionsFromConfig(app, stores[key])
sequelize[key] = Transformer.createConnectionsFromConfig(app, stores[key], plugins)
sequelize[key].fabrixApp = app
sequelize[key].migrate = stores[key].migrate
sequelize[key].models = {}
Expand Down Expand Up @@ -287,5 +318,13 @@ export const Transformer = {
// Convenience link between model.associations and the sequelize.model.associations
// models[modelName].associations = models[modelName].resolver.sequelizeModel.associations
})
},

/**
* Add Plugins
*/
getPlugins(app: FabrixApp) {
const plugins = app.config.get('sequelize.plugins')
return plugins
}
}
10 changes: 10 additions & 0 deletions lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@ export const Validator = {
return resolve(value)
})
})
},
validatePluginsConfig (config) {
return new Promise((resolve, reject) => {
joi.validate(config, Schemas.pluginsConfig, (err, value) => {
if (err) {
return reject(err)
}
return resolve(value)
})
})
}
}
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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-sequelize",
"version": "1.6.2",
"version": "1.6.3",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down Expand Up @@ -84,6 +84,6 @@
},
"license": "MIT",
"bugs": {
"url": "https://github.com/fabrix-app/spool-tapestries/issues"
"url": "https://github.com/fabrix-app/spool-sequelize/issues"
}
}
10 changes: 9 additions & 1 deletion test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,21 @@ const App = {
require('../../dist/index').SequelizeSpool // spool-sequelize
]
},
sequelize: {
plugins: {
test_global: require('./testPlugin')
}
},
stores: {
teststore: {
migrate: 'drop',
orm: 'sequelize',
database: 'Sequelize',
host: '127.0.0.1',
dialect: 'postgres'
dialect: 'postgres',
plugins: {
test_local: require('./testPlugin')
}
},
storeoverride: {
migrate: 'drop',
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/testPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(Sequelize) {
if (!Sequelize) {
Sequelize = require('sequelize')
}

// return Sequelize
return Sequelize
}
9 changes: 9 additions & 0 deletions test/unit/lib/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ describe('lib.Transformer', () => {
app = new FabrixApp(require('../../fixtures/app'))
})

describe('#getPlugins', () => {
it('should transform properly', () => {
const plugins = lib.Transformer.getPlugins(app)
const connections = lib.Transformer.getConnections(app, plugins)
const models = lib.Transformer.getModels(app, connections)
assert.ok(plugins)
})
})

describe('#getConnections', () => {
it('should transform properly', () => {
const connections = lib.Transformer.getConnections(app)
Expand Down

0 comments on commit d7bf320

Please sign in to comment.