ah-sequelize-plugin
Advanced tools
Comparing version 0.5.0 to 0.5.1
exports.default = { | ||
sequelize: function(api){ | ||
return { | ||
"autoMigrate" : true, | ||
"database" : "DEVELOPMENT_DB", | ||
@@ -35,2 +36,3 @@ "dialect" : "mysql", | ||
// return { | ||
// "autoMigrate" : false, | ||
// "logging" : false, | ||
@@ -37,0 +39,0 @@ // "database" : "PRODUCTION_DB", |
@@ -71,4 +71,3 @@ var path = require('path'); | ||
}); | ||
}, | ||
} | ||
}; | ||
@@ -82,5 +81,13 @@ | ||
api.sequelize.connect(function(err){ | ||
next(err); | ||
if(err) { | ||
return next(err); | ||
} | ||
if(api.config.sequelize.autoMigrate) { | ||
api.sequelize.migrate({method: 'up'}, next) | ||
} else { | ||
next(err); | ||
} | ||
}); | ||
} | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "I use sequelize in actionhero as an ORM", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"homepage": "http://actionherojs.com", | ||
@@ -8,0 +8,0 @@ "repository": { |
# ah-sequelize-plugin | ||
This pluggin will use the sequelize orm to create `api.models` which contain your sequelize models | ||
This plugin will use the sequelize orm to create `api.models` which contain your sequelize models | ||
@@ -8,3 +8,3 @@ ## Setup | ||
- install this plugin: `npm install ah-sequelize-plugin --save` | ||
- be sure to enable the pluggin within actionhero (`config/plugins.js`) | ||
- be sure to enable the plugin within actionhero (`config/plugins.js`) | ||
- you will need to add the sequelize package (`npm install sequelize --save`) to your package.json | ||
@@ -14,2 +14,4 @@ - you will need to add the sequelize-fixtures package (`npm install sequelize-fixtures --save`) to your package.json | ||
- there are many options you can pass to sequelize. You can learn more here: http://sequelize.readthedocs.org/en/latest/api/sequelize/index.html | ||
- you will need to add the sequelize-cli package (`npm install sequelize-cli`) to your package.json | ||
- you could install it globally instead (`npm install -g sequelize-cli`) | ||
@@ -35,3 +37,3 @@ A `./config/sequelize.js` file will be created which will store your database configuration | ||
This pluggin does not condone the use of `Sequelize.sync()` in favor of migrations. Keep you migrations in `./migrationss` and run `api.sequelize.migrate()`. | ||
This plugin does not condone the use of `Sequelize.sync()` in favor of migrations. Keep you migrations in `./migrations` and use the [sequelize-cli](https://github.com/sequelize/cli) to execute them. | ||
@@ -88,52 +90,38 @@ An example migration to create a `users` table would look like: | ||
You can use the [sequelize-cli](http://docs.sequelizejs.com/en/latest/api/migrations#cli) for more utilities or | ||
you can add a migration grunt helper(s) to your actionhero project by adding the below to your `gruntfile.js`: | ||
You can use the [sequelize-cli](http://docs.sequelizejs.com/en/latest/docs/migrations/) to create and execute migrations. | ||
Using the `migrator` class on `api.sequelize` is [deprecated](https://github.com/sequelize/sequelize/issues/3301#issuecomment-77935976), as Sequelize | ||
now recommends using [Umzug](https://github.com/sequelize/umzug) to manage database schemas. | ||
```javascript | ||
grunt.registerTask('migrate','run any pending database migrations',function(file){ | ||
var done = this.async(); | ||
init(function(api){ | ||
api.sequelize.migrate(function(){ | ||
done(); | ||
}) | ||
}) | ||
}) | ||
``` | ||
If you want to sync, you can `api.sequelize.sync()` or `api.models.yourModel.sync()`; | ||
To migrate down also add the following: | ||
By default, `ah-sequelize-plugin` will automatically execute any pending migrations when Actionhero starts up. You can disable this behaviour by adding `autoMigrate: false` to your sequelize config. | ||
```javascript | ||
grunt.registerTask('migrate:undo','revert and run the “down” action on the last run migration',function(file){ | ||
var done = this.async(); | ||
init(function(api){ | ||
api.sequelize.migrateUndo(function(){ | ||
done(); | ||
}) | ||
}) | ||
}) | ||
``` | ||
If you want to sync, you can `api.sequelize.sync()` or `api.models.yourModel.sync()`; | ||
## [Associations](http://docs.sequelizejs.com/en/latest/api/associations) | ||
If you want to declare associations, best practice has you create an `associations` initializer within your project which might look like this: | ||
If you want to declare associations, best practice has you create an `associations.js` initializer within your project which might look like this: | ||
```javascript | ||
exports.associations = function(api, next){ | ||
module.exports = { | ||
loadPriority: 1000, | ||
startPriority: 1002, // priority has to be after models have been loaded | ||
stopPriority: 1000, | ||
api.associations = {}; | ||
associations: {}, | ||
api.associations._start = function(api, next){ | ||
api.models.user.hasMany(api.models.posts); | ||
api.models.posts.hasMany(api.models.comments); | ||
initialize: function (api, next) { | ||
next(); | ||
}, | ||
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); | ||
api.models.comments.belongsTo(api.models.posts); | ||
api.models.posts.belongsTo(api.models.user); | ||
next(); | ||
}; | ||
next(); | ||
} | ||
next(); | ||
}, | ||
stop: function (api, next) { | ||
next(); | ||
} | ||
}; | ||
``` | ||
@@ -143,2 +131,2 @@ | ||
We use the `sequelize-fixtures` package to load in JSON-defined fixtures in the test NODE_ENV. Store your fixtures in `./test/fixtures/*.json` or `./test/fixtures/*.yml` | ||
We use the `sequelize-fixtures` package to load in JSON-defined fixtures in the test NODE\_ENV. Store your fixtures in `./test/fixtures/*.json` or `./test/fixtures/*.yml` |
10963
157
128