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 5a75d260
authored
May 10, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored model.update and destroy to use promises
1 parent
1bd525d2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
118 deletions
lib/hooks.js
lib/model.js
lib/hooks.js
View file @
5a75d26
...
...
@@ -92,7 +92,9 @@ Hooks.runHooks = function() {
if
(
!!
arguments
[
0
])
{
return
reject
(
arguments
[
0
])
}
if
(
arguments
.
length
)
{
resolveArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
)
}
return
run
(
hooks
[
tick
])
}))
...
...
@@ -101,7 +103,9 @@ Hooks.runHooks = function() {
maybePromise
.
spread
(
function
()
{
tick
++
if
(
arguments
.
length
)
{
resolveArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
)
}
return
run
(
hooks
[
tick
])
},
reject
)
...
...
lib/model.js
View file @
5a75d26
...
...
@@ -1266,12 +1266,7 @@ module.exports = (function() {
,
query
=
null
,
args
=
[]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
runHooks
(
self
.
options
.
hooks
.
beforeBulkDestroy
,
where
,
function
(
err
,
newWhere
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
return
self
.
runHooks
(
self
.
options
.
hooks
.
beforeBulkDestroy
,
where
).
then
(
function
(
newWhere
)
{
where
=
newWhere
||
where
if
(
self
.
_timestampAttributes
.
deletedAt
&&
options
.
force
===
false
)
{
...
...
@@ -1284,88 +1279,52 @@ module.exports = (function() {
args
=
[
self
.
getTableName
(),
where
,
options
,
self
]
}
var
runQuery
=
function
(
err
,
records
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
query
=
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
query
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
results
)
{
var
finished
=
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkDestroy
,
where
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
emitter
.
emit
(
'success'
,
results
)
})
}
var
runQuery
=
function
(
records
)
{
return
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
).
then
(
function
(
results
)
{
if
(
options
&&
options
.
hooks
===
true
)
{
var
tick
=
0
var
next
=
function
(
i
)
{
self
.
runHooks
(
self
.
options
.
hooks
.
afterDestroy
,
records
[
i
],
function
(
err
,
newValues
)
{
if
(
!!
err
)
{
return
finished
(
err
)
}
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterDestroy
,
records
[
i
]).
then
(
function
(
newValues
)
{
records
[
i
].
dataValues
=
!!
newValues
?
newValues
.
dataValues
:
records
[
i
].
dataValues
tick
++
if
(
tick
>=
records
.
length
)
{
return
finished
(
)
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkDestroy
,
where
).
return
(
results
)
}
next
(
tick
)
return
next
(
tick
)
})
}
next
(
tick
)
return
next
(
tick
)
}
else
{
finished
(
)
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkDestroy
,
where
).
return
(
results
)
}
})
}
if
(
options
&&
options
.
hooks
===
true
)
{
var
tick
=
0
self
.
all
({
where
:
where
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
records
)
{
return
self
.
all
({
where
:
where
}).
then
(
function
(
records
)
{
var
next
=
function
(
i
)
{
self
.
runHooks
(
self
.
options
.
hooks
.
beforeDestroy
,
records
[
i
],
function
(
err
,
newValues
)
{
if
(
!!
err
)
{
return
runQuery
(
err
)
}
return
self
.
runHooks
(
self
.
options
.
hooks
.
beforeDestroy
,
records
[
i
]).
then
(
function
(
newValues
)
{
records
[
i
].
dataValues
=
!!
newValues
?
newValues
.
dataValues
:
records
[
i
].
dataValues
tick
++
if
(
tick
>=
records
.
length
)
{
return
runQuery
(
null
,
records
)
return
runQuery
(
records
)
}
next
(
tick
)
return
next
(
tick
)
})
}
next
(
tick
)
return
next
(
tick
)
})
//
}
else
{
runQuery
()
return
runQuery
()
}
})
}).
run
()
}
/**
...
...
@@ -1383,7 +1342,6 @@ module.exports = (function() {
*/
Model
.
prototype
.
update
=
function
(
attrValueHash
,
where
,
options
)
{
var
self
=
this
,
query
=
null
,
tick
=
0
options
=
options
||
{}
...
...
@@ -1395,97 +1353,58 @@ module.exports = (function() {
attrValueHash
[
self
.
_timestampAttributes
.
updatedAt
]
=
Utils
.
now
()
}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
runSave
=
function
()
{
self
.
runHooks
(
self
.
options
.
hooks
.
beforeBulkUpdate
,
attrValueHash
,
where
,
function
(
err
,
attributes
,
_where
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
return
self
.
runHooks
(
self
.
options
.
hooks
.
beforeBulkUpdate
,
attrValueHash
,
where
).
spread
(
function
(
attributes
,
_where
)
{
where
=
_where
||
where
attrValueHash
=
attributes
||
attrValueHash
var
runQuery
=
function
(
err
,
records
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
query
=
self
.
QueryInterface
.
bulkUpdate
(
self
.
getTableName
(),
attrValueHash
,
where
,
options
,
self
.
rawAttributes
)
query
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
results
)
{
var
finished
=
function
(
err
,
records
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkUpdate
,
attrValueHash
,
where
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
emitter
.
emit
(
'success'
,
records
)
})
}
var
runQuery
=
function
(
records
)
{
return
self
.
QueryInterface
.
bulkUpdate
(
self
.
getTableName
(),
attrValueHash
,
where
,
options
,
self
.
rawAttributes
).
then
(
function
(
results
)
{
if
(
options
&&
options
.
hooks
===
true
&&
!!
records
&&
records
.
length
>
0
)
{
var
tick
=
0
var
next
=
function
(
i
)
{
self
.
runHooks
(
self
.
options
.
hooks
.
afterUpdate
,
records
[
i
],
function
(
err
,
newValues
)
{
if
(
!!
err
)
{
return
finished
(
err
)
}
records
[
i
].
dataValues
=
!!
newValues
?
newValues
.
dataValues
:
records
[
i
].
dataValues
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterUpdate
,
records
[
i
]).
then
(
function
(
newValues
)
{
records
[
i
].
dataValues
=
(
!!
newValues
&&
newValues
.
dataValues
)
?
newValues
.
dataValues
:
records
[
i
].
dataValues
tick
++
if
(
tick
>=
records
.
length
)
{
return
finished
(
null
,
records
)
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkUpdate
,
attrValueHash
,
where
).
return
(
records
)
}
next
(
tick
)
return
next
(
tick
)
})
}
next
(
tick
)
return
next
(
tick
)
}
else
{
finished
(
null
,
results
)
return
self
.
runHooks
(
self
.
options
.
hooks
.
afterBulkUpdate
,
attrValueHash
,
where
).
return
(
results
)
}
})
}
if
(
options
.
hooks
===
true
)
{
self
.
all
({
where
:
where
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
records
)
{
return
self
.
all
({
where
:
where
}).
then
(
function
(
records
)
{
if
(
records
===
null
||
records
.
length
<
1
)
{
return
runQuery
(
null
)
return
runQuery
(
)
}
var
next
=
function
(
i
)
{
self
.
runHooks
(
self
.
options
.
hooks
.
beforeUpdate
,
records
[
i
],
function
(
err
,
newValues
)
{
if
(
!!
err
)
{
return
runQuery
(
err
)
}
records
[
i
].
dataValues
=
!!
newValues
?
newValues
.
dataValues
:
records
[
i
].
dataValues
return
self
.
runHooks
(
self
.
options
.
hooks
.
beforeUpdate
,
records
[
i
]).
then
(
function
(
newValues
)
{
records
[
i
].
dataValues
=
(
!!
newValues
&&
newValues
.
dataValues
)
?
newValues
.
dataValues
:
records
[
i
].
dataValues
tick
++
if
(
tick
>=
records
.
length
)
{
return
runQuery
(
null
,
records
)
return
runQuery
(
records
)
}
next
(
tick
)
return
next
(
tick
)
})
}
next
(
tick
)
return
next
(
tick
)
})
}
else
{
runQuery
()
return
runQuery
()
}
})
}
...
...
@@ -1497,19 +1416,16 @@ module.exports = (function() {
var
updatedFields
=
Object
.
keys
(
attrValueHash
)
var
skippedFields
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
updatedFields
)
build
.
hookValidate
({
skip
:
skippedFields
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}).
success
(
function
(
attributes
)
{
return
build
.
hookValidate
({
skip
:
skippedFields
}).
then
(
function
(
attributes
)
{
if
(
!!
attributes
&&
!!
attributes
.
dataValues
)
{
attrValueHash
=
Utils
.
_
.
pick
.
apply
(
Utils
.
_
,
[].
concat
(
attributes
.
dataValues
).
concat
(
Object
.
keys
(
attrValueHash
)))
}
runSave
()
return
runSave
()
})
}
else
{
runSave
()
return
runSave
()
}
}).
run
()
}
/**
...
...
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