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 97ef72a9
authored
Mar 16, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor queryInterface.delete to use promises rather than custom iterator
1 parent
3095a647
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
82 deletions
lib/query-interface.js
test/integration/hooks.test.js
lib/query-interface.js
View file @
97ef72a
...
...
@@ -612,7 +612,6 @@ module.exports = (function() {
QueryInterface
.
prototype
.
delete
=
function
(
dao
,
tableName
,
identifier
,
options
)
{
var
self
=
this
,
restrict
=
false
,
cascades
=
[]
,
sql
=
self
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
,
null
,
dao
.
Model
);
...
...
@@ -623,70 +622,20 @@ module.exports = (function() {
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
)
{
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
===
'restrict'
)
{
restrict
=
true
;
}
else
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
===
'cascade'
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
useHooks
===
true
)
{
cascades
[
cascades
.
length
]
=
dao
.
Model
.
associations
[
keys
[
i
]].
accessors
.
get
;
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
===
'cascade'
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
useHooks
===
true
)
{
cascades
.
push
(
dao
.
Model
.
associations
[
keys
[
i
]].
accessors
.
get
);
}
}
}
}
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
var
tick
=
0
;
var
iterate
=
function
(
err
,
i
)
{
if
(
err
)
{
return
reject
(
err
);
}
if
(
i
>=
cascades
.
length
)
{
return
resolve
();
}
dao
[
cascades
[
i
]]().
success
(
function
(
tasks
)
{
if
(
tasks
===
null
||
tasks
.
length
<
1
)
{
if
(
i
>=
cascades
.
length
)
{
return
resolve
();
}
else
{
tick
++
;
return
iterate
(
null
,
tick
);
}
}
tasks
=
Array
.
isArray
(
tasks
)
?
tasks
:
[
tasks
];
var
ii
=
0
;
var
next
=
function
(
err
,
ii
)
{
if
(
!!
err
||
ii
>=
tasks
.
length
)
{
return
iterate
(
err
);
}
tasks
[
ii
].
destroy
().
error
(
function
(
err
)
{
return
iterate
(
err
);
})
.
success
(
function
()
{
ii
++
;
if
(
ii
>=
tasks
.
length
)
{
tick
++
;
return
iterate
(
null
,
tick
);
}
next
(
null
,
ii
);
});
};
next
(
null
,
ii
);
});
};
if
(
cascades
.
length
>
0
)
{
iterate
(
null
,
tick
);
}
else
{
resolve
();
}
}).
then
(
function
()
{
return
Promise
.
reduce
(
cascades
,
function
(
memo
,
cascade
)
{
return
dao
[
cascade
]().
then
(
function
(
instances
)
{
return
Promise
.
reduce
(
instances
,
function
(
memo
,
instance
)
{
return
instance
.
destroy
();
},
[]);
});
},
[]).
then
(
function
()
{
return
self
.
sequelize
.
query
(
sql
,
dao
,
options
);
});
};
...
...
test/integration/hooks.test.js
View file @
97ef72a
...
...
@@ -5580,37 +5580,32 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
,
beforeTask
=
false
,
afterTask
=
false
,
beforeMiniTask
=
false
,
afterMiniTask
=
false
;
,
afterMiniTask
=
false
,
VeryCustomError
=
function
()
{};
this
.
Projects
.
beforeCreate
(
function
(
project
,
options
,
fn
)
{
this
.
Projects
.
beforeCreate
(
function
(
project
,
options
)
{
beforeProject
=
true
;
fn
();
});
this
.
Projects
.
afterCreate
(
function
(
project
,
options
,
fn
)
{
this
.
Projects
.
afterCreate
(
function
(
project
,
options
)
{
afterProject
=
true
;
fn
();
});
this
.
Tasks
.
beforeDestroy
(
function
(
task
,
options
,
fn
)
{
this
.
Tasks
.
beforeDestroy
(
function
(
task
,
options
)
{
beforeTask
=
true
;
fn
(
new
Error
(
'Whoops!'
)
);
throw
new
VeryCustomError
(
'Whoops!'
);
});
this
.
Tasks
.
afterDestroy
(
function
(
task
,
options
,
fn
)
{
this
.
Tasks
.
afterDestroy
(
function
(
task
,
options
)
{
afterTask
=
true
;
fn
();
});
this
.
MiniTasks
.
beforeDestroy
(
function
(
minitask
,
options
,
fn
)
{
this
.
MiniTasks
.
beforeDestroy
(
function
(
minitask
,
options
)
{
beforeMiniTask
=
true
;
fn
();
});
this
.
MiniTasks
.
afterDestroy
(
function
(
minitask
,
options
,
fn
)
{
this
.
MiniTasks
.
afterDestroy
(
function
(
minitask
,
options
)
{
afterMiniTask
=
true
;
fn
();
});
return
this
.
sequelize
.
Promise
.
all
([
...
...
@@ -5623,14 +5618,14 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
project
.
addTask
(
task
)
]).
return
(
project
);
}).
then
(
function
(
project
)
{
return
project
.
destroy
();
}).
catch
(
function
()
{
expect
(
before
Project
).
to
.
be
.
true
;
expect
(
afterProject
).
to
.
be
.
true
;
expect
(
beforeTask
).
to
.
be
.
tru
e
;
expect
(
after
Task
).
to
.
be
.
false
;
expect
(
before
MiniTask
).
to
.
be
.
false
;
expect
(
afterMiniTask
).
to
.
be
.
false
;
return
expect
(
project
.
destroy
()).
to
.
eventually
.
be
.
rejectedWith
(
VeryCustomError
).
then
(
function
()
{
expect
(
beforeProject
).
to
.
be
.
true
;
expect
(
after
Project
).
to
.
be
.
true
;
expect
(
beforeTask
).
to
.
be
.
true
;
expect
(
afterTask
).
to
.
be
.
fals
e
;
expect
(
beforeMini
Task
).
to
.
be
.
false
;
expect
(
after
MiniTask
).
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