- 24 Sep, 2013 1 commit
-
-
Daniel Durante committed
-
- 11 Sep, 2013 1 commit
-
-
Daniel Durante committed
-
- 10 Sep, 2013 2 commits
-
-
Daniel Durante committed
-
This commit adds the ability of hooks for the DAOFactory. The following hooks (in their order of operations) are: (1) beforeValidate(dao, fn) (-) validate (2) afterValidate(dao, fn) (3) beforeBulkCreate(daos, fields, fn) beforeBulkDestroy(daos, fields, fn) beforeBulkUpdate(daos, fields, fn) (4) beforeCreate(dao, fn) beforeDestroy(dao, fn) beforeUpdate(dao, fn) (-) create / destroy / update (5) afterCreate(dao, fn) aftreDestroy(dao, fn) afterUpdate(dao, fn) (6) afterBulkCreate(daos, fields, fn) afterBulkDestory(daos, fields, fn) afterBulkUpdate(daos, fields, fn) There's a new file called hooks.js which works very similar to mixins.js which just extends a prototype. You can add the hooks like so... ... via .define(): var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }, { hooks: { beforeValidate: function(user, fn) { user.mood = 'happy' fn(null, user) }, afterValidate: function(user, fn) { user.username = 'Toni' fn(null, user) } } }) ... via .hook() method var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }) User.hook('beforeValidate', function(user, fn) { user.mood = 'happy' fn(null, user) }) User.hook('afterValidate', function(user, fn) { user.username = 'Toni' fn(null, user) }) ... via direct method: var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }) User.beforeValidate(function(user, fn) { user.mood = 'happy' fn(null, user) }) User.afterValidate(function(user, fn) { user.username = 'Toni' fn(null, user) }) Quick example: User.beforeCreate(function(user, fn) { if (user.accessLevel > 10 && user.username !== "Boss") { return fn("You can't grant this user that level!") } return fn() }) User.create({ username: 'Not a Boss', accessLevel: 20 }).error(function(err) { console.log(err) // You can't grant this user that level! }) As of right now, each hook will process in the order they where implemented / added to the factory. To invoke the hooks simply run... Model.runHooks.call(Model.options.hooks.<hook>, <args>, <callback>) Some model hooks have two or three paramters sent to each hook depending on it's type. Model.beforeBulkCreate(function(records, fields, fn) { // records = the first argument sent to .bulkCreate // fields = the second argument sent to .bulkCreate }) Model.bulkCreate([ {username: 'Toni'}, // part of records argument {username: 'Tobi'} // part of records argument ], ['username'] /* part of fields argument */) Model.beforeBulkUpdate(function(attributes, where, fn) { // attributes = first argument sent to Model.update // where = second argument sent to Model.update }) Model.update( {gender: 'Male'} /*attribures argument*/, {username: 'Tom'} /*where argument*/ ) Model.beforeBulkDestroy(function(whereClause, fn) { // whereClause = first argument sent to Model.destroy }) Model.destroy({username: 'Tom'} /*whereClause argument*/) For 1.7.x backwards compatibility, I've added a new method called .hookValidate() since .validate() is a synchronous function. All of Sequelize's API functions will invoke .hookValidate(), but if you utilize the .validate() function outside of Sequelize then you'll need to update your code if you want to run before/afterValidate hooks. Sequelzie 2.0.x will not need this change simply because it's .validate() method is already asynchronous. However, it will have the .hookValdate() function in order to make the transition from 1.7 to 2.0 smoother and easier. Eventually we'll want to deprecate this function. In addition to this commit, I've also completed the following tasks: Move validation of enum attribute value to validate method I had to complete that task in order to get the validate hooks working properly. And the last thing, I fixed executables.test.js if your DB didn't use the default values for config/config.js, this was causing errors for me on my local machine.
Daniel Durante committed
-
- 07 Sep, 2013 13 commits
-
-
DAOFactory.update() will now skip validations for columns that we're not...
Jan Aagaard Meier committed -
Daniel Durante committed
-
Daniel Durante committed
-
tests & bugfixes for DAO-Factory.update and array of values in where clause
Daniel Durante committed -
Daniel Durante committed
-
postgres requires empty array to be explicitly cast on update
Daniel Durante committed -
Daniel Durante committed
-
Add support for typed arrays in SqlString.escape and SqlString.arrayToList
Daniel Durante committed -
Cleaned up the postgres connector-manager/query code, updated node-postg...
Daniel Durante committed -
…ndency, and made sure pg.Client ended correctly on drain to prevent leaks for non pooling connections.
Daniel Durante committed -
Jan Scheurer committed
-
Daniel Durante committed
-
Jan Scheurer committed
-
- 06 Sep, 2013 3 commits
-
-
Jan Scheurer committed
-
Jan Scheurer committed
-
Rob Raux committed
-
- 05 Sep, 2013 1 commit
-
-
Rob Raux committed
-
- 04 Sep, 2013 16 commits
-
-
Jan Aagaard Meier committed
-
Domas Lapinskas committed
-
Domas Lapinskas committed
-
Domas Lapinskas committed
-
Properly copy the options objects passed to find and findall
Daniel Durante committed -
Jan Aagaard Meier committed
-
Jan Aagaard Meier committed
-
Jan Aagaard Meier committed
-
Fixes a bug with .destroy() on a DAO record
Jan Aagaard Meier committed -
Daniel Durante committed
-
This only affected Postgres really, but when you did .find(), etc. and then .destroy() on the actual DAO rather than the DAOFactory, sometimes the QueryGenerator.deleteQuery() would have trouble finding the primary keys for that table. This provides additional info to Postgres' deleteQuery() function just in case. This commit adds a fourth argument to Postgres' QueryGenerator.deleteQuery() for declaring a DAOFactory.
Daniel Durante committed -
Domas Lapinskas committed
-
Domas Lapinskas committed
-
Daniel Durante committed
-
Fixes Postgres' ability to search within functions.
Jan Aagaard Meier committed -
This is a pure and simple fix for searching with a Postgres' array column type. This commit changes hashToWhereConditions by adding a second argument of sending the factory's details over and checking for column type if the hash/value/first argument is an array. For the actual search we just utilize a simple overlap && boolean search.
Daniel Durante committed
-
- 01 Sep, 2013 3 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Add Sequelize#model to fetch DAOFactory of previously defined model
Sascha Depold committed
-