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
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
160 additions
and
23 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() {
,
errors
=
{}
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
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
self
.
model
.
dataValues
=
newValues
||
self
.
model
.
dataValues
errors
=
Utils
.
_
.
extend
(
errors
,
validateAttributes
.
call
(
self
))
errors
=
Utils
.
_
.
extend
(
errors
,
validateModel
.
call
(
self
))
...
...
@@ -36,12 +35,11 @@ DaoValidator.prototype.hookValidate = function() {
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
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
self
.
model
.
dataValues
=
newValues
||
self
.
model
.
dataValues
emitter
.
emit
(
'success'
,
self
.
model
)
})
})
...
...
lib/dao.js
View file @
00246ec
...
...
@@ -12,7 +12,7 @@ module.exports = (function() {
this
.
__options
=
this
.
__factory
.
options
this
.
options
=
options
this
.
hasPrimaryKeys
=
this
.
__factory
.
options
.
hasPrimaryKeys
// What is selected values even used for?
// What is selected values even used for?
this
.
selectedValues
=
options
.
include
?
_
.
omit
(
values
,
options
.
includeNames
)
:
values
this
.
__eagerlyLoadedAssociations
=
[]
this
.
isNewRecord
=
options
.
isNewRecord
...
...
@@ -157,7 +157,7 @@ module.exports = (function() {
// If not raw, and there's a customer setter
if
(
!
options
.
raw
&&
this
.
_customSetters
[
key
])
{
this
.
_customSetters
[
key
].
call
(
this
,
value
,
key
)
}
else
{
}
else
{
// Check if we have included models, and if this key matches the include model names/aliases
if
(
this
.
options
&&
this
.
options
.
include
&&
this
.
options
.
includeNames
.
indexOf
(
key
)
!==
-
1
)
{
...
...
@@ -375,35 +375,40 @@ module.exports = (function() {
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
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
args
[
2
]
=
values
=
newValues
||
values
// Newest values need to be on the dao since the dao is returned from the query interface
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
)
// dataValues might have changed inside the hook, rebuild
// the values hash
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
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
result
)
{
// 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
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
values
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
self
.
__factory
.
runHooks
(
'after'
+
hook
,
values
,
function
(
err
,
newValues
)
{
self
.
__factory
.
runHooks
(
'after'
+
hook
,
result
,
function
(
err
)
{
if
(
!!
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
)
})
})
...
...
@@ -486,7 +491,7 @@ module.exports = (function() {
,
query
=
null
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
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -507,7 +512,7 @@ module.exports = (function() {
emitter
.
emit
(
'error'
,
err
)
})
.
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
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
test/dao-factory.test.js
View file @
00246ec
...
...
@@ -1686,10 +1686,10 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
beforeEach
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
paranoid
:
true
})
this
.
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
title
:
DataTypes
.
STRING
},
{
paranoid
:
true
})
this
.
Project
.
hasMany
(
this
.
User
)
this
.
User
.
hasMany
(
this
.
Project
)
var
self
=
this
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
bulkCreate
([{
...
...
@@ -1777,7 +1777,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}
}).
error
(
done
)
})
})
})
test/hooks.test.js
View file @
00246ec
...
...
@@ -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