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

Commit 68bdfdc8 by Jan Aagaard Meier

Added api refernece for promises, added a silent option to dao.save

1 parent 0701b1f9
...@@ -153,7 +153,9 @@ if (program.file) { ...@@ -153,7 +153,9 @@ if (program.file) {
{file:'lib/query-chainer.js', output: 'API-Reference-QueryChainer'}, {file:'lib/query-chainer.js', output: 'API-Reference-QueryChainer'},
{file:'lib/emitters/custom-event-emitter.js', output: 'API-Reference-EventEmitter'}, {file:'lib/emitters/custom-event-emitter.js', output: 'API-Reference-EventEmitter'},
{file:'lib/hooks.js', output: 'API-Reference-Hooks'}, {file:'lib/hooks.js', output: 'API-Reference-Hooks'},
{file:'lib/associations/mixin.js', output: 'API-Reference-Associations'} {file:'lib/associations/mixin.js', output: 'API-Reference-Associations'},
{file:'lib/promise.js', output: 'API-Reference-Promise'},
{file:'lib/transaction.js', output: 'API-Reference-Transaction'}
]; ];
} }
......
...@@ -398,6 +398,7 @@ module.exports = (function() { ...@@ -398,6 +398,7 @@ module.exports = (function() {
* @param {Array} [fields] An optional array of strings, representing database columns. If fields is provided, only those columns will be validation and saved. * @param {Array} [fields] An optional array of strings, representing database columns. If fields is provided, only those columns will be validation and saved.
* @param {Object} [options] * @param {Object} [options]
* @param {Object} [options.fields] An alternative way of setting which fields should be persisted * @param {Object} [options.fields] An alternative way of setting which fields should be persisted
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* *
* @return {Promise} * @return {Promise}
...@@ -426,14 +427,20 @@ module.exports = (function() { ...@@ -426,14 +427,20 @@ module.exports = (function() {
, updatedAtAttr = this.Model._timestampAttributes.updatedAt , updatedAtAttr = this.Model._timestampAttributes.updatedAt
, createdAtAttr = this.Model._timestampAttributes.createdAt , createdAtAttr = this.Model._timestampAttributes.createdAt
if (options.fields) { if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) {
if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) { options.fields.push(updatedAtAttr)
options.fields.push(updatedAtAttr) }
}
if (createdAtAttr && options.fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) { if (options.silent === true) {
options.fields.push(createdAtAttr) // UpdateAtAttr might have been added as a result of Object.keys(Model.attributes). In that case we have to remove it again
} Utils._.remove(options.fields, function (val) {
return val === updatedAtAttr
})
updatedAtAttr = false
}
if (this.isNewRecord === true && createdAtAttr && options.fields.indexOf(createdAtAttr) === -1) {
options.fields.push(createdAtAttr)
} }
return self.hookValidate({ return self.hookValidate({
......
...@@ -138,7 +138,7 @@ module.exports = (function() { ...@@ -138,7 +138,7 @@ module.exports = (function() {
* ``` * ```
* *
* @param {function} onError * @param {function} onError
* @metohd error * @method error
* @alias fail * @alias fail
* @alias failure * @alias failure
* @return this * @return this
......
...@@ -6,6 +6,14 @@ var util = require("util") ...@@ -6,6 +6,14 @@ var util = require("util")
, INTERNAL = function() {} , INTERNAL = function() {}
, async = require("bluebird/js/main/async.js") , async = require("bluebird/js/main/async.js")
/**
* A slightly modified version of bluebird promises. This means that, on top of the methods below, you can also call all the methods listed on the link below.
*
* The main difference is that sequelize promises allows you to attach a listener that will be called with the generated SQL, each time a query is run.
*
* @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md
* @class Promise
*/
var SequelizePromise = function(resolver) { var SequelizePromise = function(resolver) {
var self = this; var self = this;
...@@ -83,16 +91,12 @@ SequelizePromise.prototype._then = function ( ...@@ -83,16 +91,12 @@ SequelizePromise.prototype._then = function (
ret._setBoundTo(this._boundTo); ret._setBoundTo(this._boundTo);
} }
/* // Start of sequelize specific
* Start of sequelize specific // Needed to transfer sql events accross .then() calls
* Needed to transfer sql events accross .then() calls
*/
if (this.proxySql && ret && ret.emit) { if (this.proxySql && ret && ret.emit) {
this.proxySql(ret); this.proxySql(ret);
} }
/* // End of sequelize specific
* End of sequelize specific
*/
var callbackIndex = this._addCallbacks(didFulfill, didReject, didProgress, ret, receiver); var callbackIndex = this._addCallbacks(didFulfill, didReject, didProgress, ret, receiver);
...@@ -120,6 +124,12 @@ SequelizePromise.prototype._settlePromiseAt = function (index) { ...@@ -120,6 +124,12 @@ SequelizePromise.prototype._settlePromiseAt = function (index) {
return Promise.prototype._settlePromiseAt.apply(this, arguments); return Promise.prototype._settlePromiseAt.apply(this, arguments);
}; };
/**
* Listen for events, event emitter style. Mostly for backwards compat. with EventEmitter
*
* @param {String} evt
* @param {Function} fct
*/
SequelizePromise.prototype.on = function(evt, fct) { SequelizePromise.prototype.on = function(evt, fct) {
if (evt === 'success') { if (evt === 'success') {
this.then(fct); this.then(fct);
...@@ -134,6 +144,11 @@ SequelizePromise.prototype.on = function(evt, fct) { ...@@ -134,6 +144,11 @@ SequelizePromise.prototype.on = function(evt, fct) {
return this; return this;
} }
/**
* Emit an event from the emitter
* @param {string} type The type of event
* @param {any} value(s)* All other arguments will be passed to the event listeners
*/
SequelizePromise.prototype.emit = function(evt) { SequelizePromise.prototype.emit = function(evt) {
var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : []; var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : [];
...@@ -167,7 +182,6 @@ SequelizePromise.prototype.emit = function(evt) { ...@@ -167,7 +182,6 @@ SequelizePromise.prototype.emit = function(evt) {
* @alias ok * @alias ok
* @return this * @return this
*/ */
SequelizePromise.prototype.success = SequelizePromise.prototype.success =
SequelizePromise.prototype.ok = function(fct) { SequelizePromise.prototype.ok = function(fct) {
if (fct.length > 1) { if (fct.length > 1) {
...@@ -187,12 +201,11 @@ SequelizePromise.prototype.ok = function(fct) { ...@@ -187,12 +201,11 @@ SequelizePromise.prototype.ok = function(fct) {
* ``` * ```
* *
* @param {function} onError * @param {function} onError
* @metohd error * @method error
* @alias fail * @alias fail
* @alias failure * @alias failure
* @return this * @return this
*/ */
SequelizePromise.prototype.failure = SequelizePromise.prototype.failure =
SequelizePromise.prototype.fail = SequelizePromise.prototype.fail =
SequelizePromise.prototype.error = function(fct) { SequelizePromise.prototype.error = function(fct) {
...@@ -239,9 +252,9 @@ SequelizePromise.prototype.sql = function(fct) { ...@@ -239,9 +252,9 @@ SequelizePromise.prototype.sql = function(fct) {
/** /**
* Proxy every event of this promise to another one. * Proxy every event of this promise to another one.
* *
* @param {SequelizePromise} The promise that should receive the events. * @param {SequelizePromise} promise The promise that should receive the events.
* @param {Object} [options] * @param {Object} [options]
* @param {Array} [options.events] An array of the events to proxy. Defaults to sql, error and success * @param {Array} [options.events] An array of the events to proxy. Defaults to sql, error and success
* @return this * @return this
*/ */
SequelizePromise.prototype.proxy = function(promise, options) { SequelizePromise.prototype.proxy = function(promise, options) {
......
...@@ -2,7 +2,6 @@ var url = require("url") ...@@ -2,7 +2,6 @@ var url = require("url")
, Path = require("path") , Path = require("path")
, Utils = require("./utils") , Utils = require("./utils")
, DAOFactory = require("./dao-factory") , DAOFactory = require("./dao-factory")
, DAOValidator = require("./dao-validator")
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, DAOFactoryManager = require("./dao-factory-manager") , DAOFactoryManager = require("./dao-factory-manager")
, QueryInterface = require("./query-interface") , QueryInterface = require("./query-interface")
...@@ -174,7 +173,14 @@ module.exports = (function() { ...@@ -174,7 +173,14 @@ module.exports = (function() {
* @property Utils * @property Utils
* @see {Utils} * @see {Utils}
*/ */
Sequelize.Utils = Utils Sequelize.prototype.Utils = Sequelize.Utils = Utils
/**
* A modified version of bluebird promises, that allows listening for sql events
* @property Promise
* @see {Promise}
*/
Sequelize.prototype.Promise = Sequelize.Promise = Promise
Sequelize.QueryTypes = QueryTypes Sequelize.QueryTypes = QueryTypes
...@@ -825,7 +831,5 @@ module.exports = (function() { ...@@ -825,7 +831,5 @@ module.exports = (function() {
} }
} }
Sequelize.Promise = Promise
return Sequelize return Sequelize
})() })()
...@@ -849,6 +849,27 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -849,6 +849,27 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}, 1000) }, 1000)
}) })
it('does not update timestamps when passing silent=true', function() {
this.clock = sinon.useFakeTimers(new Date().getTime());
var self = this
return this.User.create({ username: 'user' }).then(function (user) {
var updatedAt = user.updatedAt
self.clock.tick(30000);
return user.updateAttributes({
username: 'userman'
}, {
silent: true
}).then(function () {
expect(user.updatedAt).to.equal(updatedAt)
self.clock.restore();
})
})
})
it('updates with function and column value', function (done) { it('updates with function and column value', function (done) {
var self = this var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!