Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit ba537e9b
authored
May 16, 2015
by
Ruben Bridgewater
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
instance.changed() should return the fields in beforeUpdate hook. Fixes #3631
1 parent
7c9d1d7b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
9 deletions
changelog.md
lib/model.js
test/integration/hooks.test.js
changelog.md
View file @
ba537e9
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
-
[
BUG
]
fix showIndexQuery so appropriate indexes are returned when a schema is used
-
[
BUG
]
fix showIndexQuery so appropriate indexes are returned when a schema is used
-
[
BUG
]
Fix addIndexQuery error when the model has a schema
-
[
BUG
]
Fix addIndexQuery error when the model has a schema
-
[
BUG
]
Fix app crash in sqlite while running in special unique constraint errors
[
3730
](
https://github.com/sequelize/sequelize/pull/3730
)
-
[
BUG
]
Fix app crash in sqlite while running in special unique constraint errors
[
3730
](
https://github.com/sequelize/sequelize/pull/3730
)
-
[
BUG
]
Fix regression in beforeUpdate hook where
`instance.changed()`
would always be false
[
3727
](
https://github.com/sequelize/sequelize/pull/3727
)
# 2.1.3
# 2.1.3
-
[
BUG
]
Fix regression introduced in 2.1.2: updatedAt not set anymore
[
3667
](
https://github.com/sequelize/sequelize/pull/3667
)
-
[
BUG
]
Fix regression introduced in 2.1.2: updatedAt not set anymore
[
3667
](
https://github.com/sequelize/sequelize/pull/3667
)
...
...
lib/model.js
View file @
ba537e9
...
@@ -1685,8 +1685,14 @@ module.exports = (function() {
...
@@ -1685,8 +1685,14 @@ module.exports = (function() {
,
different
=
false
;
,
different
=
false
;
return
Promise
.
map
(
daos
,
function
(
dao
)
{
return
Promise
.
map
(
daos
,
function
(
dao
)
{
// Record updates in
dao'
s dataValues
// Record updates in
instance
s dataValues
Utils
.
_
.
extend
(
dao
.
dataValues
,
values
);
Utils
.
_
.
extend
(
dao
.
dataValues
,
values
);
// Set the changed fields on the instance
Utils
.
_
.
forIn
(
valuesUse
,
function
(
newValue
,
attr
)
{
if
(
newValue
!==
dao
.
_previousDataValues
[
attr
])
{
dao
.
setDataValue
(
attr
,
newValue
);
}
});
// Run beforeUpdate hook
// Run beforeUpdate hook
return
self
.
runHooks
(
'beforeUpdate'
,
dao
,
options
).
then
(
function
()
{
return
self
.
runHooks
(
'beforeUpdate'
,
dao
,
options
).
then
(
function
()
{
...
...
test/integration/hooks.test.js
View file @
ba537e9
...
@@ -3437,6 +3437,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
...
@@ -3437,6 +3437,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
this
.
User
.
beforeUpdate
(
function
(
user
,
options
,
fn
)
{
this
.
User
.
beforeUpdate
(
function
(
user
,
options
,
fn
)
{
expect
(
user
.
changed
()).
to
.
not
.
be
.
empty
;
user
.
beforeHookTest
=
true
;
user
.
beforeHookTest
=
true
;
fn
();
fn
();
});
});
...
@@ -3460,33 +3461,52 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
...
@@ -3460,33 +3461,52 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
});
});
it
(
'should run the after/before functions for each item created successfully changing some data before updating'
,
function
()
{
var
self
=
this
;
this
.
User
.
beforeUpdate
(
function
(
user
,
options
)
{
expect
(
user
.
changed
()).
to
.
not
.
be
.
empty
;
if
(
user
.
get
(
'id'
)
===
1
)
{
user
.
set
(
'aNumber'
,
user
.
get
(
'aNumber'
)
+
3
);
}
});
return
this
.
User
.
bulkCreate
([
{
aNumber
:
1
},
{
aNumber
:
1
},
{
aNumber
:
1
}
]).
then
(
function
()
{
return
self
.
User
.
update
({
aNumber
:
10
},
{
where
:
{
aNumber
:
1
},
individualHooks
:
true
}).
spread
(
function
(
affectedRows
,
records
)
{
records
.
forEach
(
function
(
record
,
i
)
{
expect
(
record
.
aNumber
).
to
.
equal
(
10
+
(
record
.
id
===
1
?
3
:
0
));
});
});
});
});
it
(
'should run the after/before functions for each item created with an error'
,
function
()
{
it
(
'should run the after/before functions for each item created with an error'
,
function
()
{
var
self
=
this
var
self
=
this
,
beforeBulk
=
false
,
beforeBulk
=
false
,
afterBulk
=
false
;
,
afterBulk
=
false
;
this
.
User
.
beforeBulkUpdate
(
function
(
options
,
fn
)
{
this
.
User
.
beforeBulkUpdate
(
function
(
options
)
{
beforeBulk
=
true
;
beforeBulk
=
true
;
fn
();
});
});
this
.
User
.
afterBulkUpdate
(
function
(
options
,
fn
)
{
this
.
User
.
afterBulkUpdate
(
function
(
options
)
{
afterBulk
=
true
;
afterBulk
=
true
;
fn
();
});
});
this
.
User
.
beforeUpdate
(
function
(
user
,
options
,
fn
)
{
this
.
User
.
beforeUpdate
(
function
(
user
,
options
)
{
fn
(
new
Error
(
'You shall not pass!'
)
);
throw
new
Error
(
'You shall not pass!'
);
});
});
this
.
User
.
afterUpdate
(
function
(
user
,
options
,
fn
)
{
this
.
User
.
afterUpdate
(
function
(
user
,
options
)
{
user
.
username
=
'User'
+
user
.
id
;
user
.
username
=
'User'
+
user
.
id
;
fn
();
});
});
return
this
.
User
.
bulkCreate
([{
aNumber
:
1
},
{
aNumber
:
1
},
{
aNumber
:
1
}],
{
fields
:
[
'aNumber'
]
}).
then
(
function
()
{
return
this
.
User
.
bulkCreate
([{
aNumber
:
1
},
{
aNumber
:
1
},
{
aNumber
:
1
}],
{
fields
:
[
'aNumber'
]
}).
then
(
function
()
{
return
self
.
User
.
update
({
aNumber
:
10
},
{
where
:
{
aNumber
:
1
},
individualHooks
:
true
}).
catch
(
function
(
err
)
{
return
self
.
User
.
update
({
aNumber
:
10
},
{
where
:
{
aNumber
:
1
},
individualHooks
:
true
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
instanceOf
(
Error
);
expect
(
err
.
message
).
to
.
be
.
equal
(
'You shall not pass!'
);
expect
(
beforeBulk
).
to
.
be
.
true
;
expect
(
beforeBulk
).
to
.
be
.
true
;
expect
(
afterBulk
).
to
.
be
.
false
;
expect
(
afterBulk
).
to
.
be
.
false
;
});
});
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment