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 ab1653d5
authored
May 29, 2014
by
Overlook Motel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
hooks refactor
1 parent
41812ff7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
31 deletions
lib/instance.js
lib/model.js
lib/instance.js
View file @
ab1653d
...
@@ -416,7 +416,9 @@ module.exports = (function() {
...
@@ -416,7 +416,9 @@ module.exports = (function() {
fieldsOrOptions
=
{
fields
:
fieldsOrOptions
};
fieldsOrOptions
=
{
fields
:
fieldsOrOptions
};
}
}
options
=
Utils
.
_
.
extend
({},
options
,
fieldsOrOptions
);
options
=
Utils
.
_
.
extend
({
hooks
:
true
},
options
,
fieldsOrOptions
);
if
(
!
options
.
fields
)
{
if
(
!
options
.
fields
)
{
options
.
fields
=
Object
.
keys
(
this
.
Model
.
attributes
);
options
.
fields
=
Object
.
keys
(
this
.
Model
.
attributes
);
...
@@ -451,8 +453,11 @@ module.exports = (function() {
...
@@ -451,8 +453,11 @@ module.exports = (function() {
options
.
fields
.
push
(
createdAtAttr
);
options
.
fields
.
push
(
createdAtAttr
);
}
}
return
self
.
hookValidate
({
return
Promise
.
try
(
function
()
{
skip
:
_
.
difference
(
Object
.
keys
(
self
.
rawAttributes
),
options
.
fields
)
// Validate
if
(
options
.
hooks
)
{
return
self
.
hookValidate
({
skip
:
_
.
difference
(
Object
.
keys
(
self
.
rawAttributes
),
options
.
fields
)});
}
}).
then
(
function
()
{
}).
then
(
function
()
{
options
.
fields
.
forEach
(
function
(
field
)
{
options
.
fields
.
forEach
(
function
(
field
)
{
if
(
self
.
dataValues
[
field
]
!==
undefined
)
{
if
(
self
.
dataValues
[
field
]
!==
undefined
)
{
...
@@ -537,26 +542,30 @@ module.exports = (function() {
...
@@ -537,26 +542,30 @@ module.exports = (function() {
// Add the values to the Instance
// Add the values to the Instance
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
);
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
);
return
self
.
Model
.
runHooks
(
'before'
+
hook
,
self
).
then
(
function
()
{
return
Promise
.
try
(
function
()
{
// dataValues might have changed inside the hook, rebuild
// Run before hook
// the values hash
if
(
options
.
hooks
)
{
values
=
{};
return
self
.
Model
.
runHooks
(
'before'
+
hook
,
self
).
then
(
function
()
{
// dataValues might have changed inside the hook, rebuild the values hash
options
.
fields
.
forEach
(
function
(
attr
)
{
values
=
{};
if
(
self
.
dataValues
[
attr
]
!==
undefined
)
{
values
[
attr
]
=
self
.
dataValues
[
attr
];
options
.
fields
.
forEach
(
function
(
attr
)
{
}
if
(
self
.
dataValues
[
attr
]
!==
undefined
)
{
values
[
attr
]
=
self
.
dataValues
[
attr
];
// Field name mapping
}
if
(
self
.
Model
.
rawAttributes
[
attr
].
field
)
{
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
// Field name mapping
delete
values
[
attr
];
if
(
self
.
Model
.
rawAttributes
[
attr
].
field
)
{
}
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
});
delete
values
[
attr
];
}
args
[
2
]
=
values
;
});
return
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
).
catch
(
function
(
err
)
{
args
[
2
]
=
values
;
});
}
}).
then
(
function
()
{
return
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
).
catch
(
function
(
err
)
{
if
(
!!
self
.
__options
.
uniqueKeys
&&
err
.
code
&&
self
.
QueryInterface
.
QueryGenerator
.
uniqueConstraintMapping
.
code
===
err
.
code
)
{
if
(
!!
self
.
__options
.
uniqueKeys
&&
err
.
code
&&
self
.
QueryInterface
.
QueryGenerator
.
uniqueConstraintMapping
.
code
===
err
.
code
)
{
var
fields
=
self
.
QueryInterface
.
QueryGenerator
.
uniqueConstraintMapping
.
map
(
err
.
toString
());
var
fields
=
self
.
QueryInterface
.
QueryGenerator
.
uniqueConstraintMapping
.
map
(
err
.
toString
());
...
@@ -571,15 +580,20 @@ module.exports = (function() {
...
@@ -571,15 +580,20 @@ module.exports = (function() {
}
}
throw
err
;
throw
err
;
}).
t
hen
(
function
(
result
)
{
}).
t
ap
(
function
(
result
)
{
// Transfer database generated values (defaults, autoincrement, etc)
// Transfer database generated values (defaults, autoincrement, etc)
values
=
_
.
extend
(
values
,
result
.
dataValues
);
values
=
_
.
extend
(
values
,
result
.
dataValues
);
// Ensure new values are on Instance, and reset previousDataValues
// Ensure new values are on Instance, 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
);
}).
tap
(
function
(
result
)
{
return
self
.
Model
.
runHooks
(
'after'
+
hook
,
result
).
return
(
result
);
// Run before hook
if
(
options
.
hooks
)
{
return
self
.
Model
.
runHooks
(
'after'
+
hook
,
result
);
}
}).
then
(
function
(
result
)
{
return
result
;
});
});
});
});
});
});
...
@@ -663,24 +677,37 @@ module.exports = (function() {
...
@@ -663,24 +677,37 @@ module.exports = (function() {
* @return {Promise<undefined>}
* @return {Promise<undefined>}
*/
*/
Instance
.
prototype
.
destroy
=
function
(
options
)
{
Instance
.
prototype
.
destroy
=
function
(
options
)
{
options
=
options
||
{};
options
=
Utils
.
_
.
extend
({
options
.
force
=
options
.
force
===
undefined
?
false
:
Boolean
(
options
.
force
);
hooks
:
true
,
force
:
false
},
options
||
{});
var
self
=
this
;
var
self
=
this
;
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
return
self
.
Model
.
runHooks
(
self
.
Model
.
options
.
hooks
.
beforeDestroy
,
self
).
then
(
function
()
{
return
Promise
.
try
(
function
()
{
// Run before hook
if
(
options
.
hooks
)
{
return
self
.
Model
.
runHooks
(
'beforeDestroy'
,
self
);
}
}).
then
(
function
()
{
var
identifier
;
var
identifier
;
if
(
self
.
Model
.
_timestampAttributes
.
deletedAt
&&
options
.
force
===
false
)
{
if
(
self
.
Model
.
_timestampAttributes
.
deletedAt
&&
options
.
force
===
false
)
{
self
.
dataValues
[
self
.
Model
.
_timestampAttributes
.
deletedAt
]
=
new
Date
();
self
.
dataValues
[
self
.
Model
.
_timestampAttributes
.
deletedAt
]
=
new
Date
();
options
.
hooks
=
false
;
return
self
.
save
(
options
);
return
self
.
save
(
options
);
}
else
{
}
else
{
identifier
=
self
.
__options
.
hasPrimaryKeys
?
self
.
primaryKeyValues
:
{
id
:
self
.
id
};
identifier
=
self
.
__options
.
hasPrimaryKeys
?
self
.
primaryKeyValues
:
{
id
:
self
.
id
};
return
self
.
QueryInterface
.
delete
(
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
Model
),
identifier
,
options
);
return
self
.
QueryInterface
.
delete
(
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
Model
),
identifier
,
options
);
}
}
}).
then
(
function
(
results
)
{
}).
tap
(
function
(
result
)
{
return
self
.
Model
.
runHooks
(
self
.
Model
.
options
.
hooks
.
afterDestroy
,
self
).
return
(
results
);
// Run after hook
if
(
options
.
hooks
)
{
return
self
.
Model
.
runHooks
(
'afterDestroy'
,
self
);
}
}).
then
(
function
(
result
)
{
return
result
;
});
});
};
};
...
...
lib/model.js
View file @
ab1653d
This diff is collapsed.
Click to expand it.
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