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 00246ecf
authored
Jan 09, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
before and after hooks now get the DAO instance instead of only the dataValues
1 parent
3b2c0ee0
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
155 additions
and
18 deletions
lib/dao-validator.js
lib/dao.js
test/dao-factory.test.js
test/hooks.test.js
lib/dao-validator.js
View file @
00246ec
...
@@ -23,12 +23,11 @@ DaoValidator.prototype.hookValidate = function() {
...
@@ -23,12 +23,11 @@ DaoValidator.prototype.hookValidate = function() {
,
errors
=
{}
,
errors
=
{}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
model
.
daoFactory
.
runHooks
(
'beforeValidate'
,
self
.
model
.
dataValues
,
function
(
err
,
newValues
)
{
self
.
model
.
daoFactory
.
runHooks
(
'beforeValidate'
,
self
.
model
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
self
.
model
.
dataValues
=
newValues
||
self
.
model
.
dataValues
errors
=
Utils
.
_
.
extend
(
errors
,
validateAttributes
.
call
(
self
))
errors
=
Utils
.
_
.
extend
(
errors
,
validateAttributes
.
call
(
self
))
errors
=
Utils
.
_
.
extend
(
errors
,
validateModel
.
call
(
self
))
errors
=
Utils
.
_
.
extend
(
errors
,
validateModel
.
call
(
self
))
...
@@ -36,12 +35,11 @@ DaoValidator.prototype.hookValidate = function() {
...
@@ -36,12 +35,11 @@ DaoValidator.prototype.hookValidate = function() {
return
emitter
.
emit
(
'error'
,
errors
)
return
emitter
.
emit
(
'error'
,
errors
)
}
}
self
.
model
.
daoFactory
.
runHooks
(
'afterValidate'
,
self
.
model
.
dataValues
,
function
(
err
,
newValues
)
{
self
.
model
.
daoFactory
.
runHooks
(
'afterValidate'
,
self
.
model
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
self
.
model
.
dataValues
=
newValues
||
self
.
model
.
dataValues
emitter
.
emit
(
'success'
,
self
.
model
)
emitter
.
emit
(
'success'
,
self
.
model
)
})
})
})
})
...
...
lib/dao.js
View file @
00246ec
...
@@ -375,35 +375,40 @@ module.exports = (function() {
...
@@ -375,35 +375,40 @@ module.exports = (function() {
hook
=
'Update'
hook
=
'Update'
}
}
self
.
__factory
.
runHooks
(
'before'
+
hook
,
values
,
function
(
err
,
newValues
)
{
// Add the values to the DAO
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
)
// Run the beforeCreate / beforeUpdate hook
self
.
__factory
.
runHooks
(
'before'
+
hook
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
args
[
2
]
=
values
=
newValues
||
values
// dataValues might have changed inside the hook, rebuild
// Newest values need to be on the dao since the dao is returned from the query interface
// the values hash
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
)
values
=
{}
options
.
fields
.
forEach
(
function
(
field
)
{
if
(
self
.
dataValues
[
field
]
!==
undefined
)
{
values
[
field
]
=
self
.
dataValues
[
field
]
}
})
args
[
2
]
=
values
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
result
)
{
.
success
(
function
(
result
)
{
// Transfer database generated values (defaults, autoincrement, etc)
// Transfer database generated values (defaults, autoincrement, etc)
values
=
_
.
extend
(
values
,
result
.
values
)
values
=
_
.
extend
(
values
,
result
.
dataValues
)
// Ensure new values are on DAO, and reset previousDataValues
// Ensure new values are on DAO, and reset previousDataValues
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
values
)
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
values
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
self
.
__factory
.
runHooks
(
'after'
+
hook
,
values
,
function
(
err
,
newValues
)
{
self
.
__factory
.
runHooks
(
'after'
+
hook
,
result
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
if
(
newValues
)
{
// Repeat value assignment incase afterHook changed anything
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
newValues
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
}
emitter
.
emit
(
'success'
,
result
)
emitter
.
emit
(
'success'
,
result
)
})
})
})
})
...
@@ -486,7 +491,7 @@ module.exports = (function() {
...
@@ -486,7 +491,7 @@ module.exports = (function() {
,
query
=
null
,
query
=
null
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
beforeDestroy
,
self
.
dataValues
,
function
(
err
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
beforeDestroy
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
...
@@ -507,7 +512,7 @@ module.exports = (function() {
...
@@ -507,7 +512,7 @@ module.exports = (function() {
emitter
.
emit
(
'error'
,
err
)
emitter
.
emit
(
'error'
,
err
)
})
})
.
success
(
function
(
results
)
{
.
success
(
function
(
results
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
afterDestroy
,
self
.
dataValues
,
function
(
err
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
afterDestroy
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
return
emitter
.
emit
(
'error'
,
err
)
}
}
...
...
test/dao-factory.test.js
View file @
00246ec
test/hooks.test.js
View file @
00246ec
...
@@ -6994,4 +6994,138 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
...
@@ -6994,4 +6994,138 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
})
})
})
})
describe
(
'passing DAO instances'
,
function
()
{
describe
(
'beforeValidate / afterValidate'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
hooks
:
{
beforeValidate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
},
afterValidate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'bob'
}).
success
(
function
(
user
)
{
done
()
})
})
})
})
describe
(
'beforeCreate / afterCreate'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
hooks
:
{
beforeCreate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
},
afterCreate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'bob'
}).
success
(
function
(
user
)
{
done
()
})
})
})
})
describe
(
'beforeDestroy / afterDestroy'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
hooks
:
{
beforeDestroy
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
},
afterDestroy
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'bob'
}).
success
(
function
(
user
)
{
user
.
destroy
().
success
(
function
()
{
done
()
})
})
})
})
})
describe
(
'beforeDelete / afterDelete'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
hooks
:
{
beforeDelete
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
},
afterDelete
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'bob'
}).
success
(
function
(
user
)
{
user
.
destroy
().
success
(
function
()
{
done
()
})
})
})
})
})
describe
(
'beforeUpdate / afterUpdate'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
hooks
:
{
beforeUpdate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
},
afterUpdate
:
function
(
user
,
fn
)
{
expect
(
user
).
to
.
be
.
instanceof
(
User
.
DAO
)
fn
()
}
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'bob'
}).
success
(
function
(
user
)
{
user
.
save
({
username
:
'bawb'
}).
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
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