Skip to content

Commit f3a780b

Browse files
jonathonwalzpaparomeo
authored andcommitted
Add option to inject sequelize instance
In order to allow more seamless usage of the database instance or more customized configurations, this adds a configuration option of 'sequelize' that will use that instance instead of creating a new instance.
1 parent ab08448 commit f3a780b

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ jsonApi.define({
4141

4242
**Note:** the `logging` property controls the logging of the emitted SQL and can either be `false` (which will mean it will be captured by the internal debugging module under the namespace `jsonApi:store:relationaldb:sequelize`) or a user provided function (e.g. `console.log`) to which a string containing the information to be logged will be passed as the first argument.
4343

44+
#### Alternative Usage - Provide Sequelize instance
45+
46+
If you are already using sequelize or need to have access to the sequelize instance, you may provide an instance to the store to be used instead of having the store create a new instance from the given config.
47+
48+
```javascript
49+
var RelationalDbStore = require("jsonapi-store-relationaldb");
50+
var Sequelize = require("Sequelize");
51+
52+
var sequelize = new Sequelize("jsonapi", "root", null, {dialect: "mysql"}));
53+
54+
jsonApi.define({
55+
resource: "comments",
56+
handlers: new RelationalDbStore({
57+
sequelize: sequelize
58+
})
59+
});
60+
```
61+
4462
### Features
4563

4664
* Search, Find, Create, Delete, Update

lib/sqlHandler.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,39 @@ SqlStore.prototype.initialise = function(resourceConfig) {
4444
var self = this;
4545
self.resourceConfig = resourceConfig;
4646

47-
var database = self.config.database || resourceConfig.resource;
48-
var sequelizeArgs = [database, self.config.username, self.config.password, {
49-
dialect: self.config.dialect,
50-
dialectOptions: self.config.dialectOptions,
51-
host: self.config.host,
52-
port: self.config.port,
53-
storage: self.config.storage || ":memory:",
54-
logging: self.config.logging || require("debug")("jsonApi:store:relationaldb:sequelize"),
55-
define: {
56-
// Set freezeTableName on all table definitions to prevent sequelize from pluralizing
57-
// all table names by itself.
58-
freezeTableName: true
59-
}
60-
}];
47+
if (self.config.sequelize) {
48+
self.sequelize = self.config.sequelize;
49+
} else {
50+
var database = self.config.database || resourceConfig.resource;
51+
var sequelizeArgs = [database, self.config.username, self.config.password, {
52+
dialect: self.config.dialect,
53+
dialectOptions: self.config.dialectOptions,
54+
host: self.config.host,
55+
port: self.config.port,
56+
storage: self.config.storage || ":memory:",
57+
logging: self.config.logging || require("debug")("jsonApi:store:relationaldb:sequelize"),
58+
define: {
59+
// Set freezeTableName on all table definitions to prevent sequelize from pluralizing
60+
// all table names by itself.
61+
freezeTableName: true
62+
}
63+
}];
6164

62-
// To prevent too many open connections, we will store all Sequelize instances in a hash map.
63-
// Index the hash map by a hash of the entire config object. If the same config is passed again,
64-
// reuse the existing Sequelize connection resource instead of opening a new one.
65+
// To prevent too many open connections, we will store all Sequelize instances in a hash map.
66+
// Index the hash map by a hash of the entire config object. If the same config is passed again,
67+
// reuse the existing Sequelize connection resource instead of opening a new one.
6568

66-
var md5sum = crypto.createHash("md5");
67-
var instanceId = md5sum.update(JSON.stringify(sequelizeArgs)).digest("hex");
68-
var instances = SqlStore._sequelizeInstances;
69+
var md5sum = crypto.createHash("md5");
70+
var instanceId = md5sum.update(JSON.stringify(sequelizeArgs)).digest("hex");
71+
var instances = SqlStore._sequelizeInstances;
6972

70-
if (!instances[instanceId]) {
71-
var sequelize = new (Function.prototype.bind.apply(Sequelize, [null].concat(sequelizeArgs)))();
72-
instances[instanceId] = sequelize;
73+
if (!instances[instanceId]) {
74+
var sequelize = new (Function.prototype.bind.apply(Sequelize, [null].concat(sequelizeArgs)))();
75+
instances[instanceId] = sequelize;
76+
}
77+
self.sequelize = instances[instanceId];
7378
}
7479

75-
self.sequelize = instances[instanceId];
76-
7780
self._buildModels();
7881

7982
self.ready = true;

0 commit comments

Comments
 (0)