ah-sequelize-plugin
Advanced tools
Comparing version 0.8.7 to 0.9.0
@@ -13,3 +13,3 @@ var path = require('path'); | ||
api.models = {}; | ||
api.config.sequelize.logging = api.logger.info; | ||
api.config.sequelize.logging = api.log; | ||
var sequelizeInstance = new Sequelize( | ||
@@ -73,10 +73,17 @@ api.config.sequelize.database, | ||
connect: function(next) { | ||
var dir = path.normalize(api.projectRoot + '/models'); | ||
fs.readdirSync(dir).forEach(function(file) { | ||
var nameParts = file.split("/"); | ||
var name = nameParts[(nameParts.length - 1)].split(".")[0]; | ||
var modelFunc = currySchemaFunc(require(dir + '/' + file)); | ||
api.models[name] = api.sequelize.sequelize.import(name, modelFunc); | ||
}); | ||
function importModelsFromDirectory(dir) { | ||
fs.readdirSync(dir).forEach(function(file) { | ||
if (fs.statSync(path.join(dir, file)).isDirectory()) | ||
return importModelsFromDirectory(path.join(dir, file)) | ||
if (path.extname(file) !== '.js') return; | ||
var nameParts = file.split("/"); | ||
var name = nameParts[(nameParts.length - 1)].split(".")[0]; | ||
var modelFunc = currySchemaFunc(require(dir + '/' + file)); | ||
api.sequelize.sequelize.import(name, modelFunc); | ||
}); | ||
} | ||
var dir = path.normalize(api.projectRoot + '/models'); | ||
importModelsFromDirectory(dir); | ||
api.models = api.sequelize.sequelize.models; | ||
api.sequelize.test(next); | ||
@@ -88,3 +95,4 @@ }, | ||
var SequelizeFixtures = require('sequelize-fixtures'); | ||
SequelizeFixtures.loadFile(api.projectRoot + '/test/fixtures/*.{json,yml,js}', api.models, function() { | ||
var options = { log: (api.config.logging)? console.log : function(){}}; | ||
SequelizeFixtures.loadFile(api.projectRoot + '/test/fixtures/*.{json,yml,js}', api.models, options).then(function() { | ||
next(); | ||
@@ -91,0 +99,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"description": "I use sequelize in actionhero as an ORM", | ||
"version": "0.8.7", | ||
"version": "0.9.0", | ||
"homepage": "http://actionherojs.com", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -0,1 +1,2 @@ | ||
![plugin](https://i.imgur.com/nd1btLt.png) | ||
# ah-sequelize-plugin | ||
@@ -5,2 +6,5 @@ | ||
## Notes | ||
Version 0.9.0 has a [breaking change](https://github.com/evantahler/ah-sequelize-plugin/pull/30) regarding model names. | ||
## Setup | ||
@@ -98,5 +102,22 @@ | ||
If you want to declare associations, best practice has you create an `associations.js` initializer within your project which might look like this: | ||
If you want to declare associations, best practice has you define an `.associate()` class method in your model such as: | ||
```javascript | ||
module.exports = function(sequelize, DataTypes) { | ||
var User = sequelize.define("User", { | ||
username: DataTypes.STRING | ||
}, { | ||
classMethods: { | ||
associate: function(models) { | ||
User.hasMany(models.Task) | ||
} | ||
} | ||
}); | ||
return User; | ||
}; | ||
``` | ||
Then you create an `associations.js` initializer within your project which might look like this: | ||
```javascript | ||
module.exports = { | ||
@@ -113,8 +134,7 @@ loadPriority: 1000, | ||
start: function (api, next) { | ||
api.models.user.hasMany(api.models.posts); | ||
api.models.posts.hasMany(api.models.comments); | ||
api.models.comments.belongsTo(api.models.posts); | ||
api.models.posts.belongsTo(api.models.user); | ||
for (var model in api.models) { | ||
if (api.models[model].associate) { | ||
api.models[model].associate(api.models) | ||
} | ||
} | ||
next(); | ||
@@ -121,0 +141,0 @@ }, |
15435
237
150