不要怂,就是干,撸起袖子干!

  1. 10 Sep, 2013 1 commit
    • Adds hooks / callbacks / lifecycle events · 0299ce63
      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
  2. 07 Sep, 2013 1 commit
  3. 04 Sep, 2013 11 commits
  4. 01 Sep, 2013 3 commits
  5. 31 Aug, 2013 4 commits
  6. 30 Aug, 2013 1 commit
  7. 29 Aug, 2013 11 commits
  8. 28 Aug, 2013 4 commits
    • ENUM types will no longer in Postgres · fb37f064
      Postgres will now add enum's values on .sync() if you were to add
      entires into the attribute's.value. It'll also add the enum type in
      the proper order. For example:
      
      ```js
      Table = sequelize.define...
        mood: {
          type: DataTypes.ENUM,
          values: ['happy', 'sad']
        }
      
      // For some reason down the road you redefine the same table
      // ...
        mood: {
          type: DataTypes.ENUM,
          values: ['neutral', 'happy', 'sad', 'joyful']
        }
      ```
      
      Will result in the following SQL:
      
      ```sql
      ALTER TYPE "...enumTypeName..." ADD VALUE 'neutral' BEFORE 'happy';
      ALTER TYPE "...enumTypeName..." ADD VALUE 'jouful' AFTER 'sad';
      ```
      
      - ```.drop()``` will now pass the options object through (optional)
      - Added Postgres.QueryGenerator.pgListEnums(tableName=null,
         attrName=null, options) which lists all of the ENUM types for
         Postgres, if you don't specify an attribute and tablename then
         Sequelize will list all of the Enums
      - Added Postgres.QueryGenerator.pgEnumAdd(tableName, attr, value,
         options) which will alter the enum type and add values
      - Added Postgres.QueryGenerator.pgEnumDrop(tableName, attr) which
         will drop the enum type
      - Postgres.QueryGenerator.pgEnum() will no longer try to drop the
         type unless {force: true} within the ```.sync()``` commands
      - Refactored ```QueryInterface.createTable()``` in order to allow
         Postgres to create enum types explicitly rather than from
         Postgres.```QueryGenerator.createTable()```
      - Refactored QueryInterface.dropTable() -- same as ```createTable()```
         changes .. also {force: true/force} will now set options.cascade
      - QueryInterface.dropAllTables() will now correctly add a quote
         identifier to table names for MySQL
      
      Closes https://github.com/sequelize/sequelize/issues/546
      Daniel Durante committed
    • Postgres single connections will no longer leak · 97c50bee
      Some users were experiencing problems with Postgres connections leaking when
      they didn't enable pooling. This commit should fix the problem.
      
      - In prototype.disconnect() we now properly call this.client.end with
        this.client.end.bind(this.client) (as shown in the node-postgres wiki).
      - We no longer check for readyForQuery on this.client as that value may not
        always be populated by node-postgres (possibly a bug on node-postgres).
      
      This should close https://github.com/sequelize/sequelize/issues/859
      Daniel Durante committed
    • Fixes PostgresSQL connection problems. · 270ac753
      This commit fixes the following connection problems:
      * Pools should now properly release themselves always
      * You can now set a "TestOnBorrow"-like functionality with
        config.pool.reapInterval).
      * Heroku should be able to properly send the correct DB credentials.
      Daniel Durante committed
  9. 27 Aug, 2013 4 commits