Skip to content

Commit cfb92ec

Browse files
authored
Merge pull request #27 from swarthy/connectionString
Support connection string in store config
2 parents 887bacc + a8dc218 commit cfb92ec

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

lib/transformer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,28 @@ module.exports = {
3535
})
3636
},
3737

38+
/**
39+
* Create Sequelize object based on config options
40+
* @param {Object} config Trails.js store
41+
* @return {Sequelize} Sequelize instance
42+
*/
43+
createFromConfig (config) {
44+
if (config.uri) {
45+
return new Sequelize(config.uri, _.clone(config)) // Sequelize modify options object
46+
}
47+
else {
48+
return new Sequelize(config.database, config.username, config.password, config)
49+
}
50+
},
51+
3852
/**
3953
* Transform the Trails.js "stores" config into a Sequelize object
4054
*/
4155
transformStores (app) {
4256
const stores = app.config.database.stores
4357
const sequelize = {}
4458
Object.keys(stores).forEach(key => {
45-
sequelize[key] = new Sequelize(stores[key].database, stores[key].username, stores[key].password, stores[key])
59+
sequelize[key] = this.createFromConfig(stores[key])
4660
sequelize[key].trailsApp = app
4761
})
4862

test/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ module.exports = _.defaultsDeep({
193193
dialect: 'sqlite',
194194
storage: './test/test.sqlite',
195195
database: 'test'
196+
},
197+
uristore: {
198+
uri: 'sqlite://testuser:password@testhost:1234/testdb'
196199
}
197200
},
198201
models: {

test/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const TrailsApp = require('trails')
44

5-
before(() => {
5+
before(function () {
6+
this.timeout(5000)
67
global.app = new TrailsApp(require('./app'))
78
return global.app.start().catch(global.app.stop)
89
})

test/lib/transformer.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ describe('lib.Transformer', () => {
3232
assert(connections.teststore)
3333
assert.equal(connections.storeoverride.options.dialect, 'sqlite')
3434
})
35+
it('should transform uri properly', () => {
36+
const connections = lib.Transformer.transformStores(global.app)
37+
assert(connections.uristore)
38+
assert.equal(connections.uristore.options.dialect, 'sqlite')
39+
assert.equal(connections.uristore.options.host, 'testhost')
40+
assert.equal(connections.uristore.config.host, 'testhost')
41+
assert.equal(connections.uristore.config.database, 'testdb')
42+
// test config for other dialects
43+
assert.equal(connections.uristore.config.port, 1234)
44+
assert.equal(connections.uristore.config.username, 'testuser')
45+
assert.equal(connections.uristore.config.password, 'password')
46+
})
3547
})
3648

3749
})

0 commit comments

Comments
 (0)